diff --git a/src/app/api/forum/[id]/route.ts b/src/app/api/forum/[id]/route.ts index 9a18775f..080024b0 100644 --- a/src/app/api/forum/[id]/route.ts +++ b/src/app/api/forum/[id]/route.ts @@ -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); diff --git a/src/app/dev/forum/detail/main-detail/[id]/page.tsx b/src/app/dev/forum/detail/main-detail/[id]/page.tsx index 3905795f..596596bb 100644 --- a/src/app/dev/forum/detail/main-detail/[id]/page.tsx +++ b/src/app/dev/forum/detail/main-detail/[id]/page.tsx @@ -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 ( <> - + ); } diff --git a/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx b/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx index 224220aa..2057c68d 100644 --- a/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx +++ b/src/app_modules/forum/component/detail_component/detail_create_komentar.tsx @@ -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); } } diff --git a/src/app_modules/forum/component/detail_component/detail_header.tsx b/src/app_modules/forum/component/detail_component/detail_header.tsx index c232ddf7..f012055e 100644 --- a/src/app_modules/forum/component/detail_component/detail_header.tsx +++ b/src/app_modules/forum/component/detail_component/detail_header.tsx @@ -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 ; 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); } } diff --git a/src/app_modules/forum/component/detail_component/detail_view.tsx b/src/app_modules/forum/component/detail_component/detail_view.tsx index c445c779..7f36366b 100644 --- a/src/app_modules/forum/component/detail_component/detail_view.tsx +++ b/src/app_modules/forum/component/detail_component/detail_view.tsx @@ -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, diff --git a/src/app_modules/forum/detail/main_detail.tsx b/src/app_modules/forum/detail/main_detail.tsx index c0353948..962a3159 100644 --- a/src/app_modules/forum/detail/main_detail.tsx +++ b/src/app_modules/forum/detail/main_detail.tsx @@ -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(null); - const [lsKomentar, setLsKomentar] = useState([]); + const [dataPosting, setDataPosting] = useState( + null + ); + const [listKomentar, setListKomentar] = useState([]); 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 ( <> - {!data ? ( + {!dataPosting || !listKomentar ? ( ) : ( { - setData(val); + setDataPosting(val); }} /> )} - {!data ? ( + {!dataPosting ? ( ) : ( - (data?.ForumMaster_StatusPosting?.id as any) === 1 && ( + (dataPosting?.ForumMaster_StatusPosting?.id as any) === 1 && ( { - 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 ? ( - ) : _.isEmpty(lsKomentar) ? ( - + ) : _.isEmpty(listKomentar) ? ( + ) : ( - + ( @@ -168,15 +171,15 @@ export default function Forum_MainDetail({ )} - data={lsKomentar} - setData={setLsKomentar} + data={listKomentar} + setData={setListKomentar} moreData={handleMoreDataKomentar} > {(item) => ( )} diff --git a/src/app_modules/forum/fun/create/fun_create_komentar.tsx b/src/app_modules/forum/fun/create/fun_create_komentar.tsx index a58f595b..7468af8d 100644 --- a/src/app_modules/forum/fun/create/fun_create_komentar.tsx +++ b/src/app_modules/forum/fun/create/fun_create_komentar.tsx @@ -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, + }; + } } diff --git a/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts b/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts index c5a21a2f..0e59acaf 100644 --- a/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts +++ b/src/app_modules/forum/fun/delete/fun_delete_posting_by_id.ts @@ -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, + }; + } } diff --git a/src/app_modules/forum/model/interface.tsx b/src/app_modules/forum/model/interface.tsx index ae216cce..83c7a8df 100644 --- a/src/app_modules/forum/model/interface.tsx +++ b/src/app_modules/forum/model/interface.tsx @@ -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 {