/* eslint-disable react-hooks/exhaustive-deps */ import { AlertDefaultSystem, BadgeCustom, BaseBox, CircleContainer, Grid, Spacing, StackCustom, TextCustom, ViewWrapper, } from "@/components"; import AdminBackButtonAntTitle from "@/components/_ShareComponent/Admin/BackButtonAntTitle"; import AdminButtonReject from "@/components/_ShareComponent/Admin/ButtonReject"; import AdminButtonReview from "@/components/_ShareComponent/Admin/ButtonReview"; import { GridDetail_4_8 } from "@/components/_ShareComponent/GridDetail_4_8"; import ReportBox from "@/components/Box/ReportBox"; import { MainColor } from "@/constants/color-palet"; import funUpdateStatusVoting from "@/screens/Admin/Voting/funUpdateStatus"; import { apiAdminVotingById } from "@/service/api-admin/api-admin-voting"; import { colorBadgeStatus } from "@/utils/colorBadge"; import { dateTimeView } from "@/utils/dateTimeView"; import { Entypo } from "@expo/vector-icons"; import dayjs from "dayjs"; import { router, useFocusEffect, useLocalSearchParams } from "expo-router"; import _ from "lodash"; import { useCallback, useState } from "react"; import { List } from "react-native-paper"; import Toast from "react-native-toast-message"; export default function AdminVotingDetail() { const { id, status } = useLocalSearchParams(); const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(false); console.log("[status]", status); useFocusEffect( useCallback(() => { onLoadData(); }, [id]) ); const onLoadData = async () => { try { const response = await apiAdminVotingById({ id: id as string, }); if (response.success) { setData(response.data); } } catch (error) { console.log("[ERROR]", error); } }; const listData = [ { label: "Username", value: (data && data?.Author?.username) || "-", }, { label: "Judul", value: (data && data?.title) || "-", }, { label: "Status", value: data && data?.Voting_Status?.name ? ( {status === "history" ? "Riwayat" : _.startCase(status as string)} ) : ( "-" ), }, { label: "Mulai Voting", value: (data && data?.awalVote && dateTimeView({ date: data?.awalVote })) || "-", }, { label: "Voting Berakhir", value: (data && data?.akhirVote && dateTimeView({ date: data?.akhirVote })) || "-", }, { label: "Deskripsi", value: (data && data?.deskripsi) || "-", }, { label: "Daftar Pilihan", value: data && data?.Voting_DaftarNamaVote ? data?.Voting_DaftarNamaVote?.map((item: any, i: number) => ( {item?.value}} left={(props) => ( )} /> )) : "-", }, ]; const handleUpdate = async ({ changeStatus, }: { changeStatus: "publish" | "review" | "reject"; }) => { try { const dateNow = new Date(); // const dateNowHour = dateNow.getHours(); // const awalVoteHour = dayjs(data?.awalVote).hour(); const isBefore = dayjs(dateNow).diff(dayjs(data?.awalVote), "hours") < 0; console.log("[IS BEFORE]", isBefore); if (!isBefore) { Toast.show({ type: "error", text1: "Tanggal & waktu telah lewat", text2: "Silahkan report dan ubah tanggal & waktu voting", }); return; } Toast.show({ type: "success", text1: "Berhasil mempublikasikan data", }); setIsLoading(true); const response = await funUpdateStatusVoting({ id: id as string, changeStatus, }); if (!response.success) { Toast.show({ type: "error", text1: "Gagal mempublikasikan data", }); } Toast.show({ type: "success", text1: "Berhasil mempublikasikan data", }); router.back(); } catch (error) { console.log("[ERROR]", error); } finally { setIsLoading(false); } }; return ( <> } > {listData.map((item, i) => ( {item.label}} value={{item.value}} /> ))} {status === "publish" || (status === "history" && ( Hasil Voting {data?.Voting_DaftarNamaVote?.map( (item: any, index: number) => ( {item?.value} ) )} ))} {data && data?.catatan && (status === "review" || status === "reject") && ( )} {status === "review" && ( { AlertDefaultSystem({ title: "Publish", message: "Apakah anda yakin ingin mempublikasikan data ini?", textLeft: "Cancel", textRight: "Publish", onPressRight: () => { handleUpdate({ changeStatus: "publish" }); }, }); }} onReject={() => { router.push(`/admin/voting/${id}/${status}/reject-input`); }} /> )} {status === "reject" && ( { router.push(`/admin/voting/${id}/${status}/reject-input`); }} /> )} ); }