From 2f31e4cd7af5ddd9353f7d835912cd92f4770372 Mon Sep 17 00:00:00 2001 From: amel Date: Wed, 21 Aug 2024 17:01:57 +0800 Subject: [PATCH] upd: document Deskripsi: - delete document No Issues --- src/app/api/document/route.ts | 51 ++++++++++++++++++- src/module/document/lib/api_document.ts | 11 ++++ .../document/ui/navbar_document_division.tsx | 35 +++++++++++-- 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/app/api/document/route.ts b/src/app/api/document/route.ts index fc8c9e6..331595d 100644 --- a/src/app/api/document/route.ts +++ b/src/app/api/document/route.ts @@ -29,6 +29,21 @@ export async function GET(request: Request) { return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 }); } + if (path != "home" && path != "null" && path != "undefined" && path != "") { + const cekPath = await prisma.divisionDocumentFolderFile.count({ + where: { + isActive: true, + id: String(path) + } + }) + + if (cekPath == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan item, data tidak ditemukan" }, { status: 404 }); + } + } + + + const data = await prisma.divisionDocumentFolderFile.findMany({ where: { @@ -63,11 +78,11 @@ export async function GET(request: Request) { })) - return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData, }, { status: 200 }); + return NextResponse.json({ success: true, message: "Berhasil mendapatkan item", data: allData, }, { status: 200 }); } catch (error) { console.log(error); - return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); + return NextResponse.json({ success: false, message: "Gagal mendapatkan item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); } } @@ -142,6 +157,7 @@ export async function POST(request: Request) { }; +// RENAME ITEM export async function PUT(request: Request) { try { const user = await funGetUserByCookies() @@ -195,4 +211,35 @@ export async function PUT(request: Request) { console.log(error); return NextResponse.json({ success: false, message: "Gagal mengubah nama item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); } +}; + + +// DELETE ITEM +export async function DELETE(request: Request) { + try { + const user = await funGetUserByCookies() + if (user.id == undefined) { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const data = await request.json() + + for (let i = 0; i < data.length; i++) { + const id = data[i].id; + const cekFile = await prisma.divisionDocumentFolderFile.update({ + where: { + id: id + }, + data: { + isActive: false + } + }) + } + + + return NextResponse.json({ success: true, message: "Berhasil menghapus item" }, { status: 200 }); + } catch (error) { + console.log(error); + return NextResponse.json({ success: false, message: "Gagal menghapus item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); + } }; \ No newline at end of file diff --git a/src/module/document/lib/api_document.ts b/src/module/document/lib/api_document.ts index a596c4b..afc3e86 100644 --- a/src/module/document/lib/api_document.ts +++ b/src/module/document/lib/api_document.ts @@ -32,4 +32,15 @@ export const funRenameDocument = async (data: IFormEditItem) => { body: JSON.stringify(data), }); return await response.json().catch(() => null); +}; + +export const funDeleteDocument = async (data: []) => { + const response = await fetch("/api/document", { + method: "DELETE", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }); + return await response.json().catch(() => null); }; \ No newline at end of file diff --git a/src/module/document/ui/navbar_document_division.tsx b/src/module/document/ui/navbar_document_division.tsx index 27a0d1f..1f35cbe 100644 --- a/src/module/document/ui/navbar_document_division.tsx +++ b/src/module/document/ui/navbar_document_division.tsx @@ -16,7 +16,7 @@ import DrawerMenuDocumentDivision from './drawer_menu_document_division'; import DrawerMore from './drawer_more'; import { funGetDivisionById } from '@/module/division_new'; import { useShallowEffect } from '@mantine/hooks'; -import { funGetAllDocument, funRenameDocument } from '../lib/api_document'; +import { funDeleteDocument, funGetAllDocument, funRenameDocument } from '../lib/api_document'; import { IDataDocument } from '../lib/type_document'; import { useHookstate } from '@hookstate/core'; import { globalRefreshDocument } from '../lib/val_document'; @@ -100,12 +100,28 @@ export default function NavbarDocumentDivision() { } - function onTrue(val: boolean) { + async function onConfirmDelete(val: boolean) { if (val) { - toast.success("Sukses! Data dihapus"); + try { + const respon = await funDeleteDocument(selectedFiles) + if (respon.success) { + getOneData() + } else { + toast.error(respon.message) + } + } catch (error) { + console.log(error) + toast.error("Gagal menghapus item, coba lagi nanti") + } + + handleBatal() } + setIsDelete(false) } + + + async function onRenameSubmit() { try { const res = await funRenameDocument(bodyRename) @@ -238,6 +254,8 @@ export default function NavbarDocumentDivision() { } /> + + {dataDocument.map((v, i) => { @@ -295,11 +313,18 @@ export default function NavbarDocumentDivision() { + + {/* MODAL KONFIRMASI DELETE */} setIsDelete(false)} - description="Apakah Anda yakin ingin menghapus data?" - onYes={(val) => { onTrue(val) }} /> + description="Apakah Anda yakin ingin menghapus item?" + onYes={(val) => { + onConfirmDelete(val) + }} /> + + + {/* MODAL RENAME */}