fixing transactiontype and transactionfee

This commit is contained in:
bagusajisaputroo
2025-04-14 20:09:58 +07:00
parent 18c1687ef7
commit d0ac8c55b4
8 changed files with 711 additions and 597 deletions

View File

@ -8,7 +8,14 @@ import {
SelectValue
} from '@/components/ui/select';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
} from '@/components/ui/command';
import {
Dialog,
DialogBody,
@ -96,7 +103,7 @@ const EditFeeDialog = () => {
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
if (showEditFeeDialog) {
setFormField(prevState => ({
setFormField((prevState) => ({
...prevState,
updated_by: parsedUser?.username,
updated_at: formattedTime
@ -116,9 +123,9 @@ const EditFeeDialog = () => {
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
const requiredFields = [
'name',
'name',
'description',
'period_start',
'period_end',
@ -131,38 +138,41 @@ const EditFeeDialog = () => {
'credit_to',
'credit_destination_account'
];
for (const field of requiredFields) {
if (formField[field as keyof typeof formField] === '') {
setAlert({
show: true,
message: `All required fields must be filled out. Missing: ${field.replace(/_/g, ' ')}`
setAlert({
show: true,
message: `All required fields must be filled out. Missing: ${field.replace(/_/g, ' ')}`
});
setIsSubmitting(false);
return;
}
}
if (formField.credit_to === 'I' && !formField.credit_destination) {
setAlert({
show: true,
message: 'Credit destination is required when Input Customer is selected'
setAlert({
show: true,
message: 'Credit destination is required when Input Customer is selected'
});
setIsSubmitting(false);
return;
}
setAlert({ show: false, message: '' });
const payload = { ...formField };
if (formField.credit_to !== 'I') {
payload.credit_destination = '00000000-0000-0000-0000-000000000000';
}
try {
const response = await PutData(`${API_URL}/transactionfees/update/${selectedTransferFee}`, payload);
const response = await PutData(
`${API_URL}/transactionfees/update/${selectedTransferFee}`,
payload
);
if (response?.status) {
toast.success('Successfully updated transfer fee');
reload();
@ -191,7 +201,7 @@ const EditFeeDialog = () => {
page: 1,
with_deleted: false,
order_field: 'Wallets.name',
order_direction: 'ASC',
order_direction: 'ASC'
};
try {
const response = await GetData(`${API_URL_MASTER_DATA}/wallet/list`, params);
@ -260,56 +270,58 @@ const EditFeeDialog = () => {
return dateString.split('T')[0];
};
const fetchTransactionFee = useCallback(async (id: string) => {
try {
const response = await GetData(`${API_URL}/transactionfees/getdata/${id}`, { });
if (response?.status) {
const fetchTransactionFee = useCallback(
async (id: string) => {
try {
const response = await GetData(`${API_URL}/transactionfees/getdata/${id}`, {});
setFormField({
...initialState,
name: response.data.name || '',
description: response.data.description || '',
transaction_type: response.data.transaction_type?.id || '',
minimum_amount: response.data.minimum_amount || 0,
maximum_amount: response.data.maximum_amount || 0,
period_start: formatDate(response.data.period_start),
period_end: formatDate(response.data.period_end),
deduct_amount: response.data.deduct_amount || 0,
deduct_percentage: response.data.deduct_percentage || 0,
fee_amount: response.data.fee_amount || 0,
priority: response.data.priority || '',
status: response.data.status || '',
status_include: response.data.status_include || '',
deduct_from: response.data.deduct_from||'',
deduct_from_account: response.data.deduct_from_account?.id || '',
credit_to: response.data.credit_to || '',
credit_destination: response.data.credit_destination?.id || '00000000-0000-0000-0000-000000000000',
credit_destination_account: response.data.credit_destination_account?.id || '',
updated_by: parsedUser?.username,
updated_at: new Date().toISOString().slice(0, 19).replace('T', ' ')
});
if (response?.status) {
setFormField({
...initialState,
name: response.data.name || '',
description: response.data.description || '',
transaction_type: response.data.transaction_type?.id || '',
minimum_amount: response.data.minimum_amount || 0,
maximum_amount: response.data.maximum_amount || 0,
period_start: formatDate(response.data.period_start),
period_end: formatDate(response.data.period_end),
deduct_amount: response.data.deduct_amount || 0,
deduct_percentage: response.data.deduct_percentage || 0,
fee_amount: response.data.fee_amount || 0,
priority: response.data.priority || '',
status: response.data.status || '',
status_include: response.data.status_include || '',
deduct_from: response.data.deduct_from || '',
deduct_from_account: response.data.deduct_from_account?.id || '',
credit_to: response.data.credit_to || '',
credit_destination:
response.data.credit_destination?.id || '00000000-0000-0000-0000-000000000000',
credit_destination_account: response.data.credit_destination_account?.id || '',
updated_by: parsedUser?.username,
updated_at: new Date().toISOString().slice(0, 19).replace('T', ' ')
});
}
} catch (error) {
console.error('Error fetching transaction fee details', error);
setAlert({ show: true, message: 'Failed to fetch transaction fee details' });
}
} catch (error) {
console.error('Error fetching transaction fee details', error);
setAlert({ show: true, message: 'Failed to fetch transaction fee details' });
}
}, [GetData, parsedUser?.username]);
},
[GetData, parsedUser?.username]
);
const hasFetchedRef = useRef(false);
useEffect(() => {
if (selectedTransferFee && showEditFeeDialog && !hasFetchedRef.current) {
fetchTransactionFee(selectedTransferFee);
hasFetchedRef.current = true;
}
if (!showEditFeeDialog) {
hasFetchedRef.current = false;
}
}, [selectedTransferFee, showEditFeeDialog, fetchTransactionFee]);
const handleCloseDialog = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
@ -317,11 +329,14 @@ const EditFeeDialog = () => {
};
return (
<Dialog open={showEditFeeDialog} onOpenChange={(open) => {
if (!open) {
handleCloseDialog();
}
}}>
<Dialog
open={showEditFeeDialog}
onOpenChange={(open) => {
if (!open) {
handleCloseDialog();
}
}}
>
<DialogContent className="container-fixed max-w-[1080px] flex flex-col p-5 overflow-hidden [&>button]:hidden">
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
@ -348,12 +363,14 @@ const EditFeeDialog = () => {
</Alert>
</div>
)}
<form action="" onSubmit={doUpdateTransferFee}>
<div className="card flex flex-col gap-5">
<div className="card-body grid gap-5 p-0">
<div className="w-full">
<label className="form-label">Transfer Fee Name <span className="text-red-500">*</span></label>
<label className="form-label">
Transfer Fee Name <span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
@ -364,9 +381,11 @@ const EditFeeDialog = () => {
}
/>
</div>
<div className="w-full">
<label className="form-label">Description <span className="text-red-500">*</span></label>
<label className="form-label">
Description <span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
@ -377,7 +396,7 @@ const EditFeeDialog = () => {
}
/>
</div>
<div className="w-full">
<label className="form-label">Minimum Amount</label>
<NumericFormat
@ -395,7 +414,7 @@ const EditFeeDialog = () => {
placeholder="Enter Minimum Amount"
/>
</div>
<div className="w-full">
<label className="form-label">Maximum Amount</label>
<NumericFormat
@ -413,9 +432,11 @@ const EditFeeDialog = () => {
placeholder="Enter Maximum Amount"
/>
</div>
<div className="w-full">
<label className="form-label">Period Start <span className="text-red-500">*</span></label>
<label className="form-label">
Period Start <span className="text-red-500">*</span>
</label>
<Input
className="input"
type="date"
@ -426,9 +447,11 @@ const EditFeeDialog = () => {
}
/>
</div>
<div className="w-full">
<label className="form-label">Period End <span className="text-red-500">*</span></label>
<label className="form-label">
Period End <span className="text-red-500">*</span>
</label>
<Input
className="input"
type="date"
@ -439,7 +462,7 @@ const EditFeeDialog = () => {
}
/>
</div>
<div className="w-full">
<label className="form-label">Deduct Amount</label>
<NumericFormat
@ -457,7 +480,7 @@ const EditFeeDialog = () => {
placeholder="Enter Deduct Amount"
/>
</div>
<div className="w-full">
<label className="form-label">Deduct Percentage</label>
<NumericFormat
@ -475,7 +498,7 @@ const EditFeeDialog = () => {
placeholder="Enter Deduct Percentage"
/>
</div>
<div className="w-full">
<label className="form-label">Fee Amount</label>
<NumericFormat
@ -493,9 +516,11 @@ const EditFeeDialog = () => {
placeholder="Enter Fee Amount"
/>
</div>
<div className="w-full">
<label className="form-label">Deduct From <span className="text-red-500">*</span></label>
<label className="form-label">
Deduct From <span className="text-red-500">*</span>
</label>
<Select
value={formField.deduct_from}
onValueChange={(value) => setFormField({ ...formField, deduct_from: value })}
@ -509,12 +534,16 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">Deduct From Destination <span className="text-red-500">*</span></label>
<label className="form-label">
Deduct From Destination <span className="text-red-500">*</span>
</label>
<Select
value={formField.deduct_from_account}
onValueChange={(value) => setFormField({ ...formField, deduct_from_account: value })}
onValueChange={(value) =>
setFormField({ ...formField, deduct_from_account: value })
}
>
<SelectTrigger>
<SelectValue placeholder="Select Wallet" />
@ -528,16 +557,21 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">Credit To <span className="text-red-500">*</span></label>
<label className="form-label">
Credit To <span className="text-red-500">*</span>
</label>
<Select
value={formField.credit_to}
onValueChange={(value) => setFormField({
...formField,
credit_to: value,
credit_destination: value === 'I' ? '' : '00000000-0000-0000-0000-000000000000'
})}
onValueChange={(value) =>
setFormField({
...formField,
credit_to: value,
credit_destination:
value === 'I' ? '' : '00000000-0000-0000-0000-000000000000'
})
}
>
<SelectTrigger>
<SelectValue placeholder="Select Credit To" />
@ -549,13 +583,17 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
{formField.credit_to === 'I' && (
<div className="w-full">
<label className="form-label">Credit Destination <span className="text-red-500">*</span></label>
<label className="form-label">
Credit Destination <span className="text-red-500">*</span>
</label>
<Select
value={formField.credit_destination}
onValueChange={(value) => setFormField({ ...formField, credit_destination: value })}
onValueChange={(value) =>
setFormField({ ...formField, credit_destination: value })
}
>
<SelectTrigger>
<SelectValue placeholder="Select Customer" />
@ -570,12 +608,16 @@ const EditFeeDialog = () => {
</Select>
</div>
)}
<div className="w-full">
<label className="form-label">Credit Destination Account <span className="text-red-500">*</span></label>
<label className="form-label">
Credit Destination Account <span className="text-red-500">*</span>
</label>
<Select
value={formField.credit_destination_account}
onValueChange={(value) => setFormField({ ...formField, credit_destination_account: value })}
onValueChange={(value) =>
setFormField({ ...formField, credit_destination_account: value })
}
>
<SelectTrigger>
<SelectValue placeholder="Select Wallet" />
@ -589,9 +631,11 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">Transaction Type ID <span className="text-red-500">*</span></label>
<label className="form-label">
Transaction Type ID <span className="text-red-500">*</span>
</label>
<Select
value={formField.transaction_type}
onValueChange={(transaction_type) =>
@ -610,9 +654,11 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">Status <span className="text-red-500">*</span></label>
<label className="form-label">
Status <span className="text-red-500">*</span>
</label>
<Select
value={formField.status}
onValueChange={(value) => setFormField({ ...formField, status: value })}
@ -626,9 +672,11 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">Status Include <span className="text-red-500">*</span></label>
<label className="form-label">
Status Include <span className="text-red-500">*</span>
</label>
<Select
value={formField.status_include}
onValueChange={(value) => setFormField({ ...formField, status_include: value })}
@ -642,9 +690,11 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">Priority <span className="text-red-500">*</span></label>
<label className="form-label">
Priority <span className="text-red-500">*</span>
</label>
<Select
value={formField.priority}
onValueChange={(value) => setFormField({ ...formField, priority: value })}
@ -658,7 +708,7 @@ const EditFeeDialog = () => {
</SelectContent>
</Select>
</div>
<div className="flex justify-end pt-2.5 gap-5">
<Button variant={'default'} type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Saving...' : 'Save Changes'}
@ -673,4 +723,4 @@ const EditFeeDialog = () => {
);
};
export { EditFeeDialog };
export { EditFeeDialog };