upd: document
Deskripsi: - delete document No Issues
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
};
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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() {
|
||||
</ActionIcon>
|
||||
}
|
||||
/>
|
||||
|
||||
|
||||
<Box>
|
||||
<Box p={20} pb={60}>
|
||||
{dataDocument.map((v, i) => {
|
||||
@@ -295,11 +313,18 @@ export default function NavbarDocumentDivision() {
|
||||
</LayoutDrawer>
|
||||
|
||||
|
||||
|
||||
{/* MODAL KONFIRMASI DELETE */}
|
||||
<LayoutModal opened={isDelete} onClose={() => 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 */}
|
||||
<Modal styles={{
|
||||
body: {
|
||||
borderRadius: 20
|
||||
|
||||
Reference in New Issue
Block a user