upd: project
Deskripsi: - upload file pada project No Issues
This commit is contained in:
@@ -3,8 +3,8 @@ import React from "react";
|
|||||||
|
|
||||||
function Page({ searchParams }: { searchParams: any }) {
|
function Page({ searchParams }: { searchParams: any }) {
|
||||||
|
|
||||||
if (searchParams.page == "file-save")
|
// if (searchParams.page == "file-save")
|
||||||
return <ViewFileSave kategori="project" />
|
// return <ViewFileSave kategori="project" />
|
||||||
|
|
||||||
return <CreateProject />;
|
return <CreateProject />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ import { funGetUserByCookies } from "@/module/auth";
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import path from "path";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
|
||||||
// GET ALL DATA PROJECT
|
// 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 });
|
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
|
const userId = user.id
|
||||||
|
|
||||||
|
|
||||||
@@ -112,7 +117,7 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
if (member.length > 0) {
|
if (member.length > 0) {
|
||||||
const dataMember = member.map((v: any) => ({
|
const dataMember = member.map((v: any) => ({
|
||||||
..._.omit(v, ["idUser", "name"]),
|
..._.omit(v, ["idUser", "name", "img"]),
|
||||||
idProject: data.id,
|
idProject: data.id,
|
||||||
idUser: v.idUser,
|
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 = {
|
const insertToTable = await prisma.projectFile.create({
|
||||||
name: fName,
|
data: {
|
||||||
extension: fExt,
|
idProject: data.id,
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
a++
|
||||||
fileFix.push(dataFile)
|
}
|
||||||
})
|
|
||||||
|
|
||||||
const insertFile = await prisma.divisionProjectFile.createMany({
|
|
||||||
data: fileFix
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,16 +6,10 @@ export const funGetAllProject = async (path?: string) => {
|
|||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const funCreateProject = async (data: IFormProject) => {
|
export const funCreateProject = async (data: FormData) => {
|
||||||
if (data.title.length < 3)
|
|
||||||
return { success: false, message: 'Nama Kegiatan minimal 3 karakter' }
|
|
||||||
|
|
||||||
const response = await fetch(`/api/project`, {
|
const response = await fetch(`/api/project`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
body: data,
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
});
|
});
|
||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export default function CreateProject() {
|
|||||||
const [openTugas, setOpenTugas] = useState(false)
|
const [openTugas, setOpenTugas] = useState(false)
|
||||||
const [dataTask, setDataTask] = useState<IFormDateProject[]>([])
|
const [dataTask, setDataTask] = useState<IFormDateProject[]>([])
|
||||||
const openRef = useRef<() => void>(null)
|
const openRef = useRef<() => void>(null)
|
||||||
const [fileForm, setFileForm] = useState<FormData[]>([])
|
const [fileForm, setFileForm] = useState<any[]>([])
|
||||||
const [listFile, setListFile] = useState<IListFileTaskProject[]>([])
|
const [listFile, setListFile] = useState<IListFileTaskProject[]>([])
|
||||||
const [indexDelFile, setIndexDelFile] = useState<number>(0)
|
const [indexDelFile, setIndexDelFile] = useState<number>(0)
|
||||||
const [indexDelTask, setIndexDelTask] = useState<number>(0)
|
const [indexDelTask, setIndexDelTask] = useState<number>(0)
|
||||||
@@ -91,16 +91,22 @@ export default function CreateProject() {
|
|||||||
|
|
||||||
async function onSubmit() {
|
async function onSubmit() {
|
||||||
try {
|
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) {
|
if (response.success) {
|
||||||
toast.success(response.message)
|
toast.success(response.message)
|
||||||
// setBody({
|
|
||||||
// idGroup: "",
|
|
||||||
// title: "",
|
|
||||||
// desc: "",
|
|
||||||
// })
|
|
||||||
|
|
||||||
member.set([])
|
member.set([])
|
||||||
setFileForm([])
|
setFileForm([])
|
||||||
setListFile([])
|
setListFile([])
|
||||||
@@ -111,7 +117,7 @@ export default function CreateProject() {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(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}
|
key={i}
|
||||||
>
|
>
|
||||||
<Group>
|
<Group>
|
||||||
<Avatar src={"v.image"} alt="it's me" size="lg" />
|
<Avatar src={`/api/file/img?cat=user&file=${v.img}`} alt="it's me" size="lg" />
|
||||||
<Box>
|
<Box>
|
||||||
<Text c={WARNA.biruTua} fw={"bold"}>
|
<Text c={WARNA.biruTua} fw={"bold"}>
|
||||||
{v.name}
|
{v.name}
|
||||||
@@ -342,15 +348,13 @@ export default function CreateProject() {
|
|||||||
onClose={() => setOpenDrawer(false)}
|
onClose={() => setOpenDrawer(false)}
|
||||||
title={"Pilih File"}
|
title={"Pilih File"}
|
||||||
>
|
>
|
||||||
<Flex justify={"space-around"}>
|
<Flex justify={"flex-start"} px={20}>
|
||||||
<Dropzone
|
<Dropzone
|
||||||
openRef={openRef}
|
openRef={openRef}
|
||||||
onDrop={async (files) => {
|
onDrop={async (files) => {
|
||||||
if (!files || _.isEmpty(files))
|
if (!files || _.isEmpty(files))
|
||||||
return toast.error('Tidak ada file yang dipilih')
|
return toast.error('Tidak ada file yang dipilih')
|
||||||
const fd = new FormData();
|
setFileForm([...fileForm, files[0]])
|
||||||
fd.append("file", files[0]);
|
|
||||||
setFileForm([...fileForm, fd])
|
|
||||||
setListFile([...listFile, { name: files[0].name, extension: files[0].type.split("/")[1] }])
|
setListFile([...listFile, { name: files[0].name, extension: files[0].type.split("/")[1] }])
|
||||||
}}
|
}}
|
||||||
activateOnClick={false}
|
activateOnClick={false}
|
||||||
@@ -379,7 +383,7 @@ export default function CreateProject() {
|
|||||||
<Text ta={"center"}>diperangkat</Text>
|
<Text ta={"center"}>diperangkat</Text>
|
||||||
</Box>
|
</Box>
|
||||||
</Dropzone>
|
</Dropzone>
|
||||||
<Box onClick={() => router.push("/project/create?page=file-save")}>
|
{/* <Box onClick={() => router.push("/project/create?page=file-save")}>
|
||||||
<Box
|
<Box
|
||||||
bg={"#DCEED8"}
|
bg={"#DCEED8"}
|
||||||
style={{
|
style={{
|
||||||
@@ -396,7 +400,7 @@ export default function CreateProject() {
|
|||||||
Pilih file yang
|
Pilih file yang
|
||||||
</Text>
|
</Text>
|
||||||
<Text ta={"center"}>sudah ada</Text>
|
<Text ta={"center"}>sudah ada</Text>
|
||||||
</Box>
|
</Box> */}
|
||||||
</Flex>
|
</Flex>
|
||||||
</LayoutDrawer>
|
</LayoutDrawer>
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export default function CreateUsersProject({ grup, onClose }: { grup?: string, o
|
|||||||
if (selectedFiles.some((i: any) => i.idUser == dataMember[index].id)) {
|
if (selectedFiles.some((i: any) => i.idUser == dataMember[index].id)) {
|
||||||
setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != dataMember[index].id))
|
setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != dataMember[index].id))
|
||||||
} else {
|
} 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)}
|
onClick={() => handleFileClick(index)}
|
||||||
>
|
>
|
||||||
<Center>
|
<Center>
|
||||||
<Avatar src={"https://i.pravatar.cc/1000?img=37"} alt="it's me" size="xl" />
|
<Avatar src={`/api/file/img?cat=user&file=${v.img}`} alt="it's me" size="xl" />
|
||||||
</Center>
|
</Center>
|
||||||
<Text mt={20} ta="center">
|
<Text mt={20} ta="center">
|
||||||
{v.name}
|
{v.name}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export type TypeUser = {
|
|||||||
phone: string
|
phone: string
|
||||||
email: string
|
email: string
|
||||||
gender: string
|
gender: string
|
||||||
|
img:string
|
||||||
isActive: boolean,
|
isActive: boolean,
|
||||||
group: string,
|
group: string,
|
||||||
position: string
|
position: string
|
||||||
|
|||||||
Reference in New Issue
Block a user