From faf9adbb42dddf3e4f39c1cc55f0fcdbd1077068 Mon Sep 17 00:00:00 2001 From: Wikzyy Date: Thu, 8 May 2025 16:50:47 +0700 Subject: [PATCH] update manage wallet --- src/pages/master/wallet/blocks/AddDialog.tsx | 150 +++++++++---------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/src/pages/master/wallet/blocks/AddDialog.tsx b/src/pages/master/wallet/blocks/AddDialog.tsx index 90ad6ba..a88bb23 100644 --- a/src/pages/master/wallet/blocks/AddDialog.tsx +++ b/src/pages/master/wallet/blocks/AddDialog.tsx @@ -24,21 +24,7 @@ import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { doSaveLogActivity } from '@/actions/GlobalActions'; import { RefreshCw } from 'lucide-react'; - -interface CurrencyProps { - ID: string; - code: string; - name: string; - prefix: string; - status: string; -} - -interface GroupProps { - id: string; - name: string; - description: string; - status: string; -} +import { CurrencyProps, GroupProps, initialStateWallet, validateFormsWallet } from './Types'; const API_URL_WALLET = apiConfig.service_wallet; const API_URL_MASTER_DATA = apiConfig.service_master_data; @@ -47,31 +33,15 @@ const AddDialog = () => { const { showAddDialog, handleAddDialog, selectedWallet } = useManageWalletContext(); const { GetData, PostData } = useCallApi(); const { reload } = useDataGrid(); - const [alert, setAlert] = useState({ - show: false, - message: '' - }); - const initialState: { - name: string; - description: string; - status: string; - group: string[]; - id_currency: string; - } = { - name: '', - description: '', - status: '', - group: [], - id_currency: '' - }; - const [formField, setFormField] = useState(initialState); + const [errors, setErrors] = useState>({}); + const [formField, setFormField] = useState(initialStateWallet); const [isSubmitting, setIsSubmitting] = useState(false); const [currencies, setCurrencies] = useState([]); const [groups, setGroups] = useState([]); const resetForm = () => { - setFormField(initialState); - setAlert({ show: false, message: '' }); + setFormField(initialStateWallet); + setErrors({}); }; const handleGroupChange = (groupId: string) => { @@ -115,8 +85,7 @@ const AddDialog = () => { doSaveLogActivity(createActivity); } else { - toast.error('Failed Create Wallet'); - setAlert({ show: true, message: response?.message }); + toast.error(response?.message); } } catch (error) { toast.error('Something went wrong, please try again.'); @@ -130,20 +99,11 @@ const AddDialog = () => { const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); - if ( - formField.name.trim() === '' || - formField.description.trim() === '' || - formField.status.trim() === '' || - formField.group.length === 0 || - formField.id_currency.trim() === '' - ) { - setAlert({ show: true, message: 'Please fill in all required fields.' }); + if (!validateFormsWallet(formField, setErrors)) { return; } - - // console.log(formField); - doCreateWallet(e); - setAlert({ show: false, message: '' }); + console.log(formField); + // doCreateWallet(e); }; const getCurrencyLists = async (sorting: any) => { @@ -155,7 +115,6 @@ const AddDialog = () => { order_field: sorting[0].id, order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' }); - // console.log('Currency: ', response?.data); setCurrencies(response?.data.list); } catch (error) { console.error('Error fetching currency', error); @@ -171,13 +130,17 @@ const AddDialog = () => { order_field: sorting[0].id, order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' }); - // console.log('Group: ', response?.data); setGroups(response?.data.list); } catch (error) { console.error('Error fetching group', error); } }; + const selectedGroups = groups + .filter((g) => formField.group.includes(g.id)) + .map((g) => g.name) + .join(', '); + useEffect(() => { getCurrencyLists([{ id: 'name', desc: false }]); getGroupLists([{ id: 'name', desc: false }]); @@ -198,12 +161,6 @@ const AddDialog = () => {
- {alert.show && ( - -

{alert.message}

-
- )} -
@@ -212,12 +169,17 @@ const AddDialog = () => { Wallet Name* setFormField({ ...formField, name: e.target.value })} + onChange={(e) => { + setFormField({ ...formField, name: e.target.value }); + setErrors({ ...errors, name: '' }); + }} placeholder="Wallet Name" />
+ {errors.name &&
{errors.name}
}
@@ -226,12 +188,19 @@ const AddDialog = () => { Description* setFormField({ ...formField, description: e.target.value })} + onChange={(e) => { + setFormField({ ...formField, description: e.target.value }); + setErrors({ ...errors, description: '' }); + }} placeholder="Description" />
+ {errors.description && ( +
{errors.description}
+ )}
@@ -241,9 +210,12 @@ const AddDialog = () => {
+ {errors.status && ( +
{errors.status}
+ )}
@@ -261,9 +236,12 @@ const AddDialog = () => {
+ {errors.id_currency && ( +
{errors.id_currency}
+ )}
@@ -282,18 +263,37 @@ const AddDialog = () => { -
- {groups.map((group) => ( - - ))} +
+ + {errors.group && ( +
{errors.group}
+ )} + +
+
+ {groups.map((group) => ( +
+ handleGroupChange(group.id)} + /> + +
+ ))} +
+