Merge pull request #227 from bipproduction/amalia/13-september-24

Amalia/13 september 24
This commit is contained in:
Amalia
2024-09-13 12:01:02 +08:00
committed by GitHub
6 changed files with 138 additions and 87 deletions

View File

@@ -0,0 +1,22 @@
export async function funCopyFile({ fileId, dirId }: { fileId: string, dirId: string }) {
try {
const res = await fetch(`https://wibu-storage.wibudev.com/api/files/copy/${dirId}/${dirId}`, {
method: "POST",
body: JSON.stringify({ fileId: fileId }),
headers: {
Authorization: `Bearer ${process.env.WS_APIKEY}`
}
});
if (res.ok) {
const hasil = await res.json()
return { success: true, data: hasil.data }
} else {
const errorText = await res.json();
return { success: false, data: {} }
}
} catch (error) {
console.error("Copy error:", error);
return { success: false, data: {} }
}
}

View File

@@ -6,6 +6,7 @@ import SkeletonDetailListTugasTask from "./components/skeleton_detail_list_tugas
import SkeletonDetailProfile from "./components/skeleton_detail_profile";
import SkeletonSingle from "./components/skeleton_single";
import WrapLayout from "./components/wrap_layout";
import { funCopyFile } from "./fun/copy_file";
import { funDeleteFile } from "./fun/delete_file";
import { funUploadFile } from "./fun/upload_file";
import { WARNA } from "./fun/WARNA";
@@ -42,3 +43,4 @@ export { funUploadFile }
export { funDeleteFile }
export { DIR }
export { TEMA }
export { funCopyFile }

View File

@@ -7,9 +7,13 @@ import { cookies } from "next/headers"
export default async function funDetectCookies() {
const cookiesnya = cookies()
const c = cookiesnya.get("sessionCookieSDM")
if (!c || _.isUndefined(c) || !c.value || _.isEmpty(c.value)) return false
if (!c || _.isUndefined(c) || !c.value || _.isEmpty(c.value)) {
return false
}
const dataCookies = await unsealData(c!.value, { password: pwd_key_config as string })
if (_.isEmpty(_.toString(dataCookies))) return false
if (_.isEmpty(dataCookies)) {
return false
}
return true
}

View File

@@ -9,12 +9,14 @@ import { funCopyDocument, funMoveDocument } from "../lib/api_document";
import { useHookstate } from "@hookstate/core";
import { globalRefreshDocument } from "../lib/val_document";
import { useParams } from "next/navigation";
import { useShallowEffect } from "@mantine/hooks";
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 }>()
const [forbidCopy, setForbidCopy] = useState(true)
async function onMoveItem(path: string) {
@@ -51,6 +53,16 @@ export default function DrawerMore({ data }: { data: IDataDocument[] }) {
}
function cekFileSelected() {
const cek = data.some((i: any) => i.category == "FOLDER")
setForbidCopy(cek)
}
useShallowEffect(() => {
cekFileSelected()
}, [data])
return (
<Box>
@@ -66,14 +78,17 @@ export default function DrawerMore({ data }: { data: IDataDocument[] }) {
<Text c={WARNA.biruTua}>Pindah</Text>
</Box>
</Flex>
<Flex onClick={() => setIsCopy(true)} justify={'center'} align={'center'} direction={'column'} >
<Box>
<LuFolders size={30} color={WARNA.biruTua} />
</Box>
<Box>
<Text c={WARNA.biruTua}>Salin</Text>
</Box>
</Flex>
{
(!forbidCopy) &&
<Flex onClick={() => setIsCopy(true)} justify={'center'} align={'center'} direction={'column'} >
<Box>
<LuFolders size={30} color={WARNA.biruTua} />
</Box>
<Box>
<Text c={WARNA.biruTua}>Salin</Text>
</Box>
</Flex>
}
</SimpleGrid>
</Stack>

View File

@@ -38,6 +38,7 @@ export default function NavbarDocumentDivision() {
const [share, setShare] = useState(false)
const [more, setMore] = useState(false)
const [shareSelected, setShareSelected] = useState(false)
const [copyAllowed, setCopyAllowed] = useState(true)
const searchParams = useSearchParams()
const path = searchParams.get('path')
const [dataDocument, setDataDocument] = useState<IDataDocument[]>([])
@@ -46,7 +47,7 @@ export default function NavbarDocumentDivision() {
const [selectedFiles, setSelectedFiles] = useState<any>([])
const [selectAll, setSelectAll] = useState(false)
const [dariSelectAll, setDariSelectAll] = useState(false)
const isMobile = useMediaQuery('(max-width: 369px)');
const isMobile = useMediaQuery('(max-width: 369px)');
const [bodyRename, setBodyRename] = useState({
id: '',
name: '',
@@ -69,6 +70,7 @@ export default function NavbarDocumentDivision() {
extension: dataDocument[index].extension,
category: dataDocument[index].category,
share: dataDocument[index].share,
idStorage: dataDocument[index].idStorage
}
])
}
@@ -88,6 +90,13 @@ export default function NavbarDocumentDivision() {
} else {
setShareSelected(false)
}
const cek = selectedFiles.some((i: any) => i?.category == 'FOLDER')
if (cek || shareSelected || selectedFiles.length > 1) {
setCopyAllowed(false)
} else {
setCopyAllowed(true)
}
}
const handleSelectAll = () => {
@@ -102,6 +111,7 @@ export default function NavbarDocumentDivision() {
extension: dataDocument[index].extension,
category: dataDocument[index].category,
share: dataDocument[index].share,
idStorage: dataDocument[index].idStorage
}
setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr])
}
@@ -216,6 +226,28 @@ export default function NavbarDocumentDivision() {
setRename(true)
}
const onDownload = async () => {
try {
const fileUrl = `https://wibu-storage.wibudev.com/api/files/${selectedFiles[0].idStorage}`;
const response = await fetch(fileUrl);
const blob = await response.blob();
// Create a link element, use Blob URL
const link = document.createElement("a");
const url = window.URL.createObjectURL(blob);
link.href = url;
link.download = `${selectedFiles[0].name}.${selectedFiles[0].extension}`; // Nama file yang akan diunduh
document.body.appendChild(link);
link.click();
// Cleanup
window.URL.revokeObjectURL(url);
document.body.removeChild(link);
} catch (error) {
alert(error);
}
};
return (
<Box>
{(selectedFiles.length > 0 || dariSelectAll) && (
@@ -245,9 +277,14 @@ export default function NavbarDocumentDivision() {
}}>
<Flex justify={"center"} align={"center"} h={"100%"} w={"100%"}>
<SimpleGrid cols={{ base: 5, sm: 5, lg: 5 }} spacing="xs">
<Flex justify={'center'} align={'center'} direction={'column'}>
<BsDownload size={20} color={(selectedFiles.length > 0) ? 'white' : '#656060'} />
<Text fz={12} ta={"center"} c={(selectedFiles.length > 0) ? 'white' : '#656060'}>Unduh</Text>
<Flex justify={'center'} align={'center'} direction={'column'}
onClick={() => {
if ((selectedFiles.length > 0 && copyAllowed)) {
onDownload()
}
}}>
<BsDownload size={20} color={(selectedFiles.length > 0 && copyAllowed) ? 'white' : '#656060'} />
<Text fz={12} ta={"center"} c={(selectedFiles.length > 0 && copyAllowed) ? 'white' : '#656060'}>Unduh</Text>
</Flex>
<Flex justify={'center'} align={'center'} direction={'column'}>
<ActionIcon