upd
: document Deskripsi: - copy item nb : baru setengah selesai, aku nyerah aja lahh nanti aja pikirin nya No Issues
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
};
|
||||
@@ -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);
|
||||
};
|
||||
@@ -33,4 +33,10 @@ export interface IFormDetailMoreItem {
|
||||
export interface IFormMoreItem {
|
||||
path: string,
|
||||
dataItem: IFormDetailMoreItem[]
|
||||
}
|
||||
|
||||
export interface IFormMoreCopyItem {
|
||||
idDivision: string,
|
||||
path: string,
|
||||
dataItem: IDataDocument[]
|
||||
}
|
||||
@@ -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
|
||||
<Button variant="subtle" fullWidth color={WARNA.biruTua} radius={"xl"} onClick={() => setOpened(true)}>BUAT FOLDER BARU</Button>
|
||||
</Grid.Col>
|
||||
<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>
|
||||
</Box>
|
||||
|
||||
@@ -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 (
|
||||
<Box>
|
||||
@@ -61,11 +80,11 @@ export default function DrawerMore({ data }: { data: IFormDetailMoreItem[] }) {
|
||||
|
||||
|
||||
<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 opened={isCopy} onClose={() => setIsCopy(false)} title={'Pilih Lokasi Salin'} size="lg">
|
||||
<DrawerCopyDocuments />
|
||||
<DrawerCutDocuments data={data} onChoosePath={(val) => { onCopyItem(val) }} category="copy" />
|
||||
</LayoutDrawer>
|
||||
</Box>
|
||||
);
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user