import { apiConfig } from '@/config/api.config'; import { useManageWalletRuleContext } from '../hooks/useManageWalletRuleContext'; import { Alert, useDataGrid } from '@/components'; import { useCallApi } from '@/hooks'; import { useCallback, useEffect, useState } from 'react'; import { toast } from 'sonner'; import { Dialog, DialogBody, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, 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 { NumericFormat } from 'react-number-format'; import { doSaveLogActivity } from '@/actions/GlobalActions'; import { RefreshCw } from 'lucide-react'; interface GroupProps { 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_MASTERDATA = apiConfig.service_master_data; const AddDialog = () => { const { showAddDialog, handleAddDialog, selectedWalletRule } = useManageWalletRuleContext(); const { reload } = useDataGrid(); const { PostData, GetData } = useCallApi(); const [open, setOpen] = useState(false); const [alert, setAlert] = useState({ 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([]); const [isSubmitting, setIsSubmitting] = useState(false); const resetForm = () => { setFormField(initialState); setAlert({ show: false, message: '' }); }; const doCreateWalletRule = useCallback( async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); try { const response = await PostData(`${API_URL_WALLET}/dashboard/wallet_rule`, formField); if (response?.status) { handleAddDialog(false); toast.success('Success Create Wallet Rule'); reload(); const createActivity = { module: 'Manage Wallet Rule', description: `Create Wallet Rule => ${selectedWalletRule?.ID}`, action: 'C' }; doSaveLogActivity(createActivity); } else { toast.error('Failed Create Wallet Rule'); setAlert({ show: true, message: response?.message }); } } catch (error) { toast.error('Something went wrong, please try again.'); } finally { setIsSubmitting(false); } }, [formField] ); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if ( 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; } console.log(formField); doCreateWalletRule(e); setAlert({ show: false, message: '' }); }; const getWalletLists = async (sorting: any) => { try { const response = await GetData(`${API_URL_MASTERDATA}/wallet/list`, { limit: 100, page: 1, with_deleted: false, order_field: sorting[0].id, order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' }); // console.log('WALLET: ', response?.data); setWallets(response?.data.list); } catch (error) { console.error('Error fetching wallet', error); } }; const selectedWallet = wallets.find((wallet) => wallet.id === formField.id_wallet); const filteredGroups = selectedWallet ? selectedWallet.group : []; useEffect(() => { getWalletLists([{ id: 'wallets.name', desc: false }]); }, []); useEffect(() => { if (showAddDialog === false) { resetForm(); } }, [showAddDialog]); return ( handleAddDialog(open)}> Wallet Rule - Create
{alert.show && (

{alert.message}

)}
{ setFormField((prev) => ({ ...prev, max_transaction_per_day: values.floatValue !== undefined ? values.floatValue : '' })); }} placeholder="Enter Max Transaction Per Day" />
{ setFormField((prev) => ({ ...prev, balance_minimum: values.floatValue !== undefined ? values.floatValue : '' })); }} placeholder="Enter Balance Minimum" />
{ setFormField((prev) => ({ ...prev, balance_maximum: values.floatValue !== undefined ? values.floatValue : '' })); }} placeholder="Enter Balance Maximum" />
{ setFormField((prev) => ({ ...prev, credit_limit: values.floatValue !== undefined ? values.floatValue : '' })); }} placeholder="Enter Credit Limit" />
{ setFormField((prev) => ({ ...prev, monthly_limit: values.floatValue !== undefined ? values.floatValue : '' })); }} placeholder="Enter Monthly Limit" />
); }; export default AddDialog;