add, edit, delete, visualize conversion on masterdata
This commit is contained in:
@ -1,6 +1,12 @@
|
|||||||
import { Container, DataGridInner } from '@/components';
|
import { Container, DataGridInner } from '@/components';
|
||||||
import { ManageConversionContextProvider } from './hooks/ManageConversionContext';
|
import { ManageConversionContextProvider } from './hooks/ManageConversionContext';
|
||||||
import { Breadcrumbs, Link } from '@mui/material';
|
import { Breadcrumbs, Link } from '@mui/material';
|
||||||
|
import AddDialog from './blocks/AddDialog';
|
||||||
|
import EditDialog from './blocks/EditDialog';
|
||||||
|
import { Delete } from 'lucide-react';
|
||||||
|
import DeleteDialog from './blocks/DeleteDialog';
|
||||||
|
|
||||||
|
// import EditDialog from './blocks/EditDialog';
|
||||||
|
|
||||||
const ConversionMaster = () => {
|
const ConversionMaster = () => {
|
||||||
return (
|
return (
|
||||||
@ -24,6 +30,10 @@ const ConversionMaster = () => {
|
|||||||
<div className="grid gap-5 lg:gap-7.5">
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
<DataGridInner />
|
<DataGridInner />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<AddDialog />
|
||||||
|
<EditDialog/>
|
||||||
|
<DeleteDialog/>
|
||||||
</Container>
|
</Container>
|
||||||
</ManageConversionContextProvider>
|
</ManageConversionContextProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -0,0 +1,287 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth } from '@/auth';
|
||||||
|
import React, { useCallback, useEffect, useRef, 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 { NumericFormat } from 'react-number-format';
|
||||||
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
||||||
|
import {
|
||||||
|
Command,
|
||||||
|
CommandEmpty,
|
||||||
|
CommandGroup,
|
||||||
|
CommandInput,
|
||||||
|
CommandItem,
|
||||||
|
CommandList
|
||||||
|
} from '@/components/ui/command';
|
||||||
|
import { set } from 'date-fns';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue
|
||||||
|
} from '@/components/ui/select';
|
||||||
|
import { useManageConversionContext } from '../hooks/useManageConversionContext';
|
||||||
|
interface CurrencyProps {
|
||||||
|
ID: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_wallet;
|
||||||
|
|
||||||
|
const AddDialog = () => {
|
||||||
|
const parentRef = useRef<any | null>(null);
|
||||||
|
const { showAddDialog, handleAddDialog, selectedConversion } = useManageConversionContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PostData, GetData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
status: '',
|
||||||
|
id_currency_origin: '',
|
||||||
|
id_currency_destination: '',
|
||||||
|
buy: 0,
|
||||||
|
sell: 0,
|
||||||
|
created_by: '',
|
||||||
|
created_at: ''
|
||||||
|
};
|
||||||
|
const [formField, setFormField] = useState(initialState);
|
||||||
|
const created_time = new Date();
|
||||||
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setFormField(initialState);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const doCreateConversion = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PostData(`${API_URL}/dashboard/conversion`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
resetForm();
|
||||||
|
handleAddDialog(false);
|
||||||
|
toast.success('Success Create Conversion');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Error Create Conversion');
|
||||||
|
setAlert({ show: true, message: 'Failed to create Conversion. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const doFetchCurrency = async (sorting: any) => {
|
||||||
|
try {
|
||||||
|
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
||||||
|
const response = await GetData(`${API_URL}/dashboard/currency/`, {
|
||||||
|
limit: 1000,
|
||||||
|
page: 1,
|
||||||
|
with_deleted: false,
|
||||||
|
order_field: sorting[0].id,
|
||||||
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
||||||
|
});
|
||||||
|
|
||||||
|
setCurrencies(response?.data.list);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching currency', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (
|
||||||
|
formField.id_currency_origin === '' ||
|
||||||
|
formField.id_currency_destination === '' ||
|
||||||
|
formField.buy === 0 ||
|
||||||
|
formField.sell === 0 ||
|
||||||
|
formField.status === ''
|
||||||
|
) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doCreateConversion(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showAddDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
created_by: parsedUser.username,
|
||||||
|
created_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
doFetchCurrency([{ 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>Conversion - Create</DialogTitle>
|
||||||
|
<DialogDescription></DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogBody ref={parentRef}>
|
||||||
|
<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">
|
||||||
|
<label className="form-label">
|
||||||
|
Currency Origin <span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Select
|
||||||
|
value={formField.id_currency_origin}
|
||||||
|
onValueChange={(id_currency_origin) =>
|
||||||
|
setFormField((prev) => ({ ...prev, id_currency_origin }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select Currency" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{currencies.map((currency, idx) => (
|
||||||
|
<SelectItem value={currency.ID} key={currency.ID}>
|
||||||
|
{currency.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Currency Destination <span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Select
|
||||||
|
value={formField.id_currency_destination}
|
||||||
|
onValueChange={(id_currency_destination) =>
|
||||||
|
setFormField((prev) => ({ ...prev, id_currency_destination }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select Currency" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{currencies.map((currency, idx) => (
|
||||||
|
<SelectItem value={currency.ID} key={currency.ID}>
|
||||||
|
{currency.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Buy<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<NumericFormat
|
||||||
|
className="input"
|
||||||
|
value={formField.buy}
|
||||||
|
thousandSeparator="."
|
||||||
|
decimalSeparator=","
|
||||||
|
allowNegative={false}
|
||||||
|
onValueChange={(values) => {
|
||||||
|
setFormField((prev) => ({
|
||||||
|
...prev,
|
||||||
|
buy: values.floatValue || 0
|
||||||
|
}));
|
||||||
|
}}
|
||||||
|
placeholder="Enter Buy"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Sell
|
||||||
|
<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<NumericFormat
|
||||||
|
className="input"
|
||||||
|
value={formField.sell}
|
||||||
|
thousandSeparator="."
|
||||||
|
decimalSeparator=","
|
||||||
|
allowNegative={false}
|
||||||
|
onValueChange={(values) => {
|
||||||
|
setFormField((prev) => ({
|
||||||
|
...prev,
|
||||||
|
sell: values.floatValue || 0
|
||||||
|
}));
|
||||||
|
}}
|
||||||
|
placeholder="Enter Sell"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Status
|
||||||
|
<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<div className="grow">
|
||||||
|
<Select
|
||||||
|
value={formField.status}
|
||||||
|
onValueChange={(value) =>
|
||||||
|
setFormField((prev) => ({ ...prev, status: value }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select" />
|
||||||
|
</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">Save Changes</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</DialogBody>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AddDialog;
|
||||||
|
|||||||
79
src/pages/master/conversion/blocks/DeleteDialog.tsx
Normal file
79
src/pages/master/conversion/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useManageConversionContext } from '../hooks/useManageConversionContext';
|
||||||
|
import { useCallback, useState } from 'react';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle
|
||||||
|
} from '@/components/ui/dialog';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { DialogDescription } from '@radix-ui/react-dialog';
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_wallet;
|
||||||
|
|
||||||
|
const DeleteDialog = () => {
|
||||||
|
const { showDeleteDialog, handleDeleteDialog, selectedConversion } = useManageConversionContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { DeleteData } = useCallApi();
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const doDeleteConversion = useCallback(async () => {
|
||||||
|
if (!selectedConversion) {
|
||||||
|
toast.error('No Conversion selected');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(selectedConversion);
|
||||||
|
const response = await DeleteData(
|
||||||
|
`${API_URL}/dashboard/conversion/${selectedConversion}`,
|
||||||
|
{ id: selectedConversion }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
handleDeleteDialog(false, null);
|
||||||
|
toast.success('Success Delete Conversion');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
setAlert({ show: true, message: response?.message });
|
||||||
|
toast.error('Failed Delete Conversion');
|
||||||
|
}
|
||||||
|
}, [selectedConversion, DeleteData, handleDeleteDialog, reload]);
|
||||||
|
console.log(selectedConversion);
|
||||||
|
return (
|
||||||
|
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
|
||||||
|
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||||
|
<DialogHeader className="p-0 border-0 block">
|
||||||
|
<DialogTitle></DialogTitle>
|
||||||
|
<DialogDescription></DialogDescription>
|
||||||
|
<Alert variant="warning">
|
||||||
|
<h3 className="text-lg">Are you sure?</h3>
|
||||||
|
<span className="text-sm">You will delete this data!</span>
|
||||||
|
</Alert>
|
||||||
|
{alert.show && (
|
||||||
|
<Alert variant="danger">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||||
|
<Button variant="outline" onClick={() => handleDeleteDialog(false, null)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button variant="destructive" onClick={doDeleteConversion}>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteDialog;
|
||||||
299
src/pages/master/conversion/blocks/EditDialog.tsx
Normal file
299
src/pages/master/conversion/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,299 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth } from '@/auth';
|
||||||
|
import React, { useCallback, useEffect, useRef, 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 { NumericFormat } from 'react-number-format';
|
||||||
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
||||||
|
import {
|
||||||
|
Command,
|
||||||
|
CommandEmpty,
|
||||||
|
CommandGroup,
|
||||||
|
CommandInput,
|
||||||
|
CommandItem,
|
||||||
|
CommandList
|
||||||
|
} from '@/components/ui/command';
|
||||||
|
import { set } from 'date-fns';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue
|
||||||
|
} from '@/components/ui/select';
|
||||||
|
import { useManageConversionContext } from '../hooks/useManageConversionContext';
|
||||||
|
interface CurrencyProps {
|
||||||
|
ID: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_wallet;
|
||||||
|
|
||||||
|
const EditDialog = () => {
|
||||||
|
const parentRef = useRef<any | null>(null);
|
||||||
|
const { showEditDialog, handleEditDialog, selectedConversion } = useManageConversionContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PostData, GetData, PutData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
status: '',
|
||||||
|
id_currency_origin: '',
|
||||||
|
id_currency_destination: '',
|
||||||
|
buy: 0,
|
||||||
|
sell: 0,
|
||||||
|
created_by: '',
|
||||||
|
created_at: ''
|
||||||
|
};
|
||||||
|
const [formField, setFormField] = useState(initialState);
|
||||||
|
const updated_time = new Date();
|
||||||
|
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setFormField(initialState);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const doUpdateConversion = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if(!showEditDialog) return;
|
||||||
|
const response = await PutData(`${API_URL}/dashboard/conversion/${selectedConversion}`,{
|
||||||
|
...formField
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
resetForm();
|
||||||
|
handleEditDialog(false, null);
|
||||||
|
toast.success('Success Update Conversion');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Error Create Conversion');
|
||||||
|
setAlert({ show: true, message: 'Failed to Update Conversion. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const doGetCurrency = async (sorting: any) => {
|
||||||
|
if (!showEditDialog)return;
|
||||||
|
try {
|
||||||
|
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
||||||
|
const response = await GetData(`${API_URL}/dashboard/currency/`, {
|
||||||
|
limit: 1000,
|
||||||
|
page: 1,
|
||||||
|
with_deleted: false,
|
||||||
|
order_field: sorting[0].id,
|
||||||
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
|
||||||
|
});
|
||||||
|
|
||||||
|
setCurrencies(response?.data.list);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching currency', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const doGetConversionById = useCallback(async (id: string) => {
|
||||||
|
const response = await GetData(`${API_URL}/dashboard/conversion/${id}`, { id });
|
||||||
|
console.log('Transaction Type: ', response?.data);
|
||||||
|
if (response?.status) {
|
||||||
|
setFormField((prev) => ({
|
||||||
|
...prev,
|
||||||
|
status: response.data.status,
|
||||||
|
id_currency_origin: response.data.id_currency_origin,
|
||||||
|
id_currency_destination: response.data.id_currency_destination,
|
||||||
|
buy: response.data.buy,
|
||||||
|
sell: response.data.sell,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
// console.log('form fieldd Transaction Type: ', formField);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedConversion) {
|
||||||
|
doGetConversionById(selectedConversion);
|
||||||
|
}
|
||||||
|
}, [selectedConversion]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showEditDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
created_by: parsedUser.username,
|
||||||
|
created_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showEditDialog) {
|
||||||
|
doGetCurrency([{ id: 'name', desc: false }]);
|
||||||
|
}
|
||||||
|
}, [showEditDialog]);
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showEditDialog === false) {
|
||||||
|
resetForm();
|
||||||
|
}
|
||||||
|
}, [showEditDialog]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open,null)}>
|
||||||
|
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Conversion - Update</DialogTitle>
|
||||||
|
<DialogDescription></DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogBody ref={parentRef}>
|
||||||
|
<div className="flex flex-col">
|
||||||
|
{alert.show && (
|
||||||
|
<Alert variant="danger">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<form onSubmit={doUpdateConversion}>
|
||||||
|
<div className="card-body grid gap-5">
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Currency Origin <span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Select
|
||||||
|
value={formField.id_currency_origin}
|
||||||
|
onValueChange={(id_currency_origin) =>
|
||||||
|
setFormField((prev) => ({ ...prev, id_currency_origin }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select Currency" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{currencies.map((currency, idx) => (
|
||||||
|
<SelectItem value={currency.ID} key={currency.ID}>
|
||||||
|
{currency.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Currency Destination <span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Select
|
||||||
|
value={formField.id_currency_destination}
|
||||||
|
onValueChange={(id_currency_destination) =>
|
||||||
|
setFormField((prev) => ({ ...prev, id_currency_destination }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select Currency" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{currencies.map((currency, idx) => (
|
||||||
|
<SelectItem value={currency.ID} key={currency.ID}>
|
||||||
|
{currency.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Buy<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<NumericFormat
|
||||||
|
className="input"
|
||||||
|
value={formField.buy}
|
||||||
|
thousandSeparator="."
|
||||||
|
decimalSeparator=","
|
||||||
|
allowNegative={false}
|
||||||
|
onValueChange={(values) => {
|
||||||
|
setFormField((prev) => ({
|
||||||
|
...prev,
|
||||||
|
buy: values.floatValue || 0
|
||||||
|
}));
|
||||||
|
}}
|
||||||
|
placeholder="Enter Buy"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Sell
|
||||||
|
<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<NumericFormat
|
||||||
|
className="input"
|
||||||
|
value={formField.sell}
|
||||||
|
thousandSeparator="."
|
||||||
|
decimalSeparator=","
|
||||||
|
allowNegative={false}
|
||||||
|
onValueChange={(values) => {
|
||||||
|
setFormField((prev) => ({
|
||||||
|
...prev,
|
||||||
|
sell: values.floatValue || 0
|
||||||
|
}));
|
||||||
|
}}
|
||||||
|
placeholder="Enter Sell"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="w-full">
|
||||||
|
<label className="form-label">
|
||||||
|
Status
|
||||||
|
<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<div className="grow">
|
||||||
|
<Select
|
||||||
|
value={formField.status}
|
||||||
|
onValueChange={(value) =>
|
||||||
|
setFormField((prev) => ({ ...prev, status: value }))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select" />
|
||||||
|
</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">Save Changes</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</DialogBody>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditDialog;
|
||||||
@ -11,7 +11,7 @@ const ListToolbar = () => {
|
|||||||
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
|
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
|
||||||
<div className="flex justify-between w-full items-center">
|
<div className="flex justify-between w-full items-center">
|
||||||
<div className="flex w-[50%] gap-3 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" />
|
<KeenIcon icon="magnifier" />
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
@ -19,7 +19,7 @@ const ListToolbar = () => {
|
|||||||
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
|
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
|
||||||
onChange={(event) => table.getColumn('name')?.setFilterValue(event.target.value)}
|
onChange={(event) => table.getColumn('name')?.setFilterValue(event.target.value)}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label> */}
|
||||||
{/* <DefaultTooltip title={'Filter'} placement={'top'}>
|
{/* <DefaultTooltip title={'Filter'} placement={'top'}>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
|
|||||||
@ -3,79 +3,123 @@ import { Toaster } from '@/components/ui/sonner';
|
|||||||
import { apiConfig } from '@/config/api.config';
|
import { apiConfig } from '@/config/api.config';
|
||||||
import { useCallApi } from '@/hooks';
|
import { useCallApi } from '@/hooks';
|
||||||
import { ColumnDef } from '@tanstack/react-table';
|
import { ColumnDef } from '@tanstack/react-table';
|
||||||
import { createContext, useCallback, useMemo, useState } from 'react';
|
import { createContext, useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import ListToolbar from '../blocks/ListToolbar';
|
import ListToolbar from '../blocks/ListToolbar';
|
||||||
|
|
||||||
interface ConversionProps {
|
interface CurrencyProps {
|
||||||
id: string;
|
ID: string;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Conversion {
|
||||||
|
id: string;
|
||||||
|
id_currency_origin: string;
|
||||||
|
id_currency_destination: string;
|
||||||
|
buy: number;
|
||||||
|
sell: number;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface ContextProps {
|
interface ContextProps {
|
||||||
conversion: ConversionProps[];
|
|
||||||
showAddDialog: boolean;
|
|
||||||
handleAddDialog: (show: boolean) => void;
|
|
||||||
showEditDialog: boolean;
|
showEditDialog: boolean;
|
||||||
handleEditDialog: (show: boolean, selected_conversion: string | null) => void;
|
handleEditDialog: (show: boolean, selected_conversion: string | null) => void;
|
||||||
|
showAddDialog: boolean;
|
||||||
|
handleAddDialog: (show: boolean) => void;
|
||||||
showDeleteDialog: boolean;
|
showDeleteDialog: boolean;
|
||||||
handleDeleteDialog: (show: boolean, selected_conversion: string | null) => void;
|
handleDeleteDialog: (show: boolean, selected_conversion: string | null) => void;
|
||||||
selectedConversion: string | null;
|
selectedConversion: string | null;
|
||||||
getConversionLists: (
|
conversion: string | null;
|
||||||
limit: number,
|
|
||||||
page: number,
|
|
||||||
with_deleted: boolean,
|
|
||||||
order_field: any,
|
|
||||||
order_direction: any
|
|
||||||
) => Promise<{ data: ConversionProps[]; totalCount: number } | undefined>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialProps: ContextProps = {
|
const initialProps: ContextProps = {
|
||||||
conversion: [],
|
|
||||||
showAddDialog: false,
|
|
||||||
handleAddDialog: () => {},
|
|
||||||
showEditDialog: false,
|
showEditDialog: false,
|
||||||
handleEditDialog: () => {},
|
handleEditDialog: () => {},
|
||||||
|
showAddDialog: false,
|
||||||
|
handleAddDialog: () => {},
|
||||||
showDeleteDialog: false,
|
showDeleteDialog: false,
|
||||||
handleDeleteDialog: () => {},
|
handleDeleteDialog: () => {},
|
||||||
selectedConversion: null,
|
selectedConversion: null,
|
||||||
getConversionLists: async () => undefined
|
conversion: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const ManageConversionContext = createContext<ContextProps>(initialProps);
|
const ManageConversionContext = createContext<ContextProps>(initialProps);
|
||||||
const API_URL_WALLET = apiConfig.service_wallet;
|
const API_URL = apiConfig.service_wallet;
|
||||||
|
|
||||||
const ManageConversionContextProvider = ({ children }: { children: React.ReactNode }) => {
|
const ManageConversionContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
const [conversions, setConversions] = useState<ConversionProps[]>([]);
|
|
||||||
const [showAddDialog, setShowAddDialog] = useState(false);
|
|
||||||
const [showEditDialog, setShowEditDialog] = useState(false);
|
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||||
|
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||||
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||||
const [selectedConversion, setSelectedConversion] = useState<string | null>(null);
|
|
||||||
const { GetData } = useCallApi();
|
const { GetData } = useCallApi();
|
||||||
|
const [selectedConversion, setSelectedConversion] = useState<string | null>(null);
|
||||||
|
const [conversion, setConversion] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const handleEditDialog = useCallback((show: boolean, selected_conversion: string | null) => {
|
||||||
|
setSelectedConversion(show ? selected_conversion : null);
|
||||||
|
setShowEditDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const handleAddDialog = useCallback((show: boolean) => {
|
const handleAddDialog = useCallback((show: boolean) => {
|
||||||
setShowAddDialog(show);
|
setShowAddDialog(show);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleEditDialog = useCallback((show: boolean, selected_conversion: string | null) => {
|
|
||||||
setShowEditDialog(show);
|
|
||||||
setSelectedConversion(show ? selected_conversion : null);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleDeleteDialog = useCallback((show: boolean, selected_conversion: string | null) => {
|
const handleDeleteDialog = useCallback((show: boolean, selected_conversion: string | null) => {
|
||||||
setShowEditDialog(show);
|
setShowDeleteDialog(show);
|
||||||
setSelectedConversion(show ? selected_conversion : null);
|
setSelectedConversion(show ? selected_conversion : null);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const columns = useMemo<ColumnDef<any>[]>(
|
const columns = useMemo<ColumnDef<any>[]>(
|
||||||
() => [
|
() => [
|
||||||
{
|
{
|
||||||
accessorFn: (row) => row.name,
|
accessorFn: (row) => row.currency_origin.name,
|
||||||
id: 'name',
|
id: 'id_currency_origin',
|
||||||
header: ({ column }) => <DataGridColumnHeader title="Name" column={column} />,
|
header: ({ column }) => <DataGridColumnHeader title="Currency Origin" column={column} />,
|
||||||
enableSorting: true,
|
enableSorting: true,
|
||||||
enableHiding: false,
|
enableHiding: false,
|
||||||
meta: {
|
meta: { headerClassName: 'w-[150px]' }
|
||||||
headerClassName: 'w-[250px]'
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.currency_destination.name,
|
||||||
|
id: 'id_currency_destination',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Currency Destination" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[150px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.buy,
|
||||||
|
id: 'buy',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Buy" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[150px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.sell,
|
||||||
|
id: 'sell',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Sell" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[150px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.status,
|
||||||
|
id: 'status',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Status" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[150px]' },
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const isActive = row.original.status === 'Y';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
className={`px-2 py-1 text-xs font-semibold rounded-full ${
|
||||||
|
isActive ? 'bg-green-100 text-green-600' : 'bg-red-100 text-red-600'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{isActive ? 'Active' : 'Inactive'}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -89,79 +133,80 @@ const ManageConversionContextProvider = ({ children }: { children: React.ReactNo
|
|||||||
<>
|
<>
|
||||||
<button
|
<button
|
||||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
onClick={() => handleEditDialog(true, row.id)}
|
onClick={() => handleEditDialog(true, row.ID)}
|
||||||
>
|
>
|
||||||
<KeenIcon icon="notepad-edit" />
|
<KeenIcon icon="notepad-edit" />
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
onClick={() => handleDeleteDialog(true, row.id)}
|
onClick={() => handleDeleteDialog(true, row.ID)}
|
||||||
>
|
>
|
||||||
<KeenIcon icon="trash" />
|
<KeenIcon icon="trash" />
|
||||||
</button>
|
</button>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
meta: {
|
meta: { headerClassName: 'w-[100px]', cellClassName: 'text-center' }
|
||||||
headerClassName: 'w-[100px]',
|
|
||||||
cellClassName: 'text-center'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
[]
|
[handleEditDialog, handleDeleteDialog]
|
||||||
);
|
);
|
||||||
|
const doGetConversion = async (
|
||||||
const getConversionLists = async (page: number, limit: number, sorting: any, filter: any) => {
|
page: number,
|
||||||
try {
|
limit: number,
|
||||||
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
sorting: any,
|
||||||
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
|
filter: any
|
||||||
const response = await GetData(`${API_URL_WALLET}/dashboard/conversion`, {
|
) => {
|
||||||
limit,
|
sorting = sorting.length == 0 ? [{ id: 'name', desc: true }] : sorting;
|
||||||
page: page + 1,
|
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
|
||||||
with_deleted: false,
|
console.log(sorting);
|
||||||
order_field: sorting[0].id,
|
const response = await GetData(`${API_URL}/dashboard/conversion/`, {
|
||||||
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
|
limit: limit,
|
||||||
filter: JSON.stringify(filter)
|
page: page + 1,
|
||||||
});
|
with_deleted: false,
|
||||||
console.log(response?.data);
|
order_field: sorting[0].id,
|
||||||
setConversions(response?.data.list);
|
order_direction: sorting[0].desc ? 'ASC' : 'DESC',
|
||||||
return { data: response?.data.list, totalCount: response?.data.total_count };
|
filter: JSON.stringify(filter)
|
||||||
} catch (error) {
|
});
|
||||||
console.error('Error fetching Conversion', error);
|
console.log(response?.data);
|
||||||
}
|
return { data: response?.data.list, totalCount: response?.data.total_count };
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ManageConversionContext.Provider
|
<div className="min-h-screen">
|
||||||
value={{
|
<ManageConversionContext.Provider
|
||||||
conversion: conversions,
|
value={{
|
||||||
showAddDialog,
|
showEditDialog,
|
||||||
handleAddDialog,
|
handleEditDialog,
|
||||||
showEditDialog,
|
showAddDialog,
|
||||||
handleEditDialog,
|
handleAddDialog,
|
||||||
showDeleteDialog,
|
showDeleteDialog,
|
||||||
handleDeleteDialog,
|
handleDeleteDialog,
|
||||||
selectedConversion,
|
selectedConversion,
|
||||||
getConversionLists
|
conversion
|
||||||
}}
|
}}
|
||||||
>
|
|
||||||
<Toaster expand visibleToasts={9} duration={3000} />
|
|
||||||
<DataGridProvider
|
|
||||||
columns={columns}
|
|
||||||
pagination={{ size: 10 }}
|
|
||||||
toolbar={<ListToolbar />}
|
|
||||||
layout={{ card: true }}
|
|
||||||
sorting={[{ id: 'id', desc: false }]}
|
|
||||||
serverSide={true}
|
|
||||||
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
|
||||||
getConversionLists(pageIndex, pageSize, sorting, columnFilters)
|
|
||||||
}
|
|
||||||
>
|
>
|
||||||
{children}
|
<Toaster expand visibleToasts={9} duration={3000} />
|
||||||
</DataGridProvider>
|
|
||||||
</ManageConversionContext.Provider>
|
<div className="px-4">
|
||||||
|
<DataGridProvider
|
||||||
|
columns={columns}
|
||||||
|
pagination={{ size: 10 }}
|
||||||
|
layout={{ card: true }}
|
||||||
|
toolbar={<ListToolbar />}
|
||||||
|
sorting={[{ id: 'ID', desc: true }]}
|
||||||
|
serverSide={true}
|
||||||
|
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
||||||
|
doGetConversion(pageIndex, pageSize, sorting, columnFilters)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</DataGridProvider>
|
||||||
|
</div>
|
||||||
|
</ManageConversionContext.Provider>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export { ManageConversionContext, ManageConversionContextProvider };
|
export { ManageConversionContext, ManageConversionContextProvider };
|
||||||
export type { ConversionProps };
|
export type { Conversion };
|
||||||
|
|||||||
Reference in New Issue
Block a user