feat create wallet rule

This commit is contained in:
Wikzyy
2025-03-26 00:49:21 +07:00
parent 8a593c7b5e
commit 0f5d190c9e
3 changed files with 369 additions and 4 deletions

View File

@ -0,0 +1,362 @@
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';
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 AddDialog = () => {
const { showAddDialog, handleAddDialog } = useManageWalletRuleContext();
const { reload } = useDataGrid();
const { PostData, GetData } = useCallApi();
const [open, setOpen] = useState(false);
const [alert, setAlert] = useState({
show: false,
message: ''
});
const initialState = {
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 [wallets, setWallets] = useState<WalletProps[]>([]);
const resetForm = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
};
const doCreateWalletRule = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const response = await PostData(`${API_URL_WALLET}/dashboard/wallet_rule`, formField);
if (response?.status) {
handleAddDialog(false);
toast.success('Success Create Wallet Rule');
reload();
} else {
toast.error('Failed Create Wallet Rule');
setAlert({ show: true, message: 'Failed Create Wallet Rule' });
}
},
[formField]
);
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (
formField.id_group.trim() === '' ||
formField.max_transaction_per_day === 0 ||
formField.balance_minimum === 0 ||
formField.balance_maximum === 0 ||
formField.credit_limit === 0 ||
formField.monthly_limit === 0 ||
formField.status.trim() === ''
) {
setAlert({ show: true, message: 'Please fill in all required fields.' });
return;
}
console.log(formField);
doCreateWalletRule(e);
setAlert({ show: false, message: '' });
};
const getGroupLists = async (sorting: any) => {
try {
const response = await GetData(`${API_URL_WALLET}/dashboard/group`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
console.log('GROUPS: ', response?.data);
setGroups(response?.data.list);
} catch (error) {
console.error('Error fetching groups', error);
}
};
const getWalletLists = async (sorting: any) => {
try {
const response = await GetData(`${API_URL_WALLET}/dashboard/wallet`, {
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);
}
};
useEffect(() => {
getWalletLists([{ id: 'name', desc: false }]);
getGroupLists([{ id: 'name', desc: false }]);
}, []);
useEffect(() => {
if (showAddDialog === false) {
resetForm();
}
}, [showAddDialog]);
return (
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
<DialogHeader>
<DialogTitle>Wallet Rule - Create</DialogTitle>
<DialogDescription></DialogDescription>
</DialogHeader>
<DialogBody className="scrollable">
<div className="flex flex-col">
{alert.show && (
<Alert variant="danger">
<h3>{alert.message}</h3>
</Alert>
)}
<form onSubmit={handleSubmit}>
<div className="card-body grid gap-5">
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Wallet<span className="text-red-500">*</span>
</label>
<Select
value={formField.id_wallet}
onValueChange={(value) =>
setFormField({ ...formField, id_wallet: value })
}
>
<SelectTrigger>
<SelectValue placeholder="Select Transaction Type" />
</SelectTrigger>
<SelectContent>
{wallets.map((wallet) => (
<SelectItem key={wallet.ID} value={wallet.ID}>
{wallet.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Group<span className="text-red-500">*</span>
</label>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className="input col-span-5 text-left">
{groups.find((group) => group.ID === formField.id_group)?.name ||
'Select Groups'}
</button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder="Search Groups..." />
<CommandList>
<CommandEmpty>No Group found.</CommandEmpty>
<CommandGroup>
{groups.map((group) => (
<CommandItem
key={group.ID}
value={group.ID}
onSelect={() => {
setFormField({
...formField,
id_group: group.ID
});
setOpen(false);
}}
>
{group.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Max Transaction Per Day<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
value={formField.max_transaction_per_day}
onChange={(e) =>
setFormField({
...formField,
max_transaction_per_day: parseInt(e.target.value)
})
}
/>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Balance Minimum<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
value={formField.balance_minimum}
onChange={(e) =>
setFormField({ ...formField, balance_minimum: parseInt(e.target.value) })
}
/>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Balance Maximum<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
value={formField.balance_maximum}
onChange={(e) =>
setFormField({ ...formField, balance_maximum: parseInt(e.target.value) })
}
/>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Credit Limit<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
value={formField.credit_limit}
onChange={(e) =>
setFormField({ ...formField, credit_limit: parseInt(e.target.value) })
}
/>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Monthly Limit<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
value={formField.monthly_limit}
onChange={(e) =>
setFormField({ ...formField, monthly_limit: parseInt(e.target.value) })
}
/>
</div>
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Status<span className="text-red-500">*</span>
</label>
<Select
value={formField.status}
onValueChange={(value) => setFormField({ ...formField, status: value })}
>
<SelectTrigger>
<SelectValue placeholder="Select Status" />
</SelectTrigger>
<SelectContent>
<SelectItem value="Y">Active</SelectItem>
<SelectItem value="N">Inactive</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<div className="flex justify-end gap-5">
<Button type="button" variant="outline" onClick={resetForm}>
Reset
</Button>
<Button variant="default">Create</Button>
</div>
</div>
</form>
</div>
</DialogBody>
</DialogContent>
</Dialog>
);
};
export default AddDialog;

View File

@ -1,17 +1,17 @@
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
import { Button } from '@/components/ui/button';
import { useManageProductsContext } from '../../products/hooks/useManageProductsContext';
import { useManageWalletRuleContext } from '../hooks/useManageWalletRuleContext';
const ListToolbar = () => {
const { table, reload } = useDataGrid();
const { handleAddDialog } = useManageProductsContext();
const { handleAddDialog } = useManageWalletRuleContext();
return (
<div className="card-header flex-wrap gap-2 border-b-0 px-5">
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
<div className="flex justify-between w-full items-center">
<div className="flex w-[50%] gap-3 items-center">
<label className="input input-sm w-1/3">
{/* <label className="input input-sm w-1/3">
<KeenIcon icon="magnifier" />
<input
type="text"
@ -19,7 +19,7 @@ const ListToolbar = () => {
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
onChange={(event) => table.getColumn('name')?.setFilterValue(event.target.value)}
/>
</label>
</label> */}
{/* <DefaultTooltip title={'Filter'} placement={'top'}>
<Button
variant="outline"