import { ActionIcon, BoxButtonOnFooter, ButtonCustom, CenterCustom, Spacing, StackCustom, TextAreaCustom, 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 { useAuth } from "@/hooks/use-auth"; import { apiVotingCreate } from "@/service/api-client/api-voting"; import { Ionicons } from "@expo/vector-icons"; import { router } from "expo-router"; import _ from "lodash"; import { useState } from "react"; import { View } from "react-native"; import Toast from "react-native-toast-message"; export default function VotingCreate() { const { user } = useAuth(); const [isLoading, setIsLoading] = useState(false); const [data, setData] = useState({ authorId: "", title: "", deskripsi: "", awalVote: "", akhirVote: "", listVote: [], }); const [listVote, setListVote] = useState([ { name: "Nama Pilihan", value: "", }, { name: "Nama Pilihan", value: "", }, ]); const handlerSubmit = async () => { if (!data.title || !data.deskripsi || !data.awalVote || !data.akhirVote) { Toast.show({ type: "info", text1: "Lengkapi semua data", }); return; } if (listVote.some((item: any) => item.value === "")) { Toast.show({ type: "info", text1: "Lengkapi semua data pilihan", }); return; } try { setIsLoading(true); const newData = { ...data, authorId: user?.id, awalVote: new Date(data.awalVote as any).toISOString(), akhirVote: new Date(data.akhirVote as any).toISOString(), listVote: listVote, }; const response = await apiVotingCreate(newData); // console.log("[RESPONSE]", JSON.stringify(response, null, 2)); if (response.success) { Toast.show({ type: "success", text1: "Data berhasil disimpan", }); router.replace("/(application)/(user)/voting/(tabs)/status"); } else { Toast.show({ type: "error", text1: "Data gagal disimpan", }); } } catch (error) { console.log("[ERROR]", error); } finally { setIsLoading(false); } }; const buttonSubmit = () => { return ( <> handlerSubmit()}> Simpan ); }; return ( setData({ ...data, title: value })} /> setData({ ...data, deskripsi: value })} /> setData({ ...data, awalVote: value })} minimumDate={new Date(Date.now())} /> setData({ ...data, akhirVote: value })} minimumDate={ data.awalVote ? new Date(data.awalVote) : new Date(Date.now()) } /> {listVote.map((item, index) => ( setListVote( listVote.map((item, i) => i === index ? { ...item, value } : item ) ) } /> ))} = 4} onPress={() => { setListVote([...listVote, { name: "Nama Pilihan", value: "" }]); }} icon={ } size="xl" /> { const list = _.clone(listVote); list.pop(); setListVote(list); }} icon={ } size="xl" /> ); }