fixing transactionfee

This commit is contained in:
bagusajisaputroo
2025-04-14 12:07:54 +07:00
parent ab7a8bab9e
commit 3bbea4bff7
3 changed files with 125 additions and 52 deletions

View File

@ -7,7 +7,8 @@ import {
SelectTrigger,
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 {
Dialog,
DialogBody,
@ -59,6 +60,7 @@ const EditFeeDialog = () => {
const [transactionTypes, setTransactionTypes] = useState<TransactionTypeProps[]>([]);
const parsedUser = getAuth()?.user;
const [customers, setCustomers] = useState<CustomerProps[]>([]);
const [open, setOpen] = useState(false);
const [alert, setAlert] = useState({
show: false,
message: ''
@ -94,24 +96,22 @@ const EditFeeDialog = () => {
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
if (showEditFeeDialog) {
setFormField({
...formField,
setFormField(prevState => ({
...prevState,
updated_by: parsedUser?.username,
updated_at: formattedTime
});
}));
}
}, [showEditFeeDialog]);
const resetForm = () => {
if (selectedTransferFee) {
// Re-fetch the current data to reset the form to original values
fetchTransactionFee(selectedTransferFee);
} else {
setFormField(initialState);
}
};
/* actions */
const doUpdateTransferFee = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
@ -257,15 +257,10 @@ const EditFeeDialog = () => {
const fetchTransactionFee = useCallback(async (id: string) => {
try {
const response = await GetData(`${API_URL}/transactionfees/getdata/${id}`, { id });
const response = await GetData(`${API_URL}/transactionfees/getdata/${id}`, { });
if (response?.status) {
let creditTo = '';
if (response.data.credit_destination?.id === '00000000-0000-0000-0000-000000000000') {
creditTo = 'D';
} else {
creditTo = 'I';
}
setFormField({
...initialState,
@ -282,23 +277,21 @@ const EditFeeDialog = () => {
priority: response.data.priority || '',
status: response.data.status || '',
status_include: response.data.status_include || '',
deduct_from: 'S',
deduct_from: response.data.deduct_from||'',
deduct_from_account: response.data.deduct_from_account?.id || '',
credit_to: creditTo,
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', ' ')
});
}
console.log(response)
} catch (error) {
console.error('Error fetching transaction fee details', error);
setAlert({ show: true, message: 'Failed to fetch transaction fee details' });
}
}, [GetData, parsedUser?.username]); // only use username if that's all you need
}, [GetData, parsedUser?.username]);
// This flag ensures it only fetches once per open
const hasFetchedRef = useRef(false);
useEffect(() => {
@ -307,15 +300,23 @@ const EditFeeDialog = () => {
hasFetchedRef.current = true;
}
// Reset fetch flag if dialog is closed
if (!showEditFeeDialog) {
hasFetchedRef.current = false;
}
}, [selectedTransferFee, showEditFeeDialog, fetchTransactionFee]);
const handleCloseDialog = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
handleEditFeeDialog(false, null);
};
return (
<Dialog open={showEditFeeDialog} onOpenChange={(open) => handleEditFeeDialog(open, null)}>
<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>
@ -328,7 +329,7 @@ const EditFeeDialog = () => {
</div>
<div
className="cursor-pointer hover:opacity-100 opacity-50"
onClick={() => handleEditFeeDialog(false, null)}
onClick={handleCloseDialog}
>
<KeenIcon icon="cross" className="text-1.5xl" />
</div>
@ -654,13 +655,6 @@ const EditFeeDialog = () => {
</div>
<div className="flex justify-end pt-2.5 gap-5">
<Button
variant={'outline'}
type="button"
onClick={resetForm}
>
Reset
</Button>
<Button variant={'default'} type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Saving...' : 'Save Changes'}
</Button>