fix fill edit formField

This commit is contained in:
Wikzyy
2025-03-20 10:15:01 +07:00
parent 3812e73b31
commit ac3ccbdaba
2 changed files with 81 additions and 53 deletions

View File

@ -20,7 +20,7 @@ const API_URL = apiConfig.service_master_data;
const EditDialog = () => { const EditDialog = () => {
const { showEditDialog, handleEditDialog, selectedProfession } = useManageProfessionContext(); const { showEditDialog, handleEditDialog, selectedProfession } = useManageProfessionContext();
const { reload } = useDataGrid(); const { reload } = useDataGrid();
const { PutData } = useCallApi(); const { PutData, GetData } = useCallApi();
const parsedUser = getAuth()?.user; const parsedUser = getAuth()?.user;
const created_time = new Date(); const created_time = new Date();
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' '); const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
@ -62,6 +62,19 @@ const EditDialog = () => {
[selectedProfession, formField] [selectedProfession, formField]
); );
const doFetchData = useCallback(async (id: string) => {
const response = await GetData(`${API_URL}/profession/getdata/${id}`, { id });
if (response?.status) {
setFormField((prev) => ({
...prev,
name: response.data.name
}));
} else {
setFormField(initialState);
}
}, []);
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => { const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
@ -75,6 +88,12 @@ const EditDialog = () => {
setAlert({ show: false, message: '' }); setAlert({ show: false, message: '' });
}; };
useEffect(() => {
if (selectedProfession) {
doFetchData(selectedProfession);
}
}, [selectedProfession]);
useEffect(() => { useEffect(() => {
if (showEditDialog) { if (showEditDialog) {
setFormField({ setFormField({
@ -85,6 +104,12 @@ const EditDialog = () => {
} }
}, [formattedTime]); }, [formattedTime]);
useEffect(() => {
if (showEditDialog === false) {
resetForm();
}
}, [showEditDialog]);
return ( return (
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}> <Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden"> <DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">

View File

@ -91,22 +91,54 @@ const EditDialog = () => {
[selectedProvider, formField] [selectedProvider, formField]
); );
const doFetchData = useCallback(async () => { const getCustomerList = async (sorting: any) => {
if (selectedProvider) { try {
const data = provider.find((item) => item.id === selectedProvider); sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
if (data) { const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, {
setFormField((prev) => ({ limit: 100,
...prev, page: 1,
name: data.name, with_deleted: false,
description: data.description, order_field: sorting[0].id,
type: data.type, order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
status: data.status, });
transactionTypeId: data.transactionTypeId,
agentId: data.agentId // console.log('CUSTOMER: ', response?.data);
})); setCustomers(response?.data.list);
} } catch (error) {
} else { console.error('Error fetching customer', error);
resetForm(); }
};
const getTransactionTypeList = async (sorting: any) => {
try {
const response = await GetData(`${API_URL_TRANSACTION}/transactiontype/list`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
// console.log('TRANSACTION TYPE: ', response?.data);
setTransactions(response?.data.list);
} catch (error) {
console.error('Error fetching transaction type', error);
}
};
const doFetchData = useCallback(async (id: string) => {
const response = await GetData(`${API_URL_MASTERDATA}/provider/getdata/${id}`, { id });
if (response?.status) {
setFormField((prev) => ({
...prev,
name: response?.data.name,
description: response?.data.description,
type: response?.data.type,
status: response?.data.status,
transactionTypeId: response?.data.transactionTypeId,
agentId: response?.data.agentId
}));
} }
}, []); }, []);
@ -130,9 +162,15 @@ const EditDialog = () => {
setAlert({ show: false, message: '' }); setAlert({ show: false, message: '' });
}; };
useEffect(() => {
if (showEditDialog === false) {
resetForm();
}
}, [showEditDialog]);
useEffect(() => { useEffect(() => {
if (selectedProvider) { if (selectedProvider) {
doFetchData(); doFetchData(selectedProvider);
} }
}, [selectedProvider]); }, [selectedProvider]);
@ -147,41 +185,6 @@ const EditDialog = () => {
}, [formattedTime]); }, [formattedTime]);
useEffect(() => { useEffect(() => {
const getCustomerList = async (sorting: any) => {
try {
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
// console.log('CUSTOMER: ', response?.data);
setCustomers(response?.data.list);
} catch (error) {
console.error('Error fetching customer', error);
}
};
const getTransactionTypeList = async (sorting: any) => {
try {
const response = await GetData(`${API_URL_TRANSACTION}/transactiontype/list`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
// console.log('TRANSACTION TYPE: ', response?.data);
setTransactions(response?.data.list);
} catch (error) {
console.error('Error fetching transaction type', error);
}
};
getCustomerList([{ id: 'msisdn', desc: false }]); getCustomerList([{ id: 'msisdn', desc: false }]);
getTransactionTypeList([{ id: 'name', desc: false }]); getTransactionTypeList([{ id: 'name', desc: false }]);
}, []); }, []);