fix: forum admin
perubahan pada metode API
This commit is contained in:
65
src/app/api/admin/forum/[id]/route.ts
Normal file
65
src/app/api/admin/forum/[id]/route.ts
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import prisma from "@/lib/prisma";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const { id } = params;
|
||||||
|
const data = await prisma.forum_Posting.findFirst({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
diskusi: true,
|
||||||
|
ForumMaster_StatusPosting: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
status: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
authorId: true,
|
||||||
|
Author: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
Profile: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Forum_Komentar: {
|
||||||
|
where: {
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
..._.omit(data, "Forum_Komentar"),
|
||||||
|
Forum_Komentar: data?.Forum_Komentar.length,
|
||||||
|
};
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: "Success get data",
|
||||||
|
data: result,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error get data forum", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: "Error get data forum",
|
||||||
|
reason: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,23 +1,9 @@
|
|||||||
import { AdminForum_LihatSemuaKomentar } from "@/app_modules/admin/forum";
|
import { AdminForum_LihatSemuaKomentar } from "@/app_modules/admin/forum";
|
||||||
import { adminForum_getOnePostingById } from "@/app_modules/admin/forum/fun/get/get_one_posting_by_id";
|
|
||||||
|
|
||||||
export default async function Page({ params }: { params: { id: string } }) {
|
|
||||||
let postingId = params.id;
|
|
||||||
|
|
||||||
// const listKomentar = await adminForum_getListKomentarById({
|
|
||||||
// postingId: postingId,
|
|
||||||
// page: 1,
|
|
||||||
// });
|
|
||||||
const dataPosting = await adminForum_getOnePostingById(postingId);
|
|
||||||
// const countKomentar = await adminForum_countKomentarByPostingId({
|
|
||||||
// postingId: postingId,
|
|
||||||
// });
|
|
||||||
|
|
||||||
|
export default async function Page() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AdminForum_LihatSemuaKomentar
|
<AdminForum_LihatSemuaKomentar />
|
||||||
dataPosting={dataPosting as any}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import { useDisclosure, useShallowEffect } from "@mantine/hooks";
|
|||||||
import { IconFlag3, IconSearch, IconTrash } from "@tabler/icons-react";
|
import { IconFlag3, IconSearch, IconTrash } from "@tabler/icons-react";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import "moment/locale/id";
|
import "moment/locale/id";
|
||||||
import { useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component";
|
import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component";
|
||||||
import { Admin_ComponentModal } from "../../_admin_global/_component/comp_admin_modal";
|
import { Admin_ComponentModal } from "../../_admin_global/_component/comp_admin_modal";
|
||||||
@@ -36,30 +36,63 @@ import Admin_ComponentBackButton from "../../_admin_global/back_button";
|
|||||||
import { Admin_V3_ComponentPaginationBreakpoint } from "../../_components_v3/comp_pagination_breakpoint";
|
import { Admin_V3_ComponentPaginationBreakpoint } from "../../_components_v3/comp_pagination_breakpoint";
|
||||||
import ComponentAdminForum_ViewOneDetailPosting from "../component/detail_one_posting";
|
import ComponentAdminForum_ViewOneDetailPosting from "../component/detail_one_posting";
|
||||||
import { adminForum_funDeleteKomentarById } from "../fun/delete/fun_delete_komentar_by_id";
|
import { adminForum_funDeleteKomentarById } from "../fun/delete/fun_delete_komentar_by_id";
|
||||||
import { apiAdminGetKomentarForumById } from "../lib/api_fetch_admin_forum";
|
import {
|
||||||
|
apiAdminGetKomentarForumById,
|
||||||
|
apiAdminGetPostingForumById,
|
||||||
|
} from "../lib/api_fetch_admin_forum";
|
||||||
import { AdminForum_CompTableSetHtmlStiker } from "../component/comp_table_set_html_stiker";
|
import { AdminForum_CompTableSetHtmlStiker } from "../component/comp_table_set_html_stiker";
|
||||||
import { Admin_V3_ComponentBreakpoint } from "../../_components_v3/comp_simple_grid_breakpoint";
|
import { Admin_V3_ComponentBreakpoint } from "../../_components_v3/comp_simple_grid_breakpoint";
|
||||||
|
|
||||||
export default function AdminForum_DetailPosting({
|
export default function AdminForum_DetailPosting() {
|
||||||
dataPosting,
|
const { id } = useParams();
|
||||||
}: {
|
const [data, setData] = useState<MODEL_FORUM_POSTING | null>(null);
|
||||||
dataPosting: MODEL_FORUM_POSTING;
|
|
||||||
}) {
|
useShallowEffect(() => {
|
||||||
|
onLoadData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
async function onLoadData() {
|
||||||
|
try {
|
||||||
|
const response = await apiAdminGetPostingForumById({ id: id as string });
|
||||||
|
if (response && response.success) {
|
||||||
|
setData(response.data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Invalid data format received:", error);
|
||||||
|
setData(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Stack>
|
<Stack>
|
||||||
<ComponentAdminGlobal_HeaderTamplate name="Forum: Detail" />
|
<ComponentAdminGlobal_HeaderTamplate name="Forum: Detail" />
|
||||||
<Admin_ComponentBackButton />
|
<Admin_ComponentBackButton />
|
||||||
<Admin_V3_ComponentBreakpoint>
|
{!data ? (
|
||||||
<ComponentAdminForum_ViewOneDetailPosting dataPosting={dataPosting} />
|
<CustomSkeleton height={"80vh"} width={"100%"} />
|
||||||
</Admin_V3_ComponentBreakpoint>
|
) : (
|
||||||
<TableKomentar postingId={dataPosting.id} totalComments={dataPosting.Forum_Komentar as any} />
|
<>
|
||||||
|
<Admin_V3_ComponentBreakpoint>
|
||||||
|
<ComponentAdminForum_ViewOneDetailPosting dataPosting={data} />
|
||||||
|
</Admin_V3_ComponentBreakpoint>
|
||||||
|
<TableKomentar
|
||||||
|
postingId={id as string}
|
||||||
|
totalComments={data?.Forum_Komentar as any}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function TableKomentar({ postingId, totalComments }: { postingId: string, totalComments: number }) {
|
function TableKomentar({
|
||||||
|
postingId,
|
||||||
|
totalComments,
|
||||||
|
}: {
|
||||||
|
postingId: string;
|
||||||
|
totalComments: number;
|
||||||
|
}) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [data, setData] = useState<MODEL_FORUM_KOMENTAR[] | null>(null);
|
const [data, setData] = useState<MODEL_FORUM_KOMENTAR[] | null>(null);
|
||||||
const [nPage, setNPage] = useState<number>(1);
|
const [nPage, setNPage] = useState<number>(1);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export {
|
|||||||
apiGetAdminForumPublish,
|
apiGetAdminForumPublish,
|
||||||
apiGetAdminHasilReportPosting,
|
apiGetAdminHasilReportPosting,
|
||||||
apiAdminGetKomentarForumById,
|
apiAdminGetKomentarForumById,
|
||||||
|
apiAdminGetPostingForumById,
|
||||||
};
|
};
|
||||||
|
|
||||||
const apiGetAdminForumPublishCountDasboard = async () => {
|
const apiGetAdminForumPublishCountDasboard = async () => {
|
||||||
@@ -212,3 +213,46 @@ const apiAdminGetKomentarForumById = async ({
|
|||||||
throw error; // Re-throw the error to handle it in the calling function
|
throw error; // Re-throw the error to handle it in the calling function
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const apiAdminGetPostingForumById = async ({
|
||||||
|
id,
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
}) => {
|
||||||
|
try {
|
||||||
|
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
|
||||||
|
if (!token) {
|
||||||
|
console.error("No token found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`/api/admin/forum/${id}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check if the response is OK
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json().catch(() => null);
|
||||||
|
console.error(
|
||||||
|
"Failed to get admin posting forum",
|
||||||
|
response.statusText,
|
||||||
|
errorData
|
||||||
|
);
|
||||||
|
throw new Error(
|
||||||
|
errorData?.message || "Failed to get admin posting forum"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the JSON response
|
||||||
|
const resulst = await response.json();
|
||||||
|
return resulst;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error get admin posting forum", error);
|
||||||
|
throw error; // Re-throw the error to handle it in the calling function
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user