fix alert empty validation on manage wallet rule
This commit is contained in:
81
src/pages/master/walletRule/Types.ts
Normal file
81
src/pages/master/walletRule/Types.ts
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
|
export interface WalletRuleProps {
|
||||||
|
ID: string;
|
||||||
|
id_wallet: string;
|
||||||
|
id_group: string;
|
||||||
|
max_transaction_per_day: number | null;
|
||||||
|
balance_minimum: number | null;
|
||||||
|
balance_maximum: number | null;
|
||||||
|
credit_limit: number | null;
|
||||||
|
monthly_limit: number | null;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const initialStateWalletRule: {
|
||||||
|
id_wallet: string;
|
||||||
|
id_group: string;
|
||||||
|
max_transaction_per_day: string | number | null;
|
||||||
|
balance_minimum: string | number | null;
|
||||||
|
balance_maximum: string | number | null;
|
||||||
|
credit_limit: string | number | null;
|
||||||
|
monthly_limit: string | number | null;
|
||||||
|
status: string;
|
||||||
|
} = {
|
||||||
|
id_wallet: '',
|
||||||
|
id_group: '',
|
||||||
|
max_transaction_per_day: null,
|
||||||
|
balance_minimum: null,
|
||||||
|
balance_maximum: null,
|
||||||
|
credit_limit: null,
|
||||||
|
monthly_limit: null,
|
||||||
|
status: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface GroupProps {
|
||||||
|
ID: string;
|
||||||
|
is_bank: string;
|
||||||
|
name: string;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WalletProps {
|
||||||
|
ID: string;
|
||||||
|
name: string;
|
||||||
|
id_currency: string;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const validateFormsWalletRule = (
|
||||||
|
formField: typeof initialStateWalletRule,
|
||||||
|
setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
|
||||||
|
) => {
|
||||||
|
const requiredFields = [
|
||||||
|
{ key: 'id_wallet', label: 'Wallet' },
|
||||||
|
{ key: 'id_group', label: 'Group' },
|
||||||
|
{ key: 'max_transaction_per_day', label: 'Max Transaction Per Day' },
|
||||||
|
{ key: 'balance_minimum', label: 'Balance Minimum' },
|
||||||
|
{ key: 'balance_maximum', label: 'Balance Maximum' },
|
||||||
|
{ key: 'credit_limit', label: 'Credit Limit' },
|
||||||
|
{ key: 'monthly_limit', label: 'Monthly Limit' },
|
||||||
|
{ key: 'status', label: 'Status' }
|
||||||
|
];
|
||||||
|
|
||||||
|
const newErrors: Record<string, string> = {};
|
||||||
|
let isValid = true;
|
||||||
|
|
||||||
|
requiredFields.forEach(({ key, label }) => {
|
||||||
|
if (
|
||||||
|
formField[key as keyof typeof formField] === '' ||
|
||||||
|
formField[key as keyof typeof formField] === null ||
|
||||||
|
formField[key as keyof typeof formField] === undefined
|
||||||
|
) {
|
||||||
|
newErrors[key] = `${label} is required`;
|
||||||
|
toast.error(`${label} is required`);
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setErrors(newErrors);
|
||||||
|
return isValid;
|
||||||
|
};
|
||||||
@ -33,28 +33,8 @@ import {
|
|||||||
import { NumericFormat } from 'react-number-format';
|
import { NumericFormat } from 'react-number-format';
|
||||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||||
import { RefreshCw } from 'lucide-react';
|
import { RefreshCw } from 'lucide-react';
|
||||||
|
import { initialStateWalletRule, validateFormsWalletRule } from '../Types';
|
||||||
interface GroupProps {
|
import { WalletProps } from '../../wallet/Types';
|
||||||
ID: string;
|
|
||||||
is_bank: string;
|
|
||||||
name: string;
|
|
||||||
status: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface WalletGroupProps {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
description: string;
|
|
||||||
status: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface WalletProps {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
id_currency: string;
|
|
||||||
status: string;
|
|
||||||
group: WalletGroupProps[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const API_URL_WALLET = apiConfig.service_wallet;
|
const API_URL_WALLET = apiConfig.service_wallet;
|
||||||
const API_URL_MASTERDATA = apiConfig.service_master_data;
|
const API_URL_MASTERDATA = apiConfig.service_master_data;
|
||||||
@ -63,37 +43,14 @@ const AddDialog = () => {
|
|||||||
const { showAddDialog, handleAddDialog, selectedWalletRule } = useManageWalletRuleContext();
|
const { showAddDialog, handleAddDialog, selectedWalletRule } = useManageWalletRuleContext();
|
||||||
const { reload } = useDataGrid();
|
const { reload } = useDataGrid();
|
||||||
const { PostData, GetData } = useCallApi();
|
const { PostData, GetData } = useCallApi();
|
||||||
const [open, setOpen] = useState(false);
|
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||||
const [alert, setAlert] = useState({
|
const [formField, setFormField] = useState(initialStateWalletRule);
|
||||||
show: false,
|
|
||||||
message: ''
|
|
||||||
});
|
|
||||||
const initialState: {
|
|
||||||
id_wallet: string;
|
|
||||||
id_group: string;
|
|
||||||
max_transaction_per_day: string | number | null;
|
|
||||||
balance_minimum: string | number | null;
|
|
||||||
balance_maximum: string | number | null;
|
|
||||||
credit_limit: string | number | null;
|
|
||||||
monthly_limit: string | number | null;
|
|
||||||
status: string;
|
|
||||||
} = {
|
|
||||||
id_wallet: '',
|
|
||||||
id_group: '',
|
|
||||||
max_transaction_per_day: null,
|
|
||||||
balance_minimum: null,
|
|
||||||
balance_maximum: null,
|
|
||||||
credit_limit: null,
|
|
||||||
monthly_limit: null,
|
|
||||||
status: ''
|
|
||||||
};
|
|
||||||
const [formField, setFormField] = useState(initialState);
|
|
||||||
const [wallets, setWallets] = useState<WalletProps[]>([]);
|
const [wallets, setWallets] = useState<WalletProps[]>([]);
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
setFormField(initialState);
|
setFormField(initialStateWalletRule);
|
||||||
setAlert({ show: false, message: '' });
|
setErrors({});
|
||||||
};
|
};
|
||||||
|
|
||||||
const doCreateWalletRule = useCallback(
|
const doCreateWalletRule = useCallback(
|
||||||
@ -117,8 +74,7 @@ const AddDialog = () => {
|
|||||||
|
|
||||||
doSaveLogActivity(createActivity);
|
doSaveLogActivity(createActivity);
|
||||||
} else {
|
} else {
|
||||||
toast.error('Failed Create Wallet Rule');
|
toast.error(response?.message);
|
||||||
setAlert({ show: true, message: response?.message });
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error('Something went wrong, please try again.');
|
toast.error('Something went wrong, please try again.');
|
||||||
@ -132,23 +88,11 @@ const AddDialog = () => {
|
|||||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (
|
if (!validateFormsWalletRule(formField, setErrors)) {
|
||||||
formField.id_group.trim() === '' ||
|
|
||||||
formField.status.trim() === '' ||
|
|
||||||
formField.id_wallet.trim() === '' ||
|
|
||||||
formField.max_transaction_per_day === null ||
|
|
||||||
formField.balance_minimum === null ||
|
|
||||||
formField.balance_maximum === null ||
|
|
||||||
formField.credit_limit === null ||
|
|
||||||
formField.monthly_limit === null
|
|
||||||
) {
|
|
||||||
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(formField);
|
|
||||||
doCreateWalletRule(e);
|
doCreateWalletRule(e);
|
||||||
setAlert({ show: false, message: '' });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getWalletLists = async (sorting: any) => {
|
const getWalletLists = async (sorting: any) => {
|
||||||
@ -160,8 +104,6 @@ const AddDialog = () => {
|
|||||||
order_field: sorting[0].id,
|
order_field: sorting[0].id,
|
||||||
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
||||||
});
|
});
|
||||||
|
|
||||||
// console.log('WALLET: ', response?.data);
|
|
||||||
setWallets(response?.data.list);
|
setWallets(response?.data.list);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching wallet', error);
|
console.error('Error fetching wallet', error);
|
||||||
@ -190,12 +132,6 @@ const AddDialog = () => {
|
|||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogBody className="scrollable">
|
<DialogBody className="scrollable">
|
||||||
<div className="flex flex-col">
|
<div className="flex flex-col">
|
||||||
{alert.show && (
|
|
||||||
<Alert variant="danger">
|
|
||||||
<h3>{alert.message}</h3>
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<form onSubmit={handleSubmit}>
|
<form onSubmit={handleSubmit}>
|
||||||
<div className="card-body grid gap-5">
|
<div className="card-body grid gap-5">
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
@ -203,23 +139,29 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Wallet<span className="text-red-500">*</span>
|
Wallet<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<Select
|
<div className="w-full">
|
||||||
value={formField.id_wallet}
|
<Select
|
||||||
onValueChange={(value) =>
|
value={formField.id_wallet}
|
||||||
setFormField({ ...formField, id_wallet: value, id_group: '' })
|
onValueChange={(value) => {
|
||||||
}
|
setFormField({ ...formField, id_wallet: value, id_group: '' });
|
||||||
>
|
setErrors({ ...errors, id_group: '' });
|
||||||
<SelectTrigger>
|
}}
|
||||||
<SelectValue placeholder="Select Wallet Type" />
|
>
|
||||||
</SelectTrigger>
|
<SelectTrigger className={errors.id_wallet ? 'border-red-500' : ''}>
|
||||||
<SelectContent>
|
<SelectValue placeholder="Select Wallet Type" />
|
||||||
{wallets.map((wallet) => (
|
</SelectTrigger>
|
||||||
<SelectItem key={wallet.id} value={wallet.id}>
|
<SelectContent>
|
||||||
{wallet.name}
|
{wallets.map((wallet) => (
|
||||||
</SelectItem>
|
<SelectItem key={wallet.id} value={wallet.id}>
|
||||||
))}
|
{wallet.name}
|
||||||
</SelectContent>
|
</SelectItem>
|
||||||
</Select>
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
{errors.id_wallet && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.id_wallet}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -228,26 +170,34 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Group<span className="text-red-500">*</span>
|
Group<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<Select
|
<div className="w-full">
|
||||||
value={formField.id_group}
|
<Select
|
||||||
onValueChange={(value) => setFormField({ ...formField, id_group: value })}
|
value={formField.id_group}
|
||||||
disabled={filteredGroups.length === 0}
|
onValueChange={(value) => {
|
||||||
>
|
setFormField({ ...formField, id_group: value });
|
||||||
<SelectTrigger>
|
setErrors({ ...errors, id_group: '' });
|
||||||
<SelectValue placeholder="Select Group Type" />
|
}}
|
||||||
</SelectTrigger>
|
disabled={filteredGroups.length === 0}
|
||||||
<SelectContent>
|
>
|
||||||
{filteredGroups.length > 0 ? (
|
<SelectTrigger className={errors.id_group ? 'border-red-500' : ''}>
|
||||||
filteredGroups.map((group) => (
|
<SelectValue placeholder="Select Group Type" />
|
||||||
<SelectItem key={group.id} value={group.id}>
|
</SelectTrigger>
|
||||||
{group.name}
|
<SelectContent>
|
||||||
</SelectItem>
|
{filteredGroups.length > 0 ? (
|
||||||
))
|
filteredGroups.map((group) => (
|
||||||
) : (
|
<SelectItem key={group.id} value={group.id}>
|
||||||
<SelectItem value="empty">No groups available</SelectItem>
|
{group.name}
|
||||||
)}
|
</SelectItem>
|
||||||
</SelectContent>
|
))
|
||||||
</Select>
|
) : (
|
||||||
|
<SelectItem value="empty">No groups available</SelectItem>
|
||||||
|
)}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
{errors.id_group && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.id_group}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -256,21 +206,29 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Max Transaction Per Day<span className="text-red-500">*</span>
|
Max Transaction Per Day<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<NumericFormat
|
<div className="w-full">
|
||||||
className="input"
|
<NumericFormat
|
||||||
value={formField.max_transaction_per_day ?? ''}
|
className={`input ${errors.max_transaction_per_day ? 'border-red-500' : ''}`}
|
||||||
thousandSeparator="."
|
value={formField.max_transaction_per_day ?? null}
|
||||||
decimalSeparator=","
|
thousandSeparator="."
|
||||||
allowNegative={false}
|
decimalSeparator=","
|
||||||
onValueChange={(values) => {
|
allowNegative={false}
|
||||||
setFormField((prev) => ({
|
onValueChange={(values) => {
|
||||||
...prev,
|
setFormField((prev) => ({
|
||||||
max_transaction_per_day:
|
...prev,
|
||||||
values.floatValue !== undefined ? values.floatValue : ''
|
max_transaction_per_day:
|
||||||
}));
|
values.floatValue !== undefined ? values.floatValue : ''
|
||||||
}}
|
}));
|
||||||
placeholder="Enter Max Transaction Per Day"
|
setErrors({ ...errors, max_transaction_per_day: '' });
|
||||||
/>
|
}}
|
||||||
|
placeholder="Enter Max Transaction Per Day"
|
||||||
|
/>
|
||||||
|
{errors.max_transaction_per_day && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">
|
||||||
|
{errors.max_transaction_per_day}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -279,20 +237,27 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Balance Minimum<span className="text-red-500">*</span>
|
Balance Minimum<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<NumericFormat
|
<div className="w-full">
|
||||||
className="input"
|
<NumericFormat
|
||||||
value={formField.balance_minimum ?? ''}
|
className={`input ${errors.balance_minimum ? 'border-red-500' : ''}`}
|
||||||
thousandSeparator="."
|
value={formField.balance_minimum ?? ''}
|
||||||
decimalSeparator=","
|
thousandSeparator="."
|
||||||
allowNegative={false}
|
decimalSeparator=","
|
||||||
onValueChange={(values) => {
|
allowNegative={false}
|
||||||
setFormField((prev) => ({
|
onValueChange={(values) => {
|
||||||
...prev,
|
setFormField((prev) => ({
|
||||||
balance_minimum: values.floatValue !== undefined ? values.floatValue : ''
|
...prev,
|
||||||
}));
|
balance_minimum:
|
||||||
}}
|
values.floatValue !== undefined ? values.floatValue : ''
|
||||||
placeholder="Enter Balance Minimum"
|
}));
|
||||||
/>
|
setErrors({ ...errors, balance_minimum: '' });
|
||||||
|
}}
|
||||||
|
placeholder="Enter Balance Minimum"
|
||||||
|
/>
|
||||||
|
{errors.balance_minimum && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.balance_minimum}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -301,20 +266,27 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Balance Maximum<span className="text-red-500">*</span>
|
Balance Maximum<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<NumericFormat
|
<div className="w-full">
|
||||||
className="input"
|
<NumericFormat
|
||||||
value={formField.balance_maximum ?? ''}
|
className={`input ${errors.balance_maximum ? 'border-red-500' : ''}`}
|
||||||
thousandSeparator="."
|
value={formField.balance_maximum ?? ''}
|
||||||
decimalSeparator=","
|
thousandSeparator="."
|
||||||
allowNegative={false}
|
decimalSeparator=","
|
||||||
onValueChange={(values) => {
|
allowNegative={false}
|
||||||
setFormField((prev) => ({
|
onValueChange={(values) => {
|
||||||
...prev,
|
setFormField((prev) => ({
|
||||||
balance_maximum: values.floatValue !== undefined ? values.floatValue : ''
|
...prev,
|
||||||
}));
|
balance_maximum:
|
||||||
}}
|
values.floatValue !== undefined ? values.floatValue : ''
|
||||||
placeholder="Enter Balance Maximum"
|
}));
|
||||||
/>
|
setErrors({ ...errors, balance_maximum: '' });
|
||||||
|
}}
|
||||||
|
placeholder="Enter Balance Maximum"
|
||||||
|
/>
|
||||||
|
{errors.balance_maximum && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.balance_maximum}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -323,20 +295,26 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Credit Limit<span className="text-red-500">*</span>
|
Credit Limit<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<NumericFormat
|
<div className="w-full">
|
||||||
className="input"
|
<NumericFormat
|
||||||
value={formField.credit_limit ?? ''}
|
className={`input ${errors.credit_limit ? 'border-red-500' : ''}`}
|
||||||
thousandSeparator="."
|
value={formField.credit_limit ?? ''}
|
||||||
decimalSeparator=","
|
thousandSeparator="."
|
||||||
allowNegative={false}
|
decimalSeparator=","
|
||||||
onValueChange={(values) => {
|
allowNegative={false}
|
||||||
setFormField((prev) => ({
|
onValueChange={(values) => {
|
||||||
...prev,
|
setFormField((prev) => ({
|
||||||
credit_limit: values.floatValue !== undefined ? values.floatValue : ''
|
...prev,
|
||||||
}));
|
credit_limit: values.floatValue !== undefined ? values.floatValue : ''
|
||||||
}}
|
}));
|
||||||
placeholder="Enter Credit Limit"
|
setErrors({ ...errors, credit_limit: '' });
|
||||||
/>
|
}}
|
||||||
|
placeholder="Enter Credit Limit"
|
||||||
|
/>
|
||||||
|
{errors.credit_limit && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.credit_limit}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -345,20 +323,26 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Monthly Limit<span className="text-red-500">*</span>
|
Monthly Limit<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<NumericFormat
|
<div className="w-full">
|
||||||
className="input"
|
<NumericFormat
|
||||||
value={formField.monthly_limit ?? ''}
|
className={`input ${errors.monthly_limit ? 'border-red-500' : ''}`}
|
||||||
thousandSeparator="."
|
value={formField.monthly_limit ?? ''}
|
||||||
decimalSeparator=","
|
thousandSeparator="."
|
||||||
allowNegative={false}
|
decimalSeparator=","
|
||||||
onValueChange={(values) => {
|
allowNegative={false}
|
||||||
setFormField((prev) => ({
|
onValueChange={(values) => {
|
||||||
...prev,
|
setFormField((prev) => ({
|
||||||
monthly_limit: values.floatValue !== undefined ? values.floatValue : ''
|
...prev,
|
||||||
}));
|
monthly_limit: values.floatValue !== undefined ? values.floatValue : ''
|
||||||
}}
|
}));
|
||||||
placeholder="Enter Monthly Limit"
|
setErrors({ ...errors, monthly_limit: '' });
|
||||||
/>
|
}}
|
||||||
|
placeholder="Enter Monthly Limit"
|
||||||
|
/>
|
||||||
|
{errors.monthly_limit && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.monthly_limit}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -367,18 +351,26 @@ const AddDialog = () => {
|
|||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Status<span className="text-red-500">*</span>
|
Status<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<Select
|
<div className="w-full">
|
||||||
value={formField.status}
|
<Select
|
||||||
onValueChange={(value) => setFormField({ ...formField, status: value })}
|
value={formField.status}
|
||||||
>
|
onValueChange={(value) => {
|
||||||
<SelectTrigger>
|
setFormField({ ...formField, status: value });
|
||||||
<SelectValue placeholder="Select Status" />
|
setErrors({ ...errors, status: '' });
|
||||||
</SelectTrigger>
|
}}
|
||||||
<SelectContent>
|
>
|
||||||
<SelectItem value="Y">Active</SelectItem>
|
<SelectTrigger className={errors.status ? 'border-red-500' : ''}>
|
||||||
<SelectItem value="N">Inactive</SelectItem>
|
<SelectValue placeholder="Select Status" />
|
||||||
</SelectContent>
|
</SelectTrigger>
|
||||||
</Select>
|
<SelectContent>
|
||||||
|
<SelectItem value="Y">Active</SelectItem>
|
||||||
|
<SelectItem value="N">Inactive</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
{errors.status && (
|
||||||
|
<div className="text-red-500 text-xs mt-1">{errors.status}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@ -33,20 +33,7 @@ import { Button } from '@/components/ui/button';
|
|||||||
import { NumericFormat } from 'react-number-format';
|
import { NumericFormat } from 'react-number-format';
|
||||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||||
import { RefreshCw } from 'lucide-react';
|
import { RefreshCw } from 'lucide-react';
|
||||||
|
import { GroupProps, initialStateWalletRule, validateFormsWalletRule, WalletProps } from '../Types';
|
||||||
interface GroupProps {
|
|
||||||
ID: string;
|
|
||||||
is_bank: string;
|
|
||||||
name: string;
|
|
||||||
status: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface WalletProps {
|
|
||||||
ID: string;
|
|
||||||
name: string;
|
|
||||||
id_currency: string;
|
|
||||||
status: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const API_URL_WALLET = apiConfig.service_wallet;
|
const API_URL_WALLET = apiConfig.service_wallet;
|
||||||
|
|
||||||
@ -54,37 +41,15 @@ const EditDialog = () => {
|
|||||||
const { showEditDialog, handleEditDialog, selectedWalletRule } = useManageWalletRuleContext();
|
const { showEditDialog, handleEditDialog, selectedWalletRule } = useManageWalletRuleContext();
|
||||||
const { GetData, PutData } = useCallApi();
|
const { GetData, PutData } = useCallApi();
|
||||||
const { reload } = useDataGrid();
|
const { reload } = useDataGrid();
|
||||||
const [alert, setAlert] = useState({
|
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||||
show: false,
|
const [formField, setFormField] = useState(initialStateWalletRule);
|
||||||
message: ''
|
|
||||||
});
|
|
||||||
const initialState: {
|
|
||||||
id_wallet: string;
|
|
||||||
id_group: string;
|
|
||||||
max_transaction_per_day: string | number | null;
|
|
||||||
balance_minimum: string | number | null;
|
|
||||||
balance_maximum: string | number | null;
|
|
||||||
credit_limit: string | number | null;
|
|
||||||
monthly_limit: string | number | null;
|
|
||||||
status: string;
|
|
||||||
} = {
|
|
||||||
id_wallet: '',
|
|
||||||
id_group: '',
|
|
||||||
max_transaction_per_day: null,
|
|
||||||
balance_minimum: null,
|
|
||||||
balance_maximum: null,
|
|
||||||
credit_limit: null,
|
|
||||||
monthly_limit: null,
|
|
||||||
status: ''
|
|
||||||
};
|
|
||||||
const [formField, setFormField] = useState(initialState);
|
|
||||||
const [groups, setGroups] = useState<GroupProps[]>([]);
|
const [groups, setGroups] = useState<GroupProps[]>([]);
|
||||||
const [wallets, setWallets] = useState<WalletProps[]>([]);
|
const [wallets, setWallets] = useState<WalletProps[]>([]);
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
setFormField(initialState);
|
setFormField(initialStateWalletRule);
|
||||||
setAlert({ show: false, message: '' });
|
setErrors({});
|
||||||
};
|
};
|
||||||
|
|
||||||
const doUpdateWalletRule = useCallback(
|
const doUpdateWalletRule = useCallback(
|
||||||
@ -111,8 +76,7 @@ const EditDialog = () => {
|
|||||||
|
|
||||||
doSaveLogActivity(createActivity);
|
doSaveLogActivity(createActivity);
|
||||||
} else {
|
} else {
|
||||||
toast.error('Failed Update Wallet Rule');
|
toast.error(response?.message);
|
||||||
setAlert({ show: true, message: response?.message });
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error('Something went wrong, please try again');
|
toast.error('Something went wrong, please try again');
|
||||||
@ -126,14 +90,11 @@ const EditDialog = () => {
|
|||||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (formField.id_group.trim() === '' || formField.status.trim() === '') {
|
if (!validateFormsWalletRule(formField, setErrors)) {
|
||||||
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(formField);
|
|
||||||
doUpdateWalletRule(e);
|
doUpdateWalletRule(e);
|
||||||
setAlert({ show: false, message: '' });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getGroupLists = async (sorting: any) => {
|
const getGroupLists = async (sorting: any) => {
|
||||||
@ -146,7 +107,6 @@ const EditDialog = () => {
|
|||||||
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
||||||
});
|
});
|
||||||
|
|
||||||
// console.log('GROUPS: ', response?.data);
|
|
||||||
setGroups(response?.data.list);
|
setGroups(response?.data.list);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching groups', error);
|
console.error('Error fetching groups', error);
|
||||||
@ -163,7 +123,6 @@ const EditDialog = () => {
|
|||||||
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
||||||
});
|
});
|
||||||
|
|
||||||
// console.log('WALLET: ', response?.data);
|
|
||||||
setWallets(response?.data.list);
|
setWallets(response?.data.list);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching wallet', error);
|
console.error('Error fetching wallet', error);
|
||||||
@ -215,7 +174,7 @@ const EditDialog = () => {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}, [selectedWalletRule]);
|
}, [selectedWalletRule]);
|
||||||
// console.log(selectedWalletRule);
|
|
||||||
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">
|
||||||
@ -225,12 +184,6 @@ const EditDialog = () => {
|
|||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogBody className="scrollable">
|
<DialogBody className="scrollable">
|
||||||
<div className="flex flex-col">
|
<div className="flex flex-col">
|
||||||
{alert.show && (
|
|
||||||
<Alert variant="danger">
|
|
||||||
<h3>{alert.message}</h3>
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<form onSubmit={handleSubmit}>
|
<form onSubmit={handleSubmit}>
|
||||||
<div className="card-body grid gap-5">
|
<div className="card-body grid gap-5">
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
|
|||||||
@ -5,18 +5,7 @@ import { useCallApi } from '@/hooks';
|
|||||||
import { ColumnDef } from '@tanstack/react-table';
|
import { ColumnDef } from '@tanstack/react-table';
|
||||||
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
||||||
import ListToolbar from '../blocks/ListToolbar';
|
import ListToolbar from '../blocks/ListToolbar';
|
||||||
|
import { WalletRuleProps } from '../Types';
|
||||||
interface WalletRuleProps {
|
|
||||||
ID: string;
|
|
||||||
id_wallet: string;
|
|
||||||
id_group: string;
|
|
||||||
max_transaction_per_day: number | null;
|
|
||||||
balance_minimum: number | null;
|
|
||||||
balance_maximum: number | null;
|
|
||||||
credit_limit: number | null;
|
|
||||||
monthly_limit: number | null;
|
|
||||||
status: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ContextProps {
|
interface ContextProps {
|
||||||
walletRules: WalletRuleProps[];
|
walletRules: WalletRuleProps[];
|
||||||
|
|||||||
Reference in New Issue
Block a user