From 9ee3e0a2ffe93586f118dae9d32a7d608e593619 Mon Sep 17 00:00:00 2001 From: amel Date: Mon, 2 Sep 2024 14:39:52 +0800 Subject: [PATCH] upd: project Deskripsi: - upload file pada project No Issues --- src/app/(application)/project/create/page.tsx | 4 +- src/app/api/project/route.ts | 56 ++++++++++++------- src/module/project/lib/api_project.ts | 10 +--- src/module/project/ui/create_project.tsx | 36 ++++++------ .../project/ui/create_users_project.tsx | 4 +- src/module/user/lib/type_user.ts | 1 + 6 files changed, 62 insertions(+), 49 deletions(-) diff --git a/src/app/(application)/project/create/page.tsx b/src/app/(application)/project/create/page.tsx index 3aac089..e571503 100644 --- a/src/app/(application)/project/create/page.tsx +++ b/src/app/(application)/project/create/page.tsx @@ -3,8 +3,8 @@ import React from "react"; function Page({ searchParams }: { searchParams: any }) { - if (searchParams.page == "file-save") - return + // if (searchParams.page == "file-save") + // return return ; } diff --git a/src/app/api/project/route.ts b/src/app/api/project/route.ts index 75a7af2..6eb8346 100644 --- a/src/app/api/project/route.ts +++ b/src/app/api/project/route.ts @@ -3,7 +3,8 @@ import { funGetUserByCookies } from "@/module/auth"; import _ from "lodash"; import moment from "moment"; import { NextResponse } from "next/server"; - +import path from "path"; +import fs from "fs"; // GET ALL DATA PROJECT @@ -80,7 +81,11 @@ export async function POST(request: Request) { return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); } - const { idGroup, title, task, member, file } = (await request.json()) + const body = await request.formData() + const dataBody = body.get("data") + const cekFile = body.has("file0") + + const { idGroup, title, task, member } = JSON.parse(dataBody as string) const userId = user.id @@ -112,7 +117,7 @@ export async function POST(request: Request) { if (member.length > 0) { const dataMember = member.map((v: any) => ({ - ..._.omit(v, ["idUser", "name"]), + ..._.omit(v, ["idUser", "name", "img"]), idProject: data.id, idUser: v.idUser, })) @@ -122,27 +127,36 @@ export async function POST(request: Request) { }) } - let fileFix: any[] = [] + if (cekFile) { + let a = 0 + const root = path.join(process.cwd(), "./public/file/project/"); + for (var pair of body.entries()) { + if (String(pair[0]) == "file" + a) { + const file = body.get(pair[0]) as File + const fExt = file.name.split(".").pop() + const fName = file.name.replace("." + fExt, "") - if (file.length > 0) { - file.map((v: any, index: any) => { - const f: any = file[index].get('file') - const fName = f.name - const fExt = fName.split(".").pop() - const dataFile = { - name: fName, - extension: fExt, - idProject: data.id, + const insertToTable = await prisma.projectFile.create({ + data: { + idProject: data.id, + name: fName, + extension: String(fExt) + }, + select: { + id: true + } + }) + + const nameFix = insertToTable.id + '.' + fExt + const filePath = path.join(root, nameFix) + // Konversi ArrayBuffer ke Buffer + const buffer = Buffer.from(await file.arrayBuffer()); + // Tulis file ke sistem + fs.writeFileSync(filePath, buffer); } - - fileFix.push(dataFile) - }) - - const insertFile = await prisma.divisionProjectFile.createMany({ - data: fileFix - }) - + a++ + } } diff --git a/src/module/project/lib/api_project.ts b/src/module/project/lib/api_project.ts index f02bea5..8f82bf1 100644 --- a/src/module/project/lib/api_project.ts +++ b/src/module/project/lib/api_project.ts @@ -6,16 +6,10 @@ export const funGetAllProject = async (path?: string) => { return await response.json().catch(() => null); } -export const funCreateProject = async (data: IFormProject) => { - if (data.title.length < 3) - return { success: false, message: 'Nama Kegiatan minimal 3 karakter' } - +export const funCreateProject = async (data: FormData) => { const response = await fetch(`/api/project`, { method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(data), + body: data, }); return await response.json().catch(() => null); } diff --git a/src/module/project/ui/create_project.tsx b/src/module/project/ui/create_project.tsx index bd4ad38..a40ab93 100644 --- a/src/module/project/ui/create_project.tsx +++ b/src/module/project/ui/create_project.tsx @@ -36,7 +36,7 @@ export default function CreateProject() { const [openTugas, setOpenTugas] = useState(false) const [dataTask, setDataTask] = useState([]) const openRef = useRef<() => void>(null) - const [fileForm, setFileForm] = useState([]) + const [fileForm, setFileForm] = useState([]) const [listFile, setListFile] = useState([]) const [indexDelFile, setIndexDelFile] = useState(0) const [indexDelTask, setIndexDelTask] = useState(0) @@ -91,16 +91,22 @@ export default function CreateProject() { async function onSubmit() { try { - const response = await funCreateProject({ title: body.title, idGroup: body.idGroup, task: dataTask, file: fileForm, member: memberValue }) + const fd = new FormData(); + for (let i = 0; i < fileForm.length; i++) { + fd.append(`file${i}`, fileForm[i]); + } + + fd.append("data", JSON.stringify({ + title: body.title, + idGroup: body.idGroup, + task: dataTask, + member: memberValue + })) + + const response = await funCreateProject(fd) if (response.success) { toast.success(response.message) - // setBody({ - // idGroup: "", - // title: "", - // desc: "", - // }) - member.set([]) setFileForm([]) setListFile([]) @@ -111,7 +117,7 @@ export default function CreateProject() { } } catch (error) { console.log(error) - toast.error("Gagal menambahkan tugas divisi, coba lagi nanti"); + toast.error("Gagal menambahkan kegiatan, coba lagi nanti"); } } @@ -289,7 +295,7 @@ export default function CreateProject() { key={i} > - + {v.name} @@ -342,15 +348,13 @@ export default function CreateProject() { onClose={() => setOpenDrawer(false)} title={"Pilih File"} > - + { if (!files || _.isEmpty(files)) return toast.error('Tidak ada file yang dipilih') - const fd = new FormData(); - fd.append("file", files[0]); - setFileForm([...fileForm, fd]) + setFileForm([...fileForm, files[0]]) setListFile([...listFile, { name: files[0].name, extension: files[0].type.split("/")[1] }]) }} activateOnClick={false} @@ -379,7 +383,7 @@ export default function CreateProject() { diperangkat - router.push("/project/create?page=file-save")}> + {/* router.push("/project/create?page=file-save")}> sudah ada - + */} diff --git a/src/module/project/ui/create_users_project.tsx b/src/module/project/ui/create_users_project.tsx index 32bf50d..b673d9d 100644 --- a/src/module/project/ui/create_users_project.tsx +++ b/src/module/project/ui/create_users_project.tsx @@ -24,7 +24,7 @@ export default function CreateUsersProject({ grup, onClose }: { grup?: string, o if (selectedFiles.some((i: any) => i.idUser == dataMember[index].id)) { setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != dataMember[index].id)) } else { - setSelectedFiles([...selectedFiles, { idUser: dataMember[index].id, name: dataMember[index].name }]) + setSelectedFiles([...selectedFiles, { idUser: dataMember[index].id, name: dataMember[index].name, img: dataMember[index].img }]) } }; @@ -114,7 +114,7 @@ export default function CreateUsersProject({ grup, onClose }: { grup?: string, o onClick={() => handleFileClick(index)} >
- +
{v.name} diff --git a/src/module/user/lib/type_user.ts b/src/module/user/lib/type_user.ts index 0b94cec..d9f1081 100644 --- a/src/module/user/lib/type_user.ts +++ b/src/module/user/lib/type_user.ts @@ -5,6 +5,7 @@ export type TypeUser = { phone: string email: string gender: string + img:string isActive: boolean, group: string, position: string