import Styles from "@/constants/Styles"; import { apiDeleteTaskTugas, apiGetTaskOne, apiUpdateStatusTaskDivision } from "@/lib/api"; import { setUpdateTask } from "@/lib/taskUpdate"; import { useAuthSession } from "@/providers/AuthProvider"; import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons"; import { router, 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 AlertKonfirmasi from "../alertKonfirmasi"; import DrawerBottom from "../drawerBottom"; import ItemSectionTanggalTugas from "../itemSectionTanggalTugas"; import MenuItemRow from "../menuItemRow"; import ModalSelect from "../modalSelect"; import SkeletonTask from "../skeletonTask"; import Text from "../Text"; type Props = { id: string; title: string; status: number; dateStart: string; dateEnd: string; } export default function SectionTanggalTugasTask({refreshing}: {refreshing: boolean}) { const dispatch = useDispatch() const update = useSelector((state: any) => state.taskUpdate) const [isModal, setModal] = useState(false) const [isSelect, setSelect] = useState(false) const { token, decryptToken } = useAuthSession() const [loading, setLoading] = useState(true) const arrSkeleton = Array.from({ length: 5 }) const { id, detail } = useLocalSearchParams<{ id: string, detail: string }>(); const [data, setData] = useState([]) const [tugas, setTugas] = useState({ id: '', status: 0, }) async function handleLoad(loading: boolean) { try { setLoading(loading) const hasil = await decryptToken(String(token?.current)) const response = await apiGetTaskOne({ id: detail, user: hasil, cat: 'task' }) setData(response.data) } catch (error) { console.error(error) } finally { setLoading(false) } } async function handleUpdate(status: number) { try { const hasil = await decryptToken(String(token?.current)); const response = await apiUpdateStatusTaskDivision({ user: hasil, idProject: detail, status: status, }, tugas.id); if (response.success) { dispatch(setUpdateTask({ ...update, progress: !update.progress, task: !update.task })) Toast.show({ type: 'small', text1: 'Berhasil mengubah data', }) } else { Toast.show({ type: 'small', text1: response.message, }) } } catch (error) { console.error(error); Toast.show({ type: 'small', text1: 'Gagal mengubah data', }) } finally { setSelect(false) } } useEffect(() => { handleLoad(false) }, [update.task]) useEffect(() => { if (refreshing) handleLoad(false); }, [refreshing]); useEffect(() => { handleLoad(true) }, []) async function handleDelete() { try { const hasil = await decryptToken(String(token?.current)); const response = await apiDeleteTaskTugas({ user: hasil, idProject: detail, }, tugas.id); if (response.success) { dispatch(setUpdateTask({ ...update, progress: !update.progress, task: !update.task })) Toast.show({ type: 'small', text1: 'Berhasil menghapus data', }) } else { Toast.show({ type: 'small', text1: response.message, }) } } catch (error) { console.error(error); Toast.show({ type: 'small', text1: 'Gagal menghapus data', }) } finally { setModal(false); } } return ( <> Tanggal & Tugas { loading ? arrSkeleton.map((item, index) => { return ( ) }) : data.length > 0 ? data.map((item, index) => { return ( { setTugas({ id: item.id, status: item.status }) setModal(true) }} /> ); }) : Tidak ada tugas } } title="Update Status" onPress={() => { setModal(false) setTimeout(() => { setSelect(true) }, 600); }} /> } title="Edit Tugas" onPress={() => { setModal(false) router.push(`./update/${tugas.id}`) }} /> } title="Hapus Tugas" onPress={() => { setModal(false) AlertKonfirmasi({ title: 'Konfirmasi', desc: 'Apakah anda yakin ingin menghapus data ini?', onPress: () => { handleDelete() } }) }} /> setSelect(false)} onSelect={(value) => { handleUpdate(Number(value.val)) }} title="Status" open={isSelect} valChoose={String(tugas.status)} /> ) }