fix edit forum
deskripsi: - fix edit forum
This commit is contained in:
54
src/app/api/forum/[id]/route.ts
Normal file
54
src/app/api/forum/[id]/route.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import backendLogger from "@/util/backendLogger";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export { GET };
|
||||||
|
|
||||||
|
async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const { id } = params;
|
||||||
|
|
||||||
|
const data = await prisma.forum_Posting.findUnique({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
diskusi: true,
|
||||||
|
isActive: true,
|
||||||
|
createdAt: true,
|
||||||
|
authorId: true,
|
||||||
|
Author: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
Profile: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
Forum_Komentar: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ForumMaster_StatusPosting: true,
|
||||||
|
forumMaster_StatusPostingId: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: "Success get data",
|
||||||
|
data: data,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
backendLogger.error("Error get data forum", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: "Error get data forum",
|
||||||
|
reason: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
import { Forum_EditPosting } from "@/app_modules/forum";
|
import { Forum_EditPosting } from "@/app_modules/forum";
|
||||||
import { forum_getOnePostingById } from "@/app_modules/forum/fun/get/get_one_posting_by_id";
|
|
||||||
|
|
||||||
export default async function Page({ params }: { params: { id: string } }) {
|
export default async function Page() {
|
||||||
let postingId = params.id;
|
|
||||||
const dataPosting = await forum_getOnePostingById(postingId)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Forum_EditPosting dataPosting={dataPosting as any} />
|
<Forum_EditPosting />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export { apiGetAllForum };
|
export { apiGetAllForum, apiGetOneForumById };
|
||||||
|
|
||||||
const apiGetAllForum = async ({
|
const apiGetAllForum = async ({
|
||||||
page,
|
page,
|
||||||
@@ -39,3 +39,35 @@ const apiGetAllForum = 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 apiGetOneForumById = 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/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 all forum:", response.statusText, errorData);
|
||||||
|
throw new Error(errorData?.message || "Failed to get all forum");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the JSON response
|
||||||
|
return await response.json();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error get all forum", error);
|
||||||
|
throw error; // Re-throw the error to handle it in the calling function
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,35 +1,23 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import {
|
import { Button, Group, Paper, Stack } from "@mantine/core";
|
||||||
ActionIcon,
|
|
||||||
Button,
|
|
||||||
Center,
|
|
||||||
Group,
|
|
||||||
Loader,
|
|
||||||
Paper,
|
|
||||||
Stack,
|
|
||||||
} from "@mantine/core";
|
|
||||||
import "react-quill/dist/quill.snow.css";
|
|
||||||
import "react-quill/dist/quill.bubble.css";
|
|
||||||
import { IconPhotoUp } from "@tabler/icons-react";
|
|
||||||
import { useShallowEffect } from "@mantine/hooks";
|
import { useShallowEffect } from "@mantine/hooks";
|
||||||
import { useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import ComponentGlobal_V2_LoadingPage from "@/app_modules/_global/loading_page_v2";
|
import "react-quill/dist/quill.bubble.css";
|
||||||
|
import "react-quill/dist/quill.snow.css";
|
||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import { MainColor } from "@/app_modules/_global/color/color_pallet";
|
||||||
import React, { useState } from "react";
|
import ComponentGlobal_InputCountDown from "@/app_modules/_global/component/input_countdown";
|
||||||
import { useAtom } from "jotai";
|
|
||||||
import { gs_forum_loading_edit_posting } from "../../global_state";
|
|
||||||
import { MODEL_FORUM_POSTING } from "../../model/interface";
|
|
||||||
import { forum_funEditPostingById } from "../../fun/edit/fun_edit_posting_by_id";
|
|
||||||
import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil";
|
import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil";
|
||||||
import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal";
|
import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal";
|
||||||
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
|
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
|
||||||
import ComponentGlobal_InputCountDown from "@/app_modules/_global/component/input_countdown";
|
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
|
||||||
import {
|
import { clientLogger } from "@/util/clientLogger";
|
||||||
AccentColor,
|
import dynamic from "next/dynamic";
|
||||||
MainColor,
|
import { useState } from "react";
|
||||||
} from "@/app_modules/_global/color/color_pallet";
|
import { apiGetOneForumById } from "../../component/api_fetch_forum";
|
||||||
|
import { forum_funEditPostingById } from "../../fun/edit/fun_edit_posting_by_id";
|
||||||
|
import { MODEL_FORUM_POSTING } from "../../model/interface";
|
||||||
const ReactQuill = dynamic(
|
const ReactQuill = dynamic(
|
||||||
() => {
|
() => {
|
||||||
return import("react-quill");
|
return import("react-quill");
|
||||||
@@ -37,23 +25,35 @@ const ReactQuill = dynamic(
|
|||||||
{ ssr: false }
|
{ ssr: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
export default function Forum_EditPosting({
|
const maxLength = 500;
|
||||||
dataPosting,
|
|
||||||
}: {
|
export default function Forum_EditPosting() {
|
||||||
dataPosting: MODEL_FORUM_POSTING;
|
const param = useParams<{ id: string }>();
|
||||||
}) {
|
const [data, setData] = useState<MODEL_FORUM_POSTING | null>(null);
|
||||||
const [value, setValue] = useState(dataPosting);
|
|
||||||
const [reload, setReload] = useState(false);
|
const [reload, setReload] = useState(false);
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
if (window && window.document) setReload(true);
|
if (window && window.document) setReload(true);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!reload)
|
useShallowEffect(() => {
|
||||||
return (
|
handleLoadData();
|
||||||
<>
|
}, []);
|
||||||
<ComponentGlobal_V2_LoadingPage />
|
|
||||||
</>
|
const handleLoadData = async () => {
|
||||||
);
|
try {
|
||||||
|
const response = await apiGetOneForumById({
|
||||||
|
id: param.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
setData(response.data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
clientLogger.error("Error get data forum", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!reload || !data) return <CustomSkeleton height={200} />;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -63,10 +63,21 @@ export default function Forum_EditPosting({
|
|||||||
theme="bubble"
|
theme="bubble"
|
||||||
placeholder="Apa yang sedang ingin dibahas ?"
|
placeholder="Apa yang sedang ingin dibahas ?"
|
||||||
style={{ height: 150 }}
|
style={{ height: 150 }}
|
||||||
value={value.diskusi}
|
value={data.diskusi}
|
||||||
onChange={(val) => {
|
onChange={(val) => {
|
||||||
setValue({
|
const input = val;
|
||||||
...value,
|
const text = input.replace(/<[^>]+>/g, "").trim(); // Remove HTML tags and trim whitespace
|
||||||
|
|
||||||
|
if (text.length === 0) {
|
||||||
|
setData({
|
||||||
|
...data,
|
||||||
|
diskusi: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setData({
|
||||||
|
...data,
|
||||||
diskusi: val,
|
diskusi: val,
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
@@ -74,15 +85,12 @@ export default function Forum_EditPosting({
|
|||||||
</Paper>
|
</Paper>
|
||||||
<Group position="right">
|
<Group position="right">
|
||||||
<ComponentGlobal_InputCountDown
|
<ComponentGlobal_InputCountDown
|
||||||
maxInput={500}
|
maxInput={maxLength}
|
||||||
lengthInput={value.diskusi.length}
|
lengthInput={data.diskusi.length}
|
||||||
/>
|
/>
|
||||||
</Group>
|
</Group>
|
||||||
<Group position="right">
|
<Group position="right">
|
||||||
{/* <ActionIcon>
|
<ButtonAction diskusi={data.diskusi as any} postingId={data.id} />
|
||||||
<IconPhotoUp />
|
|
||||||
</ActionIcon> */}
|
|
||||||
<ButtonAction diskusi={value.diskusi as any} postingId={value.id} />
|
|
||||||
</Group>
|
</Group>
|
||||||
</Stack>
|
</Stack>
|
||||||
{/* <div dangerouslySetInnerHTML={{ __html: value.diskusi }} /> */}
|
{/* <div dangerouslySetInnerHTML={{ __html: value.diskusi }} /> */}
|
||||||
@@ -104,15 +112,21 @@ function ButtonAction({
|
|||||||
if (diskusi === "<p><br></p>" || diskusi === "")
|
if (diskusi === "<p><br></p>" || diskusi === "")
|
||||||
return ComponentGlobal_NotifikasiPeringatan("Masukan postingan anda");
|
return ComponentGlobal_NotifikasiPeringatan("Masukan postingan anda");
|
||||||
|
|
||||||
await forum_funEditPostingById(postingId, diskusi).then((res) => {
|
try {
|
||||||
if (res.status === 200) {
|
setLoading(true);
|
||||||
setLoading(true);
|
const update = await forum_funEditPostingById(postingId, diskusi);
|
||||||
ComponentGlobal_NotifikasiBerhasil(res.message);
|
|
||||||
setTimeout(() => router.back(), 1000);
|
if (update.status === 200) {
|
||||||
|
ComponentGlobal_NotifikasiBerhasil(update.message);
|
||||||
|
router.back();
|
||||||
} else {
|
} else {
|
||||||
ComponentGlobal_NotifikasiGagal(res.message);
|
ComponentGlobal_NotifikasiGagal(update.message);
|
||||||
}
|
}
|
||||||
});
|
} catch (error) {
|
||||||
|
clientLogger.error("Error update forum", error);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -120,23 +134,24 @@ function ButtonAction({
|
|||||||
<Button
|
<Button
|
||||||
style={{
|
style={{
|
||||||
transition: "0.5s",
|
transition: "0.5s",
|
||||||
border:
|
|
||||||
diskusi === "<p><br></p>" || diskusi === "" || diskusi.length > 500
|
|
||||||
? ""
|
|
||||||
: `1px solid ${AccentColor.yellow}`,
|
|
||||||
backgroundColor:
|
backgroundColor:
|
||||||
diskusi === "<p><br></p>" || diskusi === "" || diskusi.length > 500
|
diskusi === "<p><br></p>" ||
|
||||||
|
diskusi === "" ||
|
||||||
|
diskusi.length >= maxLength
|
||||||
? ""
|
? ""
|
||||||
: MainColor.yellow,
|
: MainColor.yellow,
|
||||||
}}
|
}}
|
||||||
disabled={
|
disabled={
|
||||||
diskusi === "<p><br></p>" || diskusi === "" || diskusi.length > 500
|
diskusi === "<p><br></p>" ||
|
||||||
|
diskusi === "" ||
|
||||||
|
diskusi.length >= maxLength
|
||||||
? true
|
? true
|
||||||
: false
|
: false
|
||||||
}
|
}
|
||||||
loaderPosition="center"
|
loaderPosition="center"
|
||||||
loading={loading ? true : false}
|
loading={loading}
|
||||||
radius={"xl"}
|
radius={"xl"}
|
||||||
|
c={"black"}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
onUpdate();
|
onUpdate();
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -7,16 +7,22 @@ export async function forum_funEditPostingById(
|
|||||||
postingId: string,
|
postingId: string,
|
||||||
diskusi: string
|
diskusi: string
|
||||||
) {
|
) {
|
||||||
const updt = await prisma.forum_Posting.update({
|
try {
|
||||||
where: {
|
const updt = await prisma.forum_Posting.update({
|
||||||
id: postingId,
|
where: {
|
||||||
},
|
id: postingId,
|
||||||
data: {
|
},
|
||||||
diskusi: diskusi,
|
data: {
|
||||||
},
|
diskusi: diskusi,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!updt) return { status: 400, message: "Gagal update" };
|
if (!updt) {
|
||||||
revalidatePath("/dev/forum/main");
|
return { success: false, message: "Update gagal", status: 400 }; // Plain object dengan status
|
||||||
return { status: 200, message: "Berhasil update" };
|
}
|
||||||
|
revalidatePath("/dev/forum/main");
|
||||||
|
return { success: true, message: "Berhasil update", status: 200 }; // Plain object dengan status
|
||||||
|
} catch (error) {
|
||||||
|
return { success: false, message: "Update error", status: 500 }; // Plain object dengan status
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user