fix api forum

deskripsi:
- delete  button and function
This commit is contained in:
2025-02-19 10:30:02 +08:00
parent 4318ea4890
commit 0d230c3c47
9 changed files with 145 additions and 125 deletions

View File

@@ -24,7 +24,6 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
Profile: true,
},
},
_count: {
select: {
Forum_Komentar: true,
@@ -35,10 +34,15 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
},
});
const fixData = {
...data,
count: data?._count.Forum_Komentar,
}
return NextResponse.json({
success: true,
message: "Success get data",
data: data,
data: fixData,
});
} catch (error) {
backendLogger.error("Error get data forum", error);

View File

@@ -1,31 +1,12 @@
import { RouterForum } from "@/lib/router_hipmi/router_forum";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import Forum_MainDetail from "@/app_modules/forum/detail/main_detail";
import { forum_countTotalKomenById } from "@/app_modules/forum/fun/count/count_total_komentar_by_id";
import { forum_funGetAllKomentarById } from "@/app_modules/forum/fun/get/get_all_komentar_by_id";
import { forum_getOnePostingById } from "@/app_modules/forum/fun/get/get_one_posting_by_id";
import { redirect } from "next/navigation";
export default async function Page({ params }: { params: { id: string } }) {
let postingId = params.id;
export default async function Page() {
const userLoginId = await funGetUserIdByToken();
// const dataPosting = await forum_getOnePostingById(postingId);
const listKomentar = await forum_funGetAllKomentarById({
postingId: postingId,
page: 1,
});
const countKomentar = await forum_countTotalKomenById(postingId);
return (
<>
<Forum_MainDetail
// dataPosting={dataPosting as any}
// listKomentar={listKomentar as any}
userLoginId={userLoginId as string}
countKomentar={countKomentar}
/>
<Forum_MainDetail userLoginId={userLoginId as string} />
</>
);
}

View File

@@ -23,13 +23,11 @@ import backendLogger from "@/util/backendLogger";
import { clientLogger } from "@/util/clientLogger";
export default function ComponentForum_DetailCreateKomentar({
postingId,
onSetKomentar,
data,
userLoginId,
onSetNewKomentar,
}: {
postingId: string;
onSetKomentar: (val: any) => void;
data: MODEL_FORUM_POSTING;
userLoginId: string;
onSetNewKomentar: (val: boolean) => void;
@@ -48,11 +46,6 @@ export default function ComponentForum_DetailCreateKomentar({
setLoading(true);
const createComment = await forum_funCreateKomentar(postingId, value);
if (createComment.status === 201) {
// const loadData = await forum_funGetAllKomentarById({
// postingId: data.id,
// page: 1,
// });
// onSetKomentar(loadData);
onSetNewKomentar(true);
setValue("");
@@ -88,7 +81,6 @@ export default function ComponentForum_DetailCreateKomentar({
}
} catch (error) {
setLoading(false);
clientLogger.error("Error create komentar forum", error);
}
}

View File

@@ -50,6 +50,8 @@ import {
} from "@/app_modules/_global/color/color_pallet";
import { ComponentGlobal_LoaderAvatar } from "@/app_modules/_global/component";
import ComponentGlobal_Loader from "@/app_modules/_global/component/loader";
import { clientLogger } from "@/util/clientLogger";
import _ from "lodash";
export default function ComponentForum_DetailHeader({
data,
@@ -123,7 +125,8 @@ export default function ComponentForum_DetailHeader({
postingId={data?.id}
authorId={data?.Author.id}
userLoginId={userLoginId}
statusId={data?.forumMaster_StatusPostingId}
statusId={data?.ForumMaster_StatusPosting.id}
dataPosting={data}
onLoadData={(val) => {
onLoadData(val);
}}
@@ -141,12 +144,14 @@ function ComponentForum_DetailButtonMore_V2({
postingId,
statusId,
userLoginId,
dataPosting,
onLoadData,
}: {
authorId: any;
postingId?: any;
statusId: any;
userLoginId: any;
dataPosting: any;
onLoadData: (val: any) => void;
}) {
const router = useRouter();
@@ -312,6 +317,7 @@ function ComponentForum_DetailButtonMore_V2({
postingId={postingId}
setOpenStatus={setOpenStatusClose}
statusId={statusId}
dataPosting={dataPosting}
onLoadData={(val) => {
onLoadData(val);
}}
@@ -338,13 +344,15 @@ function ButtonDelete({
if (loading) return <ComponentGlobal_V2_LoadingPage />;
async function onDelete() {
setOpenDel(false);
await forum_funDeletePostingById(postingId as any).then((res) => {
if (res.status === 200) {
ComponentGlobal_NotifikasiBerhasil(`Postingan Terhapus`, 2000);
setLoading(true);
try {
setLoading(true);
const responseDelete = await forum_funDeletePostingById(postingId as any);
if (responseDelete.status === 200) {
setOpenDel(false);
router.back();
ComponentGlobal_NotifikasiBerhasil(`Postingan Terhapus`, 2000);
// mqtt_client.publish(
// "Forum_detail_hapus_data",
// JSON.stringify({
@@ -352,9 +360,13 @@ function ButtonDelete({
// })
// );
} else {
ComponentGlobal_NotifikasiGagal(res.message);
setLoading(false);
ComponentGlobal_NotifikasiGagal(responseDelete.message);
}
});
} catch (error) {
setLoading(false);
clientLogger.error("Error get data forum", error);
}
}
return (
<>
@@ -387,38 +399,40 @@ function ButtonStatus({
postingId,
setOpenStatus,
statusId,
dataPosting,
onLoadData,
}: {
postingId?: string;
setOpenStatus: any;
statusId?: any;
dataPosting: any;
onLoadData: (val: any) => void;
}) {
const [loading, setLoading] = useState(false);
async function onTutupForum() {
setOpenStatus(false);
const closeForum = await forum_funEditStatusPostingById(
postingId as any,
2
);
if (closeForum.status === 200) {
ComponentGlobal_NotifikasiBerhasil(`Forum Ditutup`, 2000);
try {
setLoading(true);
const closeForum = await forum_funEditStatusPostingById(
postingId as any,
2
);
const loadData = await forum_getOnePostingById(postingId as any);
onLoadData(loadData);
if (closeForum.status === 200) {
setOpenStatus(false);
ComponentGlobal_NotifikasiBerhasil(`Forum Ditutup`, 2000);
if (loadData) {
const cloneData = _.clone(dataPosting);
const updateData = {
...loadData,
...cloneData,
ForumMaster_StatusPosting: {
id: 2,
status: "Close",
},
};
onLoadData(updateData);
mqtt_client.publish(
"Forum_detail_ganti_status",
JSON.stringify({
@@ -426,32 +440,39 @@ function ButtonStatus({
data: updateData.ForumMaster_StatusPosting,
})
);
} else {
setLoading(false);
ComponentGlobal_NotifikasiGagal(closeForum.message);
}
} else {
ComponentGlobal_NotifikasiGagal(closeForum.message);
} catch (error) {
setLoading(false);
clientLogger.error("Error get data forum", error);
}
}
async function onBukaForum() {
setOpenStatus(false);
setLoading(true);
const openForum = await forum_funEditStatusPostingById(postingId as any, 1);
if (openForum.status === 200) {
ComponentGlobal_NotifikasiBerhasil(`Forum Dibuka`, 2000);
setLoading(true);
try {
const openForum = await forum_funEditStatusPostingById(
postingId as any,
1
);
if (openForum.status === 200) {
setOpenStatus(false);
ComponentGlobal_NotifikasiBerhasil(`Forum Dibuka`, 2000);
const loadData = await forum_getOnePostingById(postingId as any);
onLoadData(loadData);
if (loadData) {
const cloneData = _.clone(dataPosting);
const updateData = {
...loadData,
...cloneData,
ForumMaster_StatusPosting: {
id: 1,
status: "Open",
},
};
onLoadData(updateData);
mqtt_client.publish(
"Forum_detail_ganti_status",
JSON.stringify({
@@ -459,9 +480,13 @@ function ButtonStatus({
data: updateData.ForumMaster_StatusPosting,
})
);
} else {
setLoading(false);
ComponentGlobal_NotifikasiGagal(openForum.message);
}
} else {
ComponentGlobal_NotifikasiGagal(openForum.message);
} catch (error) {
setLoading(false);
clientLogger.error("Error get data forum", error);
}
}

View File

@@ -1,14 +1,10 @@
"use client";
import { Card, Stack, Group, Text, Box } from "@mantine/core";
import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component";
import { Box, Group, Stack, Text } from "@mantine/core";
import { IconMessageCircle, IconMessageCircleX } from "@tabler/icons-react";
import { MODEL_FORUM_POSTING } from "../../model/interface";
import ComponentForum_DetailHeader from "./detail_header";
import {
AccentColor,
MainColor,
} from "@/app_modules/_global/color/color_pallet";
import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component";
export default function ComponentForum_DetailForumView({
data,

View File

@@ -27,16 +27,17 @@ import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empt
export default function Forum_MainDetail({
userLoginId,
countKomentar,
}: {
userLoginId: string;
countKomentar: number;
}) {
const param = useParams<{ id: string }>();
const [data, setData] = useState<MODEL_FORUM_POSTING | null>(null);
const [lsKomentar, setLsKomentar] = useState<MODEL_FORUM_KOMENTAR[]>([]);
const [dataPosting, setDataPosting] = useState<MODEL_FORUM_POSTING | null>(
null
);
const [listKomentar, setListKomentar] = useState<MODEL_FORUM_KOMENTAR[]>([]);
const [activePage, setActivePage] = useState(1);
const [newKomentar, setNewKomentar] = useState(false);
const [isLoading, setIsLoading] = useState(false);
useShallowEffect(() => {
handleLoadData();
@@ -44,16 +45,21 @@ export default function Forum_MainDetail({
const handleLoadData = async () => {
try {
setIsLoading(true);
const response = await apiGetOneForumById({
id: param.id,
});
if (response) {
setData(response.data);
setDataPosting(response.data);
} else {
setDataPosting(null);
}
} catch (error) {
clientLogger.error("Error get data forum", error);
setData(null);
setDataPosting(null);
} finally {
setIsLoading(false);
}
};
@@ -69,13 +75,13 @@ export default function Forum_MainDetail({
});
if (response.success) {
setLsKomentar(response.data);
setListKomentar(response.data);
} else {
setLsKomentar([]);
setListKomentar([]);
}
} catch (error) {
clientLogger.error("Error get data komentar forum", error);
setLsKomentar([]);
setListKomentar([]);
}
};
@@ -104,8 +110,8 @@ export default function Forum_MainDetail({
mqtt_client.on("message", (topic: any, message: any) => {
const newData = JSON.parse(message.toString());
if (newData.id === data?.id) {
const cloneData = _.clone(data);
if (newData.id === dataPosting?.id) {
const cloneData = _.clone(dataPosting);
// console.log(newData.data);
const updateData = {
@@ -116,37 +122,34 @@ export default function Forum_MainDetail({
},
};
setData(updateData as any);
setDataPosting(updateData as any);
}
});
}, [data]);
}, [dataPosting]);
return (
<>
<Stack>
{!data ? (
{!dataPosting || !listKomentar ? (
<CustomSkeleton height={200} width={"100%"} />
) : (
<ComponentForum_DetailForumView
data={data}
totalKomentar={countKomentar}
data={dataPosting}
totalKomentar={dataPosting.count}
userLoginId={userLoginId}
onLoadData={(val) => {
setData(val);
setDataPosting(val);
}}
/>
)}
{!data ? (
{!dataPosting ? (
<Forum_SkeletonKomentar />
) : (
(data?.ForumMaster_StatusPosting?.id as any) === 1 && (
(dataPosting?.ForumMaster_StatusPosting?.id as any) === 1 && (
<ComponentForum_DetailCreateKomentar
postingId={data?.id}
onSetKomentar={(val) => {
setLsKomentar(val);
}}
data={data}
postingId={dataPosting?.id}
data={dataPosting}
userLoginId={userLoginId}
onSetNewKomentar={(val) => {
setNewKomentar(val);
@@ -155,12 +158,12 @@ export default function Forum_MainDetail({
)
)}
{!lsKomentar.length ? (
{!listKomentar.length && isLoading ? (
<Forum_SkeletonListKomentar />
) : _.isEmpty(lsKomentar) ? (
<ComponentGlobal_IsEmptyData />
) : _.isEmpty(listKomentar) ? (
<ComponentGlobal_IsEmptyData text="Tidak ada komentar" />
) : (
<Box >
<Box>
<ScrollOnly
height={"60vh"}
renderLoading={() => (
@@ -168,15 +171,15 @@ export default function Forum_MainDetail({
<Loader color={"yellow"} />
</Center>
)}
data={lsKomentar}
setData={setLsKomentar}
data={listKomentar}
setData={setListKomentar}
moreData={handleMoreDataKomentar}
>
{(item) => (
<ComponentForum_KomentarView
data={item}
setKomentar={setLsKomentar}
postingId={data?.id as any}
setKomentar={setListKomentar}
postingId={dataPosting?.id as any}
userLoginId={userLoginId}
/>
)}

View File

@@ -8,17 +8,26 @@ export async function forum_funCreateKomentar(
postingId: string,
komentar: string
) {
const userLoginId = await funGetUserIdByToken();
try {
const userLoginId = await funGetUserIdByToken();
const create = await prisma.forum_Komentar.create({
data: {
komentar: komentar,
forum_PostingId: postingId,
authorId: userLoginId,
},
});
const create = await prisma.forum_Komentar.create({
data: {
komentar: komentar,
forum_PostingId: postingId,
authorId: userLoginId,
},
});
if (!create) return { status: 400, message: "Gagal menambahkan komentar" };
revalidatePath("/dev/forum/detail");
return { status: 201, message: "Berhasil menambahkan komentar" };
if (!create) return { status: 400, message: "Gagal menambahkan komentar" };
return { status: 201, message: "Berhasil menambahkan komentar" };
} catch (error) {
console.log(error);
return {
status: 500,
message: "Error API",
error: (error as Error).message,
};
}
}

View File

@@ -4,16 +4,25 @@ import prisma from "@/lib/prisma";
import { revalidatePath } from "next/cache";
export async function forum_funDeletePostingById(forumId: string) {
const del = await prisma.forum_Posting.update({
where: {
id: forumId,
},
data: {
isActive: false,
},
});
try {
const del = await prisma.forum_Posting.update({
where: {
id: forumId,
},
data: {
isActive: false,
},
});
if (!del) return { status: 400, message: "Gagal dihapus" };
revalidatePath("/dev/forum/main");
return { status: 200, message: "Berhasil dihapus" };
if (!del) return { status: 400, message: "Gagal dihapus" };
revalidatePath("/dev/forum/main");
return { status: 200, message: "Berhasil dihapus" };
} catch (error) {
console.log(error);
return {
status: 500,
message: "Error API",
error: (error as Error).message,
};
}
}

View File

@@ -14,6 +14,7 @@ export interface MODEL_FORUM_POSTING {
Forum_ReportPosting: MODEL_FORUM_MASTER_REPORT[];
ForumMaster_StatusPosting: MODEL_FORUM_MASTER_STATUS;
forumMaster_StatusPostingId: number;
count: number
}
export interface MODEL_FORUM_KOMENTAR {