/* eslint-disable react-hooks/exhaustive-deps */ import { ActionIcon, BoxButtonOnFooter, ButtonCustom, CenterCustom, LoaderCustom, Spacing, StackCustom, TextAreaCustom, TextCustom, TextInputCustom, ViewWrapper, } from "@/components"; import DateTimePickerCustom from "@/components/DateInput/DateTimePickerCustom"; import { MainColor } from "@/constants/color-palet"; import { ICON_SIZE_XLARGE } from "@/constants/constans-value"; import { apiVotingGetOne, apiVotingUpdateData, } from "@/service/api-client/api-voting"; import { Ionicons } from "@expo/vector-icons"; import { router, useFocusEffect, useLocalSearchParams } from "expo-router"; import _ from "lodash"; import { useCallback, useState } from "react"; import { View } from "react-native"; import Toast from "react-native-toast-message"; interface IEditData { title?: string; deskripsi?: string; awalVote?: string; akhirVote?: string; Voting_DaftarNamaVote?: [ { value?: string; } ]; } export default function VotingEdit() { const { id } = useLocalSearchParams(); const [loadingGetData, setLoadingGetData] = useState(false); const [data, setData] = useState(); const [isLoading, setIsLoading] = useState(false); useFocusEffect( useCallback(() => { onLoadData(); }, [id]) ); const onLoadData = async () => { try { setLoadingGetData(true); const response = await apiVotingGetOne({ id: id as string }); if (response.success) { const data = response.data; setData({ title: data.title, deskripsi: data.deskripsi, awalVote: data.awalVote, akhirVote: data.akhirVote, Voting_DaftarNamaVote: data.Voting_DaftarNamaVote, }); } } catch (error) { console.log("[ERROR]", error); } finally { setLoadingGetData(false); } }; const validateDateRange = ({ selectedStratDate, selectedEndDate, }: { selectedStratDate: string | Date; selectedEndDate: string | Date; }): { isValid: boolean; error?: string } => { const startDate = new Date(selectedStratDate); const endDate = new Date(selectedEndDate); // Cek apakah tanggal valid if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) { return { isValid: false, error: "Invalid date provided", }; } if (startDate >= endDate) { return { isValid: false, error: "Ubah tanggal berakhirnya event", }; } return { isValid: true, error: undefined, }; }; const validateForm = async () => { if (!data?.title || !data?.deskripsi) { Toast.show({ type: "info", text1: "Lengkapi semua data", }); return false; } if (data?.Voting_DaftarNamaVote?.some((item: any) => item.value === "")) { Toast.show({ type: "info", text1: "Isi semua data pilihan", }); return false; } const startDate = new Date(data?.awalVote as any); const endDate = new Date(data?.akhirVote as any); if (startDate >= endDate) { Toast.show({ type: "info", text1: "Ubah tanggal berakhirnya event", }); return false; } return true; }; const handlerUpdateSubmit = async () => { const isValid = await validateForm(); if (!isValid) return; try { setIsLoading(true); const newData = { ...data, awalVote: new Date(data?.awalVote as any).toISOString(), akhirVote: new Date(data?.akhirVote as any).toISOString(), listVote: data?.Voting_DaftarNamaVote?.map((item: any) => item.value), }; const response = await apiVotingUpdateData({ id: id as string, data: newData, category: "edit", }); if (response.success) { Toast.show({ type: "success", text1: response.message, }); return router.back(); } else { Toast.show({ type: "error", text1: response.message, }); } } catch (error) { console.log("[ERROR]", error); } finally { setIsLoading(false); } }; const buttonSubmit = () => { return ( <> handlerUpdateSubmit()} > Update ); }; return ( {loadingGetData ? ( ) : ( setData({ ...data, title: text })} /> setData({ ...data, deskripsi: text })} /> { setData({ ...data, awalVote: date }); }} /> { setData({ ...data, akhirVote: date }); }} /> {validateDateRange({ selectedStratDate: data?.awalVote as any, selectedEndDate: data?.akhirVote as any, }).isValid ? ( { validateDateRange({ selectedStratDate: data?.awalVote as any, selectedEndDate: data?.akhirVote as any, }).error } ) : ( { validateDateRange({ selectedStratDate: data?.awalVote as any, selectedEndDate: data?.akhirVote as any, }).error } )} {data?.Voting_DaftarNamaVote?.map((item: any, index: number) => ( setData({ ...(data as any), Voting_DaftarNamaVote: data?.Voting_DaftarNamaVote?.map( (item: any, i: any) => i === index ? { ...item, value } : item ), }) } /> ))} = 4} onPress={() => { setData({ ...(data as any), Voting_DaftarNamaVote: [ ...(data as any)?.Voting_DaftarNamaVote, { value: "" }, ], }); }} icon={ } size="xl" /> { const list = _.clone((data as any)?.Voting_DaftarNamaVote); list.pop(); setData({ ...(data as any), Voting_DaftarNamaVote: list, }); }} icon={ } size="xl" /> )} ); }