import Styles from "@/constants/Styles";
import { apiCopyDocument, apiMoveDocument } from "@/lib/api";
import { setUpdateDokumen } from "@/lib/dokumenUpdate";
import { useAuthSession } from "@/providers/AuthProvider";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import { useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { View } from "react-native";
import Toast from "react-native-toast-message";
import { useDispatch, useSelector } from "react-redux";
import DrawerBottom from "../drawerBottom";
import MenuItemRow from "../menuItemRow";
import ModalInformasi from "./modalInformasi";
import ModalSalinMove from "./modalSalinMove";
type Props = {
idStorage: string;
id: string;
name: string;
extension: string;
category: string;
path: string;
share: boolean;
createdBy: string;
createdAt: string;
updatedAt: string;
};
export default function ModalMore({
onClose,
data,
share,
}: {
onClose: () => void;
data: Props[];
share: boolean;
}) {
const { id } = useLocalSearchParams<{ id: string }>();
const [isInformasi, setInformasi] = useState(false);
const [isCut, setIsCut] = useState(false);
const [isCopy, setIsCopy] = useState(false);
const [forbidCopy, setForbidCopy] = useState(true);
const [nFileSelected, setNFileSelected] = useState(0);
const { token, decryptToken } = useAuthSession();
const dispatch = useDispatch();
const update = useSelector((state: any) => state.dokumenUpdate);
function cekFileSelected() {
const cek = data.some((i: any) => i.category == "FOLDER");
setForbidCopy(cek);
setNFileSelected(data.length);
}
useEffect(() => {
cekFileSelected();
}, [data]);
async function handleMove(path: string) {
try {
const hasil = await decryptToken(String(token?.current));
const response = await apiMoveDocument({
user: hasil,
dataItem: data,
path,
});
if (response.success) {
Toast.show({ type: 'small', text1: 'Berhasil memindahkan file', });
dispatch(setUpdateDokumen(!update));
} else {
Toast.show({ type: 'small', text1: response.message, });
}
} catch (error) {
console.error(error);
Toast.show({ type: 'small', text1: 'Terjadi kesalahan', });
} finally {
setIsCut(false);
onClose();
}
}
async function handleCopy(path: string) {
try {
const hasil = await decryptToken(String(token?.current));
const response = await apiCopyDocument({
user: hasil,
dataItem: data,
path,
idDivision: id
});
if (response.success) {
Toast.show({ type: 'small', text1: 'Berhasil menyalin file', });
dispatch(setUpdateDokumen(!update));
} else {
Toast.show({ type: 'small', text1: response.message, });
}
} catch (error) {
console.error(error);
Toast.show({ type: 'small', text1: 'Terjadi kesalahan', });
} finally {
setIsCopy(false);
onClose();
}
}
return (
<>
{!share && (
}
title="Pindah"
onPress={() => {
setIsCut(true);
}}
/>
)}
{!forbidCopy && (
}
title="Salin"
onPress={() => {
setIsCopy(true);
}}
/>
)}
{nFileSelected == 1 && (
}
title="Informasi"
onPress={() => {
setInformasi(true);
}}
/>
)}
setInformasi(false)}
title="Informasi Dokumen"
height={80}
>
setIsCut(false)}
category={"move"}
onConfirm={(value: string) => handleMove(value)}
dataChoose={data}
/>
setIsCopy(false)}
category={"copy"}
onConfirm={(value: string) => handleCopy(value)}
dataChoose={data}
/>
>
);
}