: document

Deskripsi:
- copy item

nb : baru setengah selesai, aku nyerah aja lahh

nanti aja pikirin nya

No Issues
This commit is contained in:
amel
2024-08-22 14:30:46 +08:00
parent 92a4c06bff
commit 3d513c0d08
6 changed files with 196 additions and 13 deletions

View File

@@ -28,13 +28,38 @@ export async function POST(request: Request) {
} }
for (let i = 0; i < dataItem.length; i++) { for (let i = 0; i < dataItem.length; i++) {
const id = dataItem[i].id;
const cekFile = await prisma.divisionDocumentFolderFile.update({ let status = false
let numb = 1
let name = dataItem[i].name
do {
const cekName = await prisma.divisionDocumentFolderFile.count({
where: { where: {
id: id 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 update = await prisma.divisionDocumentFolderFile.update({
where: {
id
}, },
data: { data: {
path: path path,
name
} }
}) })
} }
@@ -46,3 +71,117 @@ export async function POST(request: Request) {
return NextResponse.json({ success: false, message: "Gagal memindahkan item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); 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 });
}
};

View File

@@ -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) => { export const funGetAllDocument = async (path?: string) => {
const response = await fetch(`/api/document${(path) ? path : ''}`, { next: { tags: ['document'] } }); const response = await fetch(`/api/document${(path) ? path : ''}`, { next: { tags: ['document'] } });
@@ -55,3 +55,14 @@ export const funMoveDocument = async (data: IFormMoreItem) => {
}); });
return await response.json().catch(() => null); 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);
};

View File

@@ -34,3 +34,9 @@ export interface IFormMoreItem {
path: string, path: string,
dataItem: IFormDetailMoreItem[] dataItem: IFormDetailMoreItem[]
} }
export interface IFormMoreCopyItem {
idDivision: string,
path: string,
dataItem: IDataDocument[]
}

View File

@@ -12,7 +12,7 @@ import { IoMdFolder } from 'react-icons/io';
import { MdFolder } from 'react-icons/md'; 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 [opened, setOpened] = useState(false);
const param = useParams<{ id: string }>() const param = useParams<{ id: string }>()
const [path, setPath] = useState('home') const [path, setPath] = useState('home')
@@ -69,7 +69,13 @@ export default function DrawerCutDocuments({ onChoosePath, data }: { data: IForm
<Button variant="subtle" fullWidth color={WARNA.biruTua} radius={"xl"} onClick={() => setOpened(true)}>BUAT FOLDER BARU</Button> <Button variant="subtle" fullWidth color={WARNA.biruTua} radius={"xl"} onClick={() => setOpened(true)}>BUAT FOLDER BARU</Button>
</Grid.Col> </Grid.Col>
<Grid.Col span={6}> <Grid.Col span={6}>
<Button variant="filled" fullWidth color={WARNA.biruTua} radius={"xl"} onClick={() => onChoosePath(path)}>PINDAH</Button> <Button variant="filled" fullWidth color={WARNA.biruTua} radius={"xl"} onClick={() => onChoosePath(path)}>
{
(category == "move") ?
"PINDAH" : "SALIN"
}
</Button>
</Grid.Col> </Grid.Col>
</Grid> </Grid>
</Box> </Box>

View File

@@ -4,16 +4,18 @@ import React, { useState } from "react";
import { LuFolders, LuFolderSymlink } from "react-icons/lu"; import { LuFolders, LuFolderSymlink } from "react-icons/lu";
import DrawerCutDocuments from "./drawer_cut_documents"; import DrawerCutDocuments from "./drawer_cut_documents";
import DrawerCopyDocuments from "./drawer_copy_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 toast from "react-hot-toast";
import { funMoveDocument } from "../lib/api_document"; import { funCopyDocument, funMoveDocument } from "../lib/api_document";
import { useHookstate } from "@hookstate/core"; import { useHookstate } from "@hookstate/core";
import { globalRefreshDocument } from "../lib/val_document"; 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 [isCut, setIsCut] = useState(false)
const [isCopy, setIsCopy] = useState(false) const [isCopy, setIsCopy] = useState(false)
const refresh = useHookstate(globalRefreshDocument) const refresh = useHookstate(globalRefreshDocument)
const param = useParams<{ id: string }>()
async function onMoveItem(path: 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 ( return (
<Box> <Box>
@@ -61,11 +80,11 @@ export default function DrawerMore({ data }: { data: IFormDetailMoreItem[] }) {
<LayoutDrawer opened={isCut} onClose={() => setIsCut(false)} title={'Pilih Lokasi Pemindahan'} size="lg"> <LayoutDrawer opened={isCut} onClose={() => setIsCut(false)} title={'Pilih Lokasi Pemindahan'} size="lg">
<DrawerCutDocuments data={data} onChoosePath={(val) => { onMoveItem(val) }} /> <DrawerCutDocuments data={data} onChoosePath={(val) => { onMoveItem(val) }} category="move" />
</LayoutDrawer> </LayoutDrawer>
<LayoutDrawer opened={isCopy} onClose={() => setIsCopy(false)} title={'Pilih Lokasi Salin'} size="lg"> <LayoutDrawer opened={isCopy} onClose={() => setIsCopy(false)} title={'Pilih Lokasi Salin'} size="lg">
<DrawerCopyDocuments /> <DrawerCutDocuments data={data} onChoosePath={(val) => { onCopyItem(val) }} category="copy" />
</LayoutDrawer> </LayoutDrawer>
</Box> </Box>
); );

View File

@@ -57,7 +57,8 @@ export default function NavbarDocumentDivision() {
id: dataDocument[index].id, id: dataDocument[index].id,
name: dataDocument[index].name, name: dataDocument[index].name,
path: dataDocument[index].path, 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, id: dataDocument[index].id,
name: dataDocument[index].name, name: dataDocument[index].name,
path: dataDocument[index].path, path: dataDocument[index].path,
extension: dataDocument[index].extension extension: dataDocument[index].extension,
category: dataDocument[index].category,
} }
setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr]) setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr])
} }