upd: task file
Deskripsi: - hapus file task divisi No Issues
This commit is contained in:
71
src/app/api/task/file/[id]/route.ts
Normal file
71
src/app/api/task/file/[id]/route.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import { prisma } from "@/module/_global";
|
||||||
|
import { funGetUserByCookies } from "@/module/auth";
|
||||||
|
import _ from "lodash";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
// HAPUS DETAIL FILE, HAPUS FILE DI ASSETS DAN DATABASE (BUKAN PAKE ISACTIVE)
|
||||||
|
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const user = await funGetUserByCookies()
|
||||||
|
if (user.id == undefined) {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||||
|
}
|
||||||
|
const { id } = context.params;
|
||||||
|
const data = await prisma.divisionProjectFile.count({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data == 0) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
message: "Hapus file gagal, data tidak ditemukan",
|
||||||
|
},
|
||||||
|
{ status: 404 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataRelasi = await prisma.divisionProjectFile.findUnique({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const dataFile = await prisma.containerFileDivision.findUnique({
|
||||||
|
where: {
|
||||||
|
id: dataRelasi?.idFile
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
fs.unlink(`./public/file/task/${dataFile?.id}.${dataFile?.extension}`, (err) => { })
|
||||||
|
|
||||||
|
const deleteRelasi = await prisma.divisionProjectFile.delete({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const deleteFile = await prisma.containerFileDivision.delete({
|
||||||
|
where: {
|
||||||
|
id: dataRelasi?.idFile,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: "File berhasil dihapus",
|
||||||
|
data,
|
||||||
|
},
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal menghapus file, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -120,3 +120,13 @@ export const funEditTask = async (path: string, data: { title: string }) => {
|
|||||||
});
|
});
|
||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const funDeleteFileTask = async (path: string) => {
|
||||||
|
const response = await fetch(`/api/task/file/${path}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return await response.json().catch(() => null);
|
||||||
|
};
|
||||||
@@ -287,7 +287,7 @@ export default function CreateTask() {
|
|||||||
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) => {
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import { SkeletonDetailListTugasTask, WARNA } from "@/module/_global";
|
import { LayoutDrawer, SkeletonDetailListTugasTask, WARNA } from "@/module/_global";
|
||||||
import { Box, Group, Skeleton, Text } from "@mantine/core";
|
import { Box, Flex, Group, SimpleGrid, Skeleton, Stack, Text } from "@mantine/core";
|
||||||
import { useShallowEffect } from "@mantine/hooks";
|
import { useShallowEffect } from "@mantine/hooks";
|
||||||
import { useParams } from "next/navigation";
|
import { useParams } from "next/navigation";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { BsFiletypeCsv, BsFiletypeHeic, BsFiletypeJpg, BsFiletypePdf, BsFiletypePng } from "react-icons/bs";
|
import { BsFileTextFill, BsFiletypeCsv, BsFiletypeHeic, BsFiletypeJpg, BsFiletypePdf, BsFiletypePng } from "react-icons/bs";
|
||||||
import { funGetTaskDivisionById } from "../lib/api_task";
|
import { funDeleteFileTask, funGetTaskDivisionById } from "../lib/api_task";
|
||||||
import { IDataFileTaskDivision } from "../lib/type_task";
|
import { IDataFileTaskDivision } from "../lib/type_task";
|
||||||
|
import { FaTrash } from "react-icons/fa6";
|
||||||
|
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||||
|
|
||||||
export default function ListFileDetailTask() {
|
export default function ListFileDetailTask() {
|
||||||
const [isData, setData] = useState<IDataFileTaskDivision[]>([])
|
const [isData, setData] = useState<IDataFileTaskDivision[]>([])
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const param = useParams<{ id: string, detail: string }>()
|
const param = useParams<{ id: string, detail: string }>()
|
||||||
|
const [openDrawer, setOpenDrawer] = useState(false)
|
||||||
|
const [isOpenModal, setOpenModal] = useState(false)
|
||||||
|
const [idData, setIdData] = useState('')
|
||||||
|
const [nameData, setNameData] = useState('')
|
||||||
async function getOneData() {
|
async function getOneData() {
|
||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
@@ -34,6 +40,25 @@ export default function ListFileDetailTask() {
|
|||||||
getOneData();
|
getOneData();
|
||||||
}, [param.detail])
|
}, [param.detail])
|
||||||
|
|
||||||
|
|
||||||
|
async function onDelete() {
|
||||||
|
try {
|
||||||
|
const res = await funDeleteFileTask(idData);
|
||||||
|
if (res.success) {
|
||||||
|
toast.success(res.message)
|
||||||
|
getOneData()
|
||||||
|
setIdData("")
|
||||||
|
setOpenDrawer(false)
|
||||||
|
} else {
|
||||||
|
toast.error(res.message);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
toast.error("Gagal menghapus file, coba lagi nanti");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box pt={20}>
|
<Box pt={20}>
|
||||||
<Text fw={'bold'} c={WARNA.biruTua}>File</Text>
|
<Text fw={'bold'} c={WARNA.biruTua}>File</Text>
|
||||||
@@ -67,6 +92,12 @@ export default function ListFileDetailTask() {
|
|||||||
padding: 10
|
padding: 10
|
||||||
}}
|
}}
|
||||||
mb={10}
|
mb={10}
|
||||||
|
|
||||||
|
onClick={() => {
|
||||||
|
setNameData(item.name + '.' + item.extension)
|
||||||
|
setIdData(item.id)
|
||||||
|
setOpenDrawer(true)
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Group>
|
<Group>
|
||||||
{item.extension == "pdf" && <BsFiletypePdf size={25} />}
|
{item.extension == "pdf" && <BsFiletypePdf size={25} />}
|
||||||
@@ -74,13 +105,53 @@ export default function ListFileDetailTask() {
|
|||||||
{item.extension == "png" && <BsFiletypePng size={25} />}
|
{item.extension == "png" && <BsFiletypePng size={25} />}
|
||||||
{item.extension == "jpg" || item.extension == "jpeg" && <BsFiletypeJpg size={25} />}
|
{item.extension == "jpg" || item.extension == "jpeg" && <BsFiletypeJpg size={25} />}
|
||||||
{item.extension == "heic" && <BsFiletypeHeic size={25} />}
|
{item.extension == "heic" && <BsFiletypeHeic size={25} />}
|
||||||
<Text>{item.name}</Text>
|
<Text>{item.name + '.' + item.extension}</Text>
|
||||||
</Group>
|
</Group>
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<LayoutDrawer opened={openDrawer} title={nameData} onClose={() => setOpenDrawer(false)}>
|
||||||
|
<Box>
|
||||||
|
<Stack pt={10}>
|
||||||
|
<SimpleGrid
|
||||||
|
cols={{ base: 3, sm: 3, lg: 3 }}
|
||||||
|
>
|
||||||
|
<Flex onClick={() => { }} justify={'center'} align={'center'} direction={'column'} >
|
||||||
|
<Box>
|
||||||
|
<BsFileTextFill size={30} color={WARNA.biruTua} />
|
||||||
|
</Box>
|
||||||
|
<Box>
|
||||||
|
<Text c={WARNA.biruTua}>Lihat file</Text>
|
||||||
|
</Box>
|
||||||
|
</Flex>
|
||||||
|
|
||||||
|
<Flex onClick={() => { setOpenModal(true) }} justify={'center'} align={'center'} direction={'column'} >
|
||||||
|
<Box>
|
||||||
|
<FaTrash size={30} color={WARNA.biruTua} />
|
||||||
|
</Box>
|
||||||
|
<Box>
|
||||||
|
<Text c={WARNA.biruTua}>Hapus file</Text>
|
||||||
|
</Box>
|
||||||
|
</Flex>
|
||||||
|
</SimpleGrid>
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
</LayoutDrawer>
|
||||||
|
|
||||||
|
|
||||||
|
<LayoutModal opened={isOpenModal} onClose={() => setOpenModal(false)}
|
||||||
|
description="Apakah Anda yakin ingin menghapus file ini? File yang dihapus tidak dapat dikembalikan"
|
||||||
|
onYes={(val) => {
|
||||||
|
if (val) {
|
||||||
|
onDelete()
|
||||||
|
}
|
||||||
|
setOpenModal(false)
|
||||||
|
}} />
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user