/* eslint-disable react-hooks/exhaustive-deps */ import { ButtonCustom, LoaderCustom, SelectCustom, StackCustom, TextAreaCustom, TextInputCustom, ViewWrapper, } from "@/components"; import { apiCollaborationEditData, apiCollaborationGetOne, } from "@/service/api-client/api-collaboration"; import { apiMasterCollaborationType } from "@/service/api-client/api-master"; import { router, useFocusEffect, useLocalSearchParams } from "expo-router"; import { useCallback, useState } from "react"; import Toast from "react-native-toast-message"; export default function CollaborationEdit() { const { id } = useLocalSearchParams(); console.log("id :", id); const [data, setData] = useState(); const [listMaster, setListMaster] = useState([]); const [loadingData, setLoadingData] = useState(false); const [isLoading, setIsLoading] = useState(false); useFocusEffect( useCallback(() => { const fetchData = async () => { try { setLoadingData(true); await onLoadData(); await onLoadMaster(); } catch (error) { console.log("[ERROR]", error); } finally { setLoadingData(false); } }; fetchData(); }, [id]) ); const onLoadData = async () => { try { const response = await apiCollaborationGetOne({ id: id as string }); if (response.success) { setData(response.data); } } catch (error) { console.log("[ERROR]", error); } }; async function onLoadMaster() { try { const response = await apiMasterCollaborationType(); setListMaster(response.data); } catch (error) { console.log("[ERROR]", error); } } const handlerSubmitUpdate = async () => { if ( !data?.title || !data?.lokasi || !data?.projectCollaborationMaster_IndustriId || !data?.purpose || !data?.benefit ) { Toast.show({ type: "error", text1: "Gagal", text2: "Harap isi semua data", }); return; } try { setIsLoading(true); const response = await apiCollaborationEditData({ id: id as string, data: data, }); if (response.success) { Toast.show({ type: "success", text1: response.message, }); router.back(); } else { Toast.show({ type: "error", text1: response.message, }); } } catch (error) { console.log("[ERROR]", error); } finally { setIsLoading(false); } }; return ( {loadingData ? ( ) : ( setData({ ...data, title: value })} /> setData({ ...data, lokasi: value })} /> ({ label: item.name, value: item.id, }))} value={data?.projectCollaborationMaster_IndustriId} onChange={(value) => setData({ ...data, projectCollaborationMaster_IndustriId: value }) } /> setData({ ...data, purpose: value })} /> setData({ ...data, benefit: value })} /> { handlerSubmitUpdate(); }} /> )} ); }