From 1cfda6745e74b6c3e3c06bcb2514b5f63fae68dd Mon Sep 17 00:00:00 2001 From: Wikzyy Date: Tue, 18 Mar 2025 10:36:40 +0700 Subject: [PATCH 1/2] chore: simplify status display by replacing EnforceSwitch with badges --- .../manage-position/hooks/ManagePositionContext.tsx | 13 +++++++++---- .../user/manage-user/hooks/ManageUserContext.tsx | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/pages/settings/user/manage-position/hooks/ManagePositionContext.tsx b/src/pages/settings/user/manage-position/hooks/ManagePositionContext.tsx index ee3f565..2bc9d80 100644 --- a/src/pages/settings/user/manage-position/hooks/ManagePositionContext.tsx +++ b/src/pages/settings/user/manage-position/hooks/ManagePositionContext.tsx @@ -85,11 +85,16 @@ const ManagePositionContextProvider = ({ children }: { children: React.ReactNode enableSorting: false, enableHiding: false, cell: ({ row }) => { + const isActive = row.original.status === 'Y'; + return ( - {}} - /> + + {isActive ? 'Active' : 'Inactive'} + ); }, meta: { diff --git a/src/pages/settings/user/manage-user/hooks/ManageUserContext.tsx b/src/pages/settings/user/manage-user/hooks/ManageUserContext.tsx index 284ce93..efae604 100644 --- a/src/pages/settings/user/manage-user/hooks/ManageUserContext.tsx +++ b/src/pages/settings/user/manage-user/hooks/ManageUserContext.tsx @@ -127,11 +127,16 @@ const ManageUserContextProvider = ({ children }: { children: React.ReactNode }) enableSorting: false, enableHiding: false, cell: ({ row }) => { + const isActive = row.original.status === 'Y'; + return ( - {}} - /> + + {isActive ? 'Active' : 'Inactive'} + ); }, meta: { From e0c064c74931a85ea2acb8d428b9d5345336b881 Mon Sep 17 00:00:00 2001 From: Wikzyy Date: Tue, 18 Mar 2025 10:46:39 +0700 Subject: [PATCH 2/2] feat: increase fetchSucos limit and add doFetchData function --- src/pages/master/aldeias/blocks/AddDialog.tsx | 38 +++++------ .../master/aldeias/blocks/EditDialog.tsx | 68 +++++++++++++------ .../master/municipios/blocks/EditDialog.tsx | 43 +++++++++--- .../master/postoadms/blocks/AddDialog.tsx | 38 +++++------ .../master/postoadms/blocks/EditDialog.tsx | 67 ++++++++++++------ .../master/products/blocks/EditDialog.tsx | 33 ++++++++- 6 files changed, 199 insertions(+), 88 deletions(-) diff --git a/src/pages/master/aldeias/blocks/AddDialog.tsx b/src/pages/master/aldeias/blocks/AddDialog.tsx index 26c53f3..af089ef 100644 --- a/src/pages/master/aldeias/blocks/AddDialog.tsx +++ b/src/pages/master/aldeias/blocks/AddDialog.tsx @@ -78,6 +78,24 @@ const AddDialog = () => { [formField] ); + const doFetchSucos = async (sorting: any) => { + try { + sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; + const response = await GetData(`${API_URL}/sucos/list`, { + limit: 1000, + page: 1, + with_deleted: false, + order_field: sorting[0].id, + order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' + }); + + // console.log('SUCOS', response?.data); + setSucos(response?.data.list); + } catch (error) { + console.error('Error fetching municipios', error); + } + }; + const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -102,25 +120,7 @@ const AddDialog = () => { }, [formattedTime]); useEffect(() => { - const fetchSucos = async (sorting: any) => { - try { - sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; - const response = await GetData(`${API_URL}/sucos/list`, { - limit: 100, - page: 1, - with_deleted: false, - order_field: sorting[0].id, - order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' - }); - - // console.log('SUCOS', response?.data); - setSucos(response?.data.list); - } catch (error) { - console.error('Error fetching municipios', error); - } - }; - - fetchSucos([{ id: 'name', desc: false }]); + doFetchSucos([{ id: 'name', desc: false }]); }, []); useEffect(() => { diff --git a/src/pages/master/aldeias/blocks/EditDialog.tsx b/src/pages/master/aldeias/blocks/EditDialog.tsx index 179208c..d69981e 100644 --- a/src/pages/master/aldeias/blocks/EditDialog.tsx +++ b/src/pages/master/aldeias/blocks/EditDialog.tsx @@ -77,6 +77,42 @@ const EditDialog = () => { [selectedAldeias, formField] ); + const doFetchSucos = async (sorting: any) => { + try { + sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; + const response = await GetData(`${API_URL}/sucos/list`, { + limit: 1000, + page: 1, + with_deleted: false, + order_field: sorting[0].id, + order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' + }); + + // console.log('SUCOS', response?.data); + setSucos(response?.data.list); + } catch (error) { + console.error('Error fetching municipios', error); + } + }; + + const doFetchData = useCallback(async (id: string) => { + const response = await GetData(`${API_URL}/aldeias/getdata/${id}`, { id }); + + if (response?.status) { + setFormField((prev) => ({ + ...prev, + name: response.data.name, + sucos_id: response.data.sucos.id + })); + } else { + setFormField((prev) => ({ + ...prev, + name: '', + sucos_id: 0 + })); + } + }, []); + const handleUpdate = (e: React.FormEvent) => { e.preventDefault(); @@ -90,6 +126,18 @@ const EditDialog = () => { setAlert({ show: false, message: '' }); }; + useEffect(() => { + if (selectedAldeias) { + doFetchData(selectedAldeias); + } + }, [selectedAldeias]); + + useEffect(() => { + if (showEditDialog === false) { + resetForm(); + } + }, [showEditDialog]); + useEffect(() => { if (showEditDialog) { setFormField({ @@ -101,25 +149,7 @@ const EditDialog = () => { }, [formattedTime]); useEffect(() => { - const fetchSucos = async (sorting: any) => { - try { - sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; - const response = await GetData(`${API_URL}/sucos/list`, { - limit: 100, - page: 1, - with_deleted: false, - order_field: sorting[0].id, - order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' - }); - - // console.log('SUCOS', response?.data); - setSucos(response?.data.list); - } catch (error) { - console.error('Error fetching municipios', error); - } - }; - - fetchSucos([{ id: 'name', desc: false }]); + doFetchSucos([{ id: 'name', desc: false }]); }, []); return ( diff --git a/src/pages/master/municipios/blocks/EditDialog.tsx b/src/pages/master/municipios/blocks/EditDialog.tsx index 8cf5d5f..50406bc 100644 --- a/src/pages/master/municipios/blocks/EditDialog.tsx +++ b/src/pages/master/municipios/blocks/EditDialog.tsx @@ -25,7 +25,7 @@ const EditDialog = () => { const { showEditDialog, handleEditDialog, selectedMunicipios, municipios } = useManageMunicipiosContext(); const { reload } = useDataGrid(); - const { PutData } = useCallApi(); + const { PutData, GetData } = useCallApi(); const parsedUser = getAuth()?.user; const [alert, setAlert] = useState({ show: false, @@ -44,6 +44,7 @@ const EditDialog = () => { const resetForm = () => { setFormField(initialState); + setAlert({ show: false, message: '' }); }; const doUpdateMunicipios = useCallback( @@ -74,6 +75,22 @@ const EditDialog = () => { [selectedMunicipios, formField] ); + const doFetchData = useCallback(async (id: string) => { + const response = await GetData(`${API_URL}/municipios/getdata/${id}`, { id }); + + if (response?.status) { + setFormField((prev) => ({ + ...prev, + name: response.data.name + })); + } else { + setFormField((prev) => ({ + ...prev, + name: '' + })); + } + }, []); + const handleUpdate = (e: React.FormEvent) => { e.preventDefault(); @@ -82,20 +99,22 @@ const EditDialog = () => { return; } - // setFormField({ - // name: formField.name, - // created_by: parsedUser.email, - // created_at: formattedTime - // }); - doUpdateMunicipios(e); console.log(formField); setAlert({ show: false, message: '' }); }; - // const doFetchMunicipios = useCallback(async (id: string) => { - // const response = await axios.get(`${API_URL}/municipios/${id}`); - // }, []); + useEffect(() => { + if (selectedMunicipios) { + doFetchData(selectedMunicipios); + } + }, [selectedMunicipios]); + + useEffect(() => { + if (showEditDialog === false) { + resetForm(); + } + }, [showEditDialog]); useEffect(() => { if (selectedMunicipios) { @@ -127,7 +146,9 @@ const EditDialog = () => {
- + { [formField] ); + const doFetchMunicipios = async (sorting: any) => { + try { + sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; + const response = await GetData(`${API_URL}/municipios/list`, { + limit: 100, + page: 1, + with_deleted: false, + order_field: sorting[0].id, + order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' + }); + + // console.log(response?.data); + setMunicipios(response?.data.list); + } catch (error) { + console.error('Error fetching municipios', error); + } + }; + const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -110,25 +128,7 @@ const AddDialog = () => { }, [showAddDialog]); useEffect(() => { - const fetchMunicipios = async (sorting: any) => { - try { - sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; - const response = await GetData(`${API_URL}/municipios/list`, { - limit: 100, - page: 1, - with_deleted: false, - order_field: sorting[0].id, - order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' - }); - - // console.log(response?.data); - setMunicipios(response?.data.list); - } catch (error) { - console.error('Error fetching municipios', error); - } - }; - - fetchMunicipios([{ id: 'name', desc: false }]); + doFetchMunicipios([{ id: 'name', desc: false }]); }, []); return ( diff --git a/src/pages/master/postoadms/blocks/EditDialog.tsx b/src/pages/master/postoadms/blocks/EditDialog.tsx index abb8f0d..d170d1a 100644 --- a/src/pages/master/postoadms/blocks/EditDialog.tsx +++ b/src/pages/master/postoadms/blocks/EditDialog.tsx @@ -80,6 +80,41 @@ const EditDialog = () => { [selectedPostoAdms, formField] ); + const doFetchMunicipios = useCallback(async (sorting: any) => { + try { + sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; + const response = await GetData(`${API_URL}/municipios/list`, { + limit: 100, + page: 1, + with_deleted: false, + order_field: sorting[0].id, + order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' + }); + + // console.log('MUNICIPIOS: ', response?.data); + setMunicipios(response?.data.list); + } catch (error) { + console.error('Error fetching municipios', error); + } + }, []); + + const doFetchData = useCallback(async (id: string) => { + const response = await GetData(`${API_URL}/postoadms/getdata/${id}`, { id }); + + if (response?.status) { + setFormField((prev) => ({ + ...prev, + name: response.data.name, + municipio_id: response.data.municipios.id + })); + } else { + setFormField((prev) => ({ + ...prev, + name: '' + })); + } + }, []); + const handleUpdate = (e: React.FormEvent) => { e.preventDefault(); @@ -93,6 +128,18 @@ const EditDialog = () => { setAlert({ show: false, message: '' }); }; + useEffect(() => { + if (showEditDialog === false) { + resetForm(); + } + }, [showEditDialog]); + + useEffect(() => { + if (selectedPostoAdms) { + doFetchData(selectedPostoAdms); + } + }, [selectedPostoAdms]); + useEffect(() => { if (selectedPostoAdms) { setFormField({ @@ -104,25 +151,7 @@ const EditDialog = () => { }, [formattedTime]); useEffect(() => { - try { - const fetchMunicipios = async (sorting: any) => { - sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; - const response = await GetData(`${API_URL}/municipios/list`, { - limit: 100, - page: 1, - with_deleted: false, - order_field: sorting[0].id, - order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' - }); - - // console.log(response?.data); - setMunicipios(response?.data.list); - }; - - fetchMunicipios([{ id: 'name', desc: false }]); - } catch (error) { - console.error('Error fetching municipios', error); - } + doFetchMunicipios([{ id: 'name', desc: false }]); }, []); // console.log(selectedPostoAdms); diff --git a/src/pages/master/products/blocks/EditDialog.tsx b/src/pages/master/products/blocks/EditDialog.tsx index a34f93d..06eb7b6 100644 --- a/src/pages/master/products/blocks/EditDialog.tsx +++ b/src/pages/master/products/blocks/EditDialog.tsx @@ -20,7 +20,7 @@ const API_URL = apiConfig.service_master_data; const EditDialog = () => { const { showEditDialog, handleEditDialog, selectedProducts } = useManageProductsContext(); const { reload } = useDataGrid(); - const { PutData } = useCallApi(); + const { PutData, GetData } = useCallApi(); const parsedUser = getAuth()?.user; const created_time = new Date(); const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' '); @@ -66,6 +66,25 @@ const EditDialog = () => { [selectedProducts, formField] ); + const doFetchData = useCallback(async (id: string) => { + const response = await GetData(`${API_URL}/product/getdata/${selectedProducts}`, { id }); + + if (response?.status) { + setFormField((prev) => ({ + ...prev, + name: response.data.name, + description: response.data.description, + price_point: response.data.price_point, + price_cash: response.data.price_cash, + cashback_point: response.data.cashback_point, + cashback_cash: response.data.cashback_cash, + status: response.data.status + })); + } else { + setFormField(initialState); + } + }, []); + const handleUpdate = (e: React.FormEvent) => { e.preventDefault(); @@ -89,6 +108,18 @@ const EditDialog = () => { setAlert({ show: false, message: '' }); }; + useEffect(() => { + if (showEditDialog === false) { + resetForm(); + } + }, [showEditDialog]); + + useEffect(() => { + if (selectedProducts) { + doFetchData(selectedProducts); + } + }, [selectedProducts]); + useEffect(() => { if (showEditDialog) { setFormField({