From 3d513c0d08a88be5d2ee8c4560c0c1930708a18a Mon Sep 17 00:00:00 2001 From: amel Date: Thu, 22 Aug 2024 14:30:46 +0800 Subject: [PATCH] upd : document Deskripsi: - copy item nb : baru setengah selesai, aku nyerah aja lahh nanti aja pikirin nya No Issues --- src/app/api/document/more/route.ts | 145 +++++++++++++++++- src/module/document/lib/api_document.ts | 13 +- src/module/document/lib/type_document.ts | 6 + .../document/ui/drawer_cut_documents.tsx | 10 +- src/module/document/ui/drawer_more.tsx | 29 +++- .../document/ui/navbar_document_division.tsx | 6 +- 6 files changed, 196 insertions(+), 13 deletions(-) diff --git a/src/app/api/document/more/route.ts b/src/app/api/document/more/route.ts index 8127fab..60cead8 100644 --- a/src/app/api/document/more/route.ts +++ b/src/app/api/document/more/route.ts @@ -28,13 +28,38 @@ export async function POST(request: Request) { } for (let i = 0; i < dataItem.length; i++) { + + let status = false + let numb = 1 + let name = dataItem[i].name + do { + const cekName = await prisma.divisionDocumentFolderFile.count({ + where: { + path: path, + isActive: true, + extension: dataItem[i].extension, + name + } + }) + + if (cekName > 0) { + name = dataItem[i].name + " (" + numb + ")" + numb++ + status = false + } else { + status = true + } + } while (status == false); + + const id = dataItem[i].id; - const cekFile = await prisma.divisionDocumentFolderFile.update({ + const update = await prisma.divisionDocumentFolderFile.update({ where: { - id: id + id }, data: { - path: path + path, + name } }) } @@ -45,4 +70,118 @@ export async function POST(request: Request) { console.log(error); return NextResponse.json({ success: false, message: "Gagal memindahkan item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); } +}; + + +// COPY ITEM +export async function PUT(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 { idDivision, path, dataItem } = (await request.json()); + + + if (path != "home") { + const cekPath = await prisma.divisionDocumentFolderFile.count({ + where: { + isActive: true, + id: path + } + }) + + if (cekPath == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan path, data tidak ditemukan" }, { status: 404 }); + } + } + + for (let i = 0; i < dataItem.length; i++) { + let name = dataItem[i].name; + const category = dataItem[i].category; + const extension = dataItem[i].extension; + + let status = false + let numb = 1 + do { + const cekName = await prisma.divisionDocumentFolderFile.count({ + where: { + path: path, + isActive: true, + extension, + name + } + }) + + if (cekName > 0) { + name = dataItem[i].name + " (" + numb + ")" + numb++ + status = false + } else { + status = true + } + } while (status == false); + + + const create = await prisma.divisionDocumentFolderFile.create({ + data: { + name, + path, + idDivision, + category, + extension, + createdBy: user.id + }, + select: { + id: true + } + }) + + // let newPath = create.id + // let idPath = dataItem[i].id; + // let statusCek = false + // do { + // const cekFolder = await prisma.divisionDocumentFolderFile.findMany({ + // where: { + // isActive: true, + // path: idPath + // } + // }) + + // if (cekFolder.length == 0) { + // statusCek = true + // } else { + // for (let index = 0; index < cekFolder.length; index++) { + // const addChildren = await prisma.divisionDocumentFolderFile.create({ + // data: { + // name: cekFolder[index].name, + // path: newPath, + // idDivision, + // category: cekFolder[index].category, + // extension: cekFolder[index].extension, + // createdBy: user.id + // }, + // select: { + // id: true + // } + // }) + + // newPath = create.id + // } + + + // } + + + // } while (statusCek == false); + + } + + + return NextResponse.json({ success: true, message: "Berhasil salin item" }, { status: 200 }); + } catch (error) { + console.log(error); + return NextResponse.json({ success: false, message: "Gagal salin 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 de4f8b5..93f419f 100644 --- a/src/module/document/lib/api_document.ts +++ b/src/module/document/lib/api_document.ts @@ -1,4 +1,4 @@ -import { IFormEditItem, IFormFolder, IFormMoreItem } from "./type_document"; +import { IFormEditItem, IFormFolder, IFormMoreCopyItem, IFormMoreItem } from "./type_document"; export const funGetAllDocument = async (path?: string) => { const response = await fetch(`/api/document${(path) ? path : ''}`, { next: { tags: ['document'] } }); @@ -54,4 +54,15 @@ export const funMoveDocument = async (data: IFormMoreItem) => { body: JSON.stringify(data), }); return await response.json().catch(() => null); +}; + +export const funCopyDocument = async (data: IFormMoreCopyItem) => { + const response = await fetch("/api/document/more", { + method: "PUT", + 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/lib/type_document.ts b/src/module/document/lib/type_document.ts index d5b06cd..97dcd7d 100644 --- a/src/module/document/lib/type_document.ts +++ b/src/module/document/lib/type_document.ts @@ -33,4 +33,10 @@ export interface IFormDetailMoreItem { export interface IFormMoreItem { path: string, dataItem: IFormDetailMoreItem[] +} + +export interface IFormMoreCopyItem { + idDivision: string, + path: string, + dataItem: IDataDocument[] } \ No newline at end of file diff --git a/src/module/document/ui/drawer_cut_documents.tsx b/src/module/document/ui/drawer_cut_documents.tsx index 69298ea..07e099c 100644 --- a/src/module/document/ui/drawer_cut_documents.tsx +++ b/src/module/document/ui/drawer_cut_documents.tsx @@ -12,7 +12,7 @@ import { IoMdFolder } from 'react-icons/io'; import { MdFolder } from 'react-icons/md'; -export default function DrawerCutDocuments({ onChoosePath, data }: { data: IFormDetailMoreItem[], onChoosePath: (val: string) => void }) { +export default function DrawerCutDocuments({ category, onChoosePath, data }: { category: string, data: IFormDetailMoreItem[], onChoosePath: (val: string) => void }) { const [opened, setOpened] = useState(false); const param = useParams<{ id: string }>() const [path, setPath] = useState('home') @@ -69,7 +69,13 @@ export default function DrawerCutDocuments({ onChoosePath, data }: { data: IForm - + diff --git a/src/module/document/ui/drawer_more.tsx b/src/module/document/ui/drawer_more.tsx index 6f35338..f27ecad 100644 --- a/src/module/document/ui/drawer_more.tsx +++ b/src/module/document/ui/drawer_more.tsx @@ -4,16 +4,18 @@ import React, { useState } from "react"; import { LuFolders, LuFolderSymlink } from "react-icons/lu"; import DrawerCutDocuments from "./drawer_cut_documents"; import DrawerCopyDocuments from "./drawer_copy_documents"; -import { IFormDetailMoreItem } from "../lib/type_document"; +import { IDataDocument, IFormDetailMoreItem } from "../lib/type_document"; import toast from "react-hot-toast"; -import { funMoveDocument } from "../lib/api_document"; +import { funCopyDocument, funMoveDocument } from "../lib/api_document"; import { useHookstate } from "@hookstate/core"; import { globalRefreshDocument } from "../lib/val_document"; +import { useParams } from "next/navigation"; -export default function DrawerMore({ data }: { data: IFormDetailMoreItem[] }) { +export default function DrawerMore({ data }: { data: IDataDocument[] }) { const [isCut, setIsCut] = useState(false) const [isCopy, setIsCopy] = useState(false) const refresh = useHookstate(globalRefreshDocument) + const param = useParams<{ id: string }>() async function onMoveItem(path: string) { @@ -33,6 +35,23 @@ export default function DrawerMore({ data }: { data: IFormDetailMoreItem[] }) { } + async function onCopyItem(path: string) { + try { + const res = await funCopyDocument({ idDivision: param.id, path, dataItem: data }) + if (res.success) { + toast.success(res.message) + refresh.set(true) + } else { + toast.error(res.message) + } + } catch (error) { + console.log(error) + toast.error("Gagal memindahkan item, coba lagi nanti") + } + setIsCopy(false) + } + + return ( @@ -61,11 +80,11 @@ export default function DrawerMore({ data }: { data: IFormDetailMoreItem[] }) { setIsCut(false)} title={'Pilih Lokasi Pemindahan'} size="lg"> - { onMoveItem(val) }} /> + { onMoveItem(val) }} category="move" /> setIsCopy(false)} title={'Pilih Lokasi Salin'} size="lg"> - + { onCopyItem(val) }} category="copy" /> ); diff --git a/src/module/document/ui/navbar_document_division.tsx b/src/module/document/ui/navbar_document_division.tsx index 97b5e96..f11f688 100644 --- a/src/module/document/ui/navbar_document_division.tsx +++ b/src/module/document/ui/navbar_document_division.tsx @@ -57,7 +57,8 @@ export default function NavbarDocumentDivision() { id: dataDocument[index].id, name: dataDocument[index].name, path: dataDocument[index].path, - extension: dataDocument[index].extension + extension: dataDocument[index].extension, + category: dataDocument[index].category, } ]) } @@ -81,7 +82,8 @@ export default function NavbarDocumentDivision() { id: dataDocument[index].id, name: dataDocument[index].name, path: dataDocument[index].path, - extension: dataDocument[index].extension + extension: dataDocument[index].extension, + category: dataDocument[index].category, } setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr]) }