fixing transaction_type field on update transactionfee

This commit is contained in:
bagusajisaputroo
2025-04-15 00:31:55 +07:00
parent 49b19629bd
commit f407780355

View File

@ -7,15 +7,6 @@ import {
SelectTrigger, SelectTrigger,
SelectValue SelectValue
} from '@/components/ui/select'; } from '@/components/ui/select';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
} from '@/components/ui/command';
import { import {
Dialog, Dialog,
DialogBody, DialogBody,
@ -33,6 +24,8 @@ import { toast } from 'sonner';
import { useCallApi } from '@/hooks'; import { useCallApi } from '@/hooks';
import { doSaveLogActivity } from '@/actions/GlobalActions'; import { doSaveLogActivity } from '@/actions/GlobalActions';
import { getAuth } from '@/auth'; import { getAuth } from '@/auth';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command';
const API_URL = apiConfig.service_transaction; const API_URL = apiConfig.service_transaction;
const API_URL_MASTER_DATA = apiConfig.service_master_data; const API_URL_MASTER_DATA = apiConfig.service_master_data;
@ -41,14 +34,12 @@ const API_URL_CUSTOMER = apiConfig.service_customer;
interface WalletProps { interface WalletProps {
id: string; id: string;
name: string; name: string;
description?: string;
} }
interface CustomerProps { interface CustomerProps {
id: string; id: string;
username: string; username: string;
msisdn: string; msisdn: string;
fullname?: string;
} }
interface TransactionTypeProps { interface TransactionTypeProps {
@ -61,18 +52,32 @@ const EditFeeDialog = () => {
const { showEditFeeDialog, handleEditFeeDialog, selectedTransferFee } = const { showEditFeeDialog, handleEditFeeDialog, selectedTransferFee } =
useManageTransferFeeContext(); useManageTransferFeeContext();
const { reload } = useDataGrid(); const { reload } = useDataGrid();
const [wallets, setWallets] = useState<WalletProps[]>([]);
const { GetData, PutData } = useCallApi(); const { GetData, PutData } = useCallApi();
const [isSubmitting, setIsSubmitting] = useState(false);
const [transactionTypes, setTransactionTypes] = useState<TransactionTypeProps[]>([]);
const parsedUser = getAuth()?.user; const parsedUser = getAuth()?.user;
const [wallets, setWallets] = useState<WalletProps[]>([]);
const [customers, setCustomers] = useState<CustomerProps[]>([]); const [customers, setCustomers] = useState<CustomerProps[]>([]);
const [transactionTypes, setTransactionTypes] = useState<TransactionTypeProps[]>([]);
const [transactionTypeName, setTransactionTypeName] = useState('');
const customersWithNames = customers.map((customer) => ({
id: customer.id,
name: customer.username
}));
const [customerSearchTerm, setCustomerSearchTerm] = useState('');
const [showCustomerSearch, setShowCustomerSearch] = useState(false);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [alert, setAlert] = useState({ const [alert, setAlert] = useState({
show: false, show: false,
message: '' message: ''
}); });
// Loading states
const [isLoadingTransferFee, setIsLoadingTransferFee] = useState(false);
const [isLoadingTransactionType, setIsLoadingTransactionType] = useState(false);
const [isLoadingWallets, setIsLoadingWallets] = useState(false);
const [isLoadingCustomers, setIsLoadingCustomers] = useState(false);
const initialState = { const initialState = {
name: '', name: '',
description: '', description: '',
@ -109,7 +114,7 @@ const EditFeeDialog = () => {
updated_at: formattedTime updated_at: formattedTime
})); }));
} }
}, [showEditFeeDialog]); }, [showEditFeeDialog, parsedUser?.username]);
const resetForm = () => { const resetForm = () => {
if (selectedTransferFee) { if (selectedTransferFee) {
@ -178,10 +183,11 @@ const EditFeeDialog = () => {
reload(); reload();
handleEditFeeDialog(false, null); handleEditFeeDialog(false, null);
const createActivity = { const createActivity = {
module: 'Manage Transfer Type', module: 'Manage Transfer Fee',
description: `Edit Transfer Type => ${selectedTransferFee}`, description: `Edit Transfer Fee => ${formField.name}`,
action: 'U' action: 'U'
}; };
doSaveLogActivity(createActivity);
} else { } else {
setAlert({ show: true, message: response?.message || 'Failed to update transfer fee' }); setAlert({ show: true, message: response?.message || 'Failed to update transfer fee' });
} }
@ -196,6 +202,7 @@ const EditFeeDialog = () => {
); );
const fetchWallets = useCallback(async () => { const fetchWallets = useCallback(async () => {
setIsLoadingWallets(true);
const params = { const params = {
limit: 100, limit: 100,
page: 1, page: 1,
@ -213,6 +220,8 @@ const EditFeeDialog = () => {
} catch (error) { } catch (error) {
console.error('Error fetching wallets', error); console.error('Error fetching wallets', error);
setWallets([]); setWallets([]);
} finally {
setIsLoadingWallets(false);
} }
}, [GetData]); }, [GetData]);
@ -225,6 +234,7 @@ const EditFeeDialog = () => {
if (!showEditFeeDialog) return; if (!showEditFeeDialog) return;
const getCustomerList = async (sorting: any) => { const getCustomerList = async (sorting: any) => {
setIsLoadingCustomers(true);
try { try {
sorting = sorting.length === 0 ? [{ id: 'name', desc: false }] : sorting; sorting = sorting.length === 0 ? [{ id: 'name', desc: false }] : sorting;
const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, { const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, {
@ -237,16 +247,19 @@ const EditFeeDialog = () => {
setCustomers(response?.data.list || []); setCustomers(response?.data.list || []);
} catch (error) { } catch (error) {
console.error('Error fetching customers', error); console.error('Error fetching customers', error);
} finally {
setIsLoadingCustomers(false);
} }
}; };
getCustomerList([{ id: 'msisdn', desc: false }]); getCustomerList([{ id: 'id', desc: false }]);
}, [showEditFeeDialog, GetData]); }, [showEditFeeDialog, GetData]);
useEffect(() => { useEffect(() => {
if (!showEditFeeDialog) return; if (!showEditFeeDialog) return;
const getTransactionTypeList = async (sorting: any) => { const getTransactionTypeList = async (sorting: any) => {
setIsLoadingTransactionType(true);
try { try {
sorting = sorting.length === 0 ? [{ id: 'name', desc: false }] : sorting; sorting = sorting.length === 0 ? [{ id: 'name', desc: false }] : sorting;
const response = await GetData(`${API_URL}/transactiontype/list`, { const response = await GetData(`${API_URL}/transactiontype/list`, {
@ -259,6 +272,8 @@ const EditFeeDialog = () => {
setTransactionTypes(response?.data.list || []); setTransactionTypes(response?.data.list || []);
} catch (error) { } catch (error) {
console.error('Error fetching transaction types', error); console.error('Error fetching transaction types', error);
} finally {
setIsLoadingTransactionType(false);
} }
}; };
@ -272,6 +287,7 @@ const EditFeeDialog = () => {
const fetchTransactionFee = useCallback( const fetchTransactionFee = useCallback(
async (id: string) => { async (id: string) => {
setIsLoadingTransferFee(true);
try { try {
const response = await GetData(`${API_URL}/transactionfees/getdata/${id}`, {}); const response = await GetData(`${API_URL}/transactionfees/getdata/${id}`, {});
@ -300,10 +316,16 @@ const EditFeeDialog = () => {
updated_by: parsedUser?.username, updated_by: parsedUser?.username,
updated_at: new Date().toISOString().slice(0, 19).replace('T', ' ') updated_at: new Date().toISOString().slice(0, 19).replace('T', ' ')
}); });
if (response.data.transaction_type?.name) {
setTransactionTypeName(response.data.transaction_type.name);
}
} }
} catch (error) { } catch (error) {
console.error('Error fetching transaction fee details', error); console.error('Error fetching transaction fee details', error);
setAlert({ show: true, message: 'Failed to fetch transaction fee details' }); setAlert({ show: true, message: 'Failed to fetch transaction fee details' });
} finally {
setIsLoadingTransferFee(false);
} }
}, },
[GetData, parsedUser?.username] [GetData, parsedUser?.username]
@ -328,6 +350,37 @@ const EditFeeDialog = () => {
handleEditFeeDialog(false, null); handleEditFeeDialog(false, null);
}; };
const renderSelectWithLoading = (
value: string,
onChangeHandler: (value: string) => void,
options: { id: string; name: string }[] | null,
placeholder: string,
isLoading: boolean
) => {
return (
<Select value={value} onValueChange={onChangeHandler} disabled={isLoading}>
<SelectTrigger>
{isLoading ? (
<div className="flex items-center">
<div className="animate-pulse bg-gray-200 h-4 w-24 rounded"></div>
<span className="ml-2">Loading...</span>
</div>
) : (
<SelectValue placeholder={placeholder} />
)}
</SelectTrigger>
<SelectContent>
{options &&
options.map((option) => (
<SelectItem value={option.id} key={option.id}>
{option.name}
</SelectItem>
))}
</SelectContent>
</Select>
);
};
return ( return (
<Dialog <Dialog
open={showEditFeeDialog} open={showEditFeeDialog}
@ -364,9 +417,50 @@ const EditFeeDialog = () => {
</div> </div>
)} )}
{isLoadingTransferFee ? (
<div className="flex flex-col items-center justify-center p-8">
<div className="animate-pulse flex space-x-4 w-full">
<div className="flex-1 space-y-4 py-1">
<div className="h-4 bg-gray-200 rounded w-3/4"></div>
<div className="space-y-2">
<div className="h-4 bg-gray-200 rounded"></div>
<div className="h-4 bg-gray-200 rounded w-5/6"></div>
</div>
</div>
</div>
<p className="mt-4 text-gray-500">Loading transfer fee details...</p>
</div>
) : (
<form action="" onSubmit={doUpdateTransferFee}> <form action="" onSubmit={doUpdateTransferFee}>
<div className="card flex flex-col gap-5"> <div className="card flex flex-col gap-5">
<div className="card-body grid gap-5 p-0"> <div className="card-body grid gap-5 p-0">
<div className="w-full">
<label className="form-label">
Transaction Type ID <span className="text-red-500">*</span>
</label>
<div className="relative">
<Input
className="input bg-gray-100"
type="text"
value={isLoadingTransactionType ? '' : transactionTypeName}
readOnly
/>
{isLoadingTransactionType && (
<div className="absolute inset-0 flex items-center justify-start bg-gray-100 px-3">
<div className="flex items-center">
<div className="animate-pulse bg-gray-200 h-4 w-24 rounded"></div>
<span className="ml-2 text-gray-500">Loading transaction type...</span>
</div>
</div>
)}
<input
type="hidden"
name="transaction_type"
value={formField.transaction_type}
/>
</div>
</div>
<div className="w-full"> <div className="w-full">
<label className="form-label"> <label className="form-label">
Transfer Fee Name <span className="text-red-500">*</span> Transfer Fee Name <span className="text-red-500">*</span>
@ -539,23 +633,13 @@ const EditFeeDialog = () => {
<label className="form-label"> <label className="form-label">
Deduct From Destination <span className="text-red-500">*</span> Deduct From Destination <span className="text-red-500">*</span>
</label> </label>
<Select {renderSelectWithLoading(
value={formField.deduct_from_account} formField.deduct_from_account,
onValueChange={(value) => (value) => setFormField({ ...formField, deduct_from_account: value }),
setFormField({ ...formField, deduct_from_account: value }) wallets,
} 'Select Wallet',
> isLoadingWallets
<SelectTrigger> )}
<SelectValue placeholder="Select Wallet" />
</SelectTrigger>
<SelectContent>
{wallets.map((wallet) => (
<SelectItem value={wallet.id} key={wallet.id}>
{wallet.name || wallet.description}
</SelectItem>
))}
</SelectContent>
</Select>
</div> </div>
<div className="w-full"> <div className="w-full">
@ -589,70 +673,62 @@ const EditFeeDialog = () => {
<label className="form-label"> <label className="form-label">
Credit Destination <span className="text-red-500">*</span> Credit Destination <span className="text-red-500">*</span>
</label> </label>
<Select <Popover open={open} onOpenChange={setOpen}>
value={formField.credit_destination} <PopoverTrigger asChild>
onValueChange={(value) => <button
setFormField({ ...formField, credit_destination: value }) type="button"
} className="input w-full text-left"
> >
<SelectTrigger> {customers.find(
<SelectValue placeholder="Select Customer" /> (customer) => customer.id === formField.credit_destination
</SelectTrigger> )?.username || 'Select Customer'}
<SelectContent> </button>
</PopoverTrigger>
<PopoverContent className="w-full p-0">
<Command className="w-full">
<CommandInput
placeholder="Search Customer..."
className="w-full border-none px-3 py-2"
/>
<CommandList className="max-h-[250px] overflow-y-auto">
<CommandEmpty>No Customer found.</CommandEmpty>
<CommandGroup>
{customers.map((customer) => ( {customers.map((customer) => (
<SelectItem value={customer.id} key={customer.id}> <CommandItem
{customer.fullname || `${customer.username} - ${customer.msisdn}`} key={customer.id}
</SelectItem> value={customer.username}
onSelect={() => {
setFormField({
...formField,
credit_destination: customer.id
});
setOpen(false);
}}
>
{customer.username}
</CommandItem>
))} ))}
</SelectContent> </CommandGroup>
</Select> </CommandList>
</Command>
</PopoverContent>
</Popover>
</div> </div>
)} )}
<div className="w-full"> <div className="w-full">
<label className="form-label"> <label className="form-label">
Credit Destination Account <span className="text-red-500">*</span> Credit Destination Account <span className="text-red-500">*</span>
</label> </label>
<Select {renderSelectWithLoading(
value={formField.credit_destination_account} formField.credit_destination_account,
onValueChange={(value) => (value) => setFormField({ ...formField, credit_destination_account: value }),
setFormField({ ...formField, credit_destination_account: value }) wallets,
} 'Select Wallet',
> isLoadingWallets
<SelectTrigger> )}
<SelectValue placeholder="Select Wallet" />
</SelectTrigger>
<SelectContent>
{wallets.map((wallet) => (
<SelectItem value={wallet.id} key={wallet.id}>
{wallet.name || wallet.description}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="w-full">
<label className="form-label">
Transaction Type ID <span className="text-red-500">*</span>
</label>
<Select
value={formField.transaction_type}
onValueChange={(transaction_type) =>
setFormField((prev) => ({ ...prev, transaction_type }))
}
>
<SelectTrigger>
<SelectValue placeholder="Select Transaction Type" />
</SelectTrigger>
<SelectContent>
{transactionTypes.map((transactiontype) => (
<SelectItem value={transactiontype.id} key={transactiontype.id}>
{transactiontype.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div> </div>
<div className="w-full"> <div className="w-full">
@ -710,13 +786,31 @@ const EditFeeDialog = () => {
</div> </div>
<div className="flex justify-end pt-2.5 gap-5"> <div className="flex justify-end pt-2.5 gap-5">
<Button variant={'default'} type="submit" disabled={isSubmitting}> <Button
variant={'outline'}
type="button"
onClick={resetForm}
>
Reset
</Button>
<Button
variant={'default'}
type="submit"
disabled={
isSubmitting ||
isLoadingTransferFee ||
isLoadingTransactionType ||
isLoadingWallets ||
isLoadingCustomers
}
>
{isSubmitting ? 'Saving...' : 'Save Changes'} {isSubmitting ? 'Saving...' : 'Save Changes'}
</Button> </Button>
</div> </div>
</div> </div>
</div> </div>
</form> </form>
)}
</DialogBody> </DialogBody>
</DialogContent> </DialogContent>
</Dialog> </Dialog>