Merge branch 'master' of https://git.shiblysolution.id/TPAY/dashboard
This commit is contained in:
@ -27,7 +27,7 @@ const App = () => {
|
||||
<PathnameProvider>
|
||||
<AppRouting />
|
||||
</PathnameProvider>
|
||||
<Toaster />
|
||||
<Toaster expand visibleToasts={20} duration={3000} />
|
||||
</BrowserRouter>
|
||||
);
|
||||
};
|
||||
|
||||
@ -6,12 +6,12 @@ import { apiConfig } from '@/config/api.config';
|
||||
import { useEffect, useState } from 'react';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
Dialog,
|
||||
DialogBody,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
Dialog,
|
||||
DialogBody,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
} from '@/components/ui/dialog';
|
||||
import TransactionLogViewer from './DetailTransactionLog';
|
||||
|
||||
@ -29,125 +29,182 @@ const statusMap: Record<StatusCode, StatusInfo> = {
|
||||
W: { label: 'Waiting Schedule', bg: 'bg-yellow-100', text: 'text-yellow-600' },
|
||||
O: { label: 'On Process', bg: 'bg-blue-100', text: 'text-blue-600' },
|
||||
F: { label: 'Fail', bg: 'bg-red-100', text: 'text-red-600' },
|
||||
D: { label: 'Done', bg: 'bg-green-100', text: 'text-green-600' },
|
||||
D: { label: 'Done', bg: 'bg-green-100', text: 'text-green-600' }
|
||||
};
|
||||
|
||||
export const renderStatusBadge = (statusRaw: string | null | undefined) => {
|
||||
const status = statusRaw as StatusCode;
|
||||
const { label, bg, text } = statusMap[status] ?? {
|
||||
label: 'Unknown',
|
||||
bg: 'bg-gray-100',
|
||||
text: 'text-gray-600',
|
||||
};
|
||||
const status = statusRaw as StatusCode;
|
||||
const { label, bg, text } = statusMap[status] ?? {
|
||||
label: 'Unknown',
|
||||
bg: 'bg-gray-100',
|
||||
text: 'text-gray-600'
|
||||
};
|
||||
|
||||
return (
|
||||
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${bg} ${text}`}>
|
||||
{label}
|
||||
</span>
|
||||
);
|
||||
return (
|
||||
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${bg} ${text}`}>{label}</span>
|
||||
);
|
||||
};
|
||||
|
||||
const DetailTransaction = () => {
|
||||
const { GetData } = useCallApi();
|
||||
const {
|
||||
showDetailDialog,
|
||||
setShowDetailDialog,
|
||||
selectedTransactionId,
|
||||
setShowDetailLogDialog,
|
||||
setDetailLogData
|
||||
} = useTransactionContext();
|
||||
const { GetData } = useCallApi();
|
||||
const {
|
||||
showDetailDialog,
|
||||
setShowDetailDialog,
|
||||
selectedTransactionId,
|
||||
setShowDetailLogDialog,
|
||||
setDetailLogData
|
||||
} = useTransactionContext();
|
||||
|
||||
const [transactionDetails, setTransactionDetails] = useState<any>(null);
|
||||
const [transactionDetails, setTransactionDetails] = useState<any>(null);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchTransactionDetails = async () => {
|
||||
if (selectedTransactionId) {
|
||||
try {
|
||||
const response = await GetData(`${API_URL}/transaction/history/${selectedTransactionId}`, {
|
||||
id: selectedTransactionId
|
||||
});
|
||||
// console.log(response?.data);
|
||||
setTransactionDetails(response?.data);
|
||||
} catch (error) {
|
||||
console.error('Error fetching transaction', error);
|
||||
}
|
||||
useEffect(() => {
|
||||
const fetchTransactionDetails = async () => {
|
||||
setIsLoading(true);
|
||||
if (selectedTransactionId) {
|
||||
try {
|
||||
const response = await GetData(
|
||||
`${API_URL}/transaction/history/${selectedTransactionId}`,
|
||||
{
|
||||
id: selectedTransactionId
|
||||
}
|
||||
};
|
||||
|
||||
if (showDetailDialog && selectedTransactionId) {
|
||||
fetchTransactionDetails();
|
||||
);
|
||||
// console.log(response?.data);
|
||||
setTransactionDetails(response?.data);
|
||||
} catch (error) {
|
||||
console.error('Error fetching transaction', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [showDetailDialog, selectedTransactionId, GetData]);
|
||||
}
|
||||
};
|
||||
|
||||
const [activeTab, setActiveTab] = useState('detail'); // 'detail', 'log', 'approve'
|
||||
if (showDetailDialog && selectedTransactionId) {
|
||||
fetchTransactionDetails();
|
||||
}
|
||||
}, [showDetailDialog, selectedTransactionId, GetData]);
|
||||
|
||||
return (
|
||||
<Dialog open={showDetailDialog} onOpenChange={setShowDetailDialog}>
|
||||
<DialogContent className="container-fixed max-w-[1024px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Transaction Details</DialogTitle>
|
||||
</DialogHeader>
|
||||
<DialogBody>
|
||||
{/* Tab Content */}
|
||||
<div className="py-4 overflow-y-auto max-h-[400px]">
|
||||
<div className="space-y-4">
|
||||
<div className="border rounded-lg overflow-x-auto">
|
||||
<table className="min-w-full table-auto">
|
||||
<thead>
|
||||
<tr className="bg-gray-100">
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Username</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Fullname</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Amount</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Status</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Process Date</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Invoice Number</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Actions</th>
|
||||
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{transactionDetails?.log && transactionDetails?.log.length > 0 ? (
|
||||
transactionDetails.log.map((log: { id: number, customer: any, amount: number, remark: string, reference: string, request_date: string, payment_response: string, status: string}, index: number) => (
|
||||
<tr key={index} className="border-t">
|
||||
<td className="px-4 py-2 text-sm text-gray-500">{log.customer.username ?? '-'}</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">{log.customer.fullname ?? '-'}</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">{log.amount ?? '-'}</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">{renderStatusBadge(log.status) ?? '-'}</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">{log.request_date && moment(log.request_date).isValid() ? moment(log.request_date).format('DD/MM/YYYY HH:mm:ss') : '-'}</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">{log.reference ?? '-'}</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
<div key={`actions-${log.id}`}>
|
||||
<button
|
||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||
onClick={() => {
|
||||
setDetailLogData(log)
|
||||
setShowDetailLogDialog(true);
|
||||
}}
|
||||
>
|
||||
<KeenIcon icon="eye" />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
))
|
||||
) : (
|
||||
<tr>
|
||||
<td colSpan={8} className="px-4 py-2 text-center text-sm text-gray-500">
|
||||
No logs available
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
const [activeTab, setActiveTab] = useState('detail'); // 'detail', 'log', 'approve'
|
||||
|
||||
const resetForm = () => {
|
||||
setTransactionDetails(null);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!showDetailDialog) {
|
||||
resetForm();
|
||||
}
|
||||
}, [showDetailDialog]);
|
||||
|
||||
return (
|
||||
<Dialog open={showDetailDialog} onOpenChange={setShowDetailDialog}>
|
||||
<DialogContent className="container-fixed max-w-[1024px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Transaction Details</DialogTitle>
|
||||
</DialogHeader>
|
||||
<DialogBody>
|
||||
{/* Tab Content */}
|
||||
<div className="py-4 overflow-y-auto max-h-[400px]">
|
||||
<div className="space-y-4">
|
||||
<div className="border rounded-lg overflow-x-auto">
|
||||
{isLoading ? (
|
||||
<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>
|
||||
</DialogBody>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
<p className="mt-4 text-gray-500">Loading Logs Details...</p>
|
||||
</div>
|
||||
) : (
|
||||
<table className="min-w-full table-auto">
|
||||
<thead>
|
||||
<tr className="bg-gray-100">
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Username</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Fullname</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Amount</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Status</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Process Date</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">
|
||||
Invoice Number
|
||||
</th>
|
||||
<th className="px-4 py-2 text-left text-sm text-gray-500">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{transactionDetails?.log && transactionDetails.log.length > 0 ? (
|
||||
transactionDetails.log.map(
|
||||
(
|
||||
log: {
|
||||
id: number;
|
||||
customer: any;
|
||||
amount: number;
|
||||
remark: string;
|
||||
reference: string;
|
||||
request_date: string;
|
||||
payment_response: string;
|
||||
status: string;
|
||||
},
|
||||
index: number
|
||||
) => (
|
||||
<tr key={index} className="border-t">
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
{log.customer?.username ?? 'Not Found'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
{log.customer?.fullname ?? 'Not Found'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
{log.amount ?? '-'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
{renderStatusBadge(log.status) ?? '-'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
{log.request_date && moment(log.request_date).isValid()
|
||||
? moment(log.request_date).format('DD/MM/YYYY HH:mm:ss')
|
||||
: '-'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
{log.reference ?? '-'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-sm text-gray-500">
|
||||
<div key={`actions-${log.id}`}>
|
||||
<button
|
||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||
onClick={() => {
|
||||
setDetailLogData(log);
|
||||
setShowDetailLogDialog(true);
|
||||
}}
|
||||
>
|
||||
<KeenIcon icon="eye" />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
)
|
||||
) : (
|
||||
<tr>
|
||||
<td colSpan={8} className="px-4 py-2 text-center text-sm text-gray-500">
|
||||
No logs available
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</DialogBody>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default DetailTransaction;
|
||||
|
||||
@ -31,7 +31,7 @@ const ListToolbar = () => {
|
||||
|
||||
const handleFilterData = useCallback(() => {
|
||||
try {
|
||||
table.getColumn('transaction_date')?.setFilterValue(trxDate);
|
||||
table.getColumn('execution_date')?.setFilterValue(trxDate);
|
||||
} catch (error) {
|
||||
toast.error('Error applying filter');
|
||||
console.error('Error applying filter:', error);
|
||||
|
||||
@ -35,6 +35,7 @@ import {
|
||||
import { useManageConversionContext } from '../hooks/useManageConversionContext';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { initialStateConversion, validateFormConversion } from './Types';
|
||||
interface CurrencyProps {
|
||||
ID: string;
|
||||
name: string;
|
||||
@ -50,27 +51,15 @@ const AddDialog = () => {
|
||||
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 [errors, setErrors] = useState<Record<string, string>>({});
|
||||
const [formField, setFormField] = useState(initialStateConversion);
|
||||
const created_time = new Date();
|
||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
setFormField(initialStateConversion);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const doCreateConversion = useCallback(
|
||||
@ -94,8 +83,7 @@ const AddDialog = () => {
|
||||
doSaveLogActivity(createActivity);
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Error Create Conversion');
|
||||
setAlert({ show: true, message: response?.message });
|
||||
toast.error(response?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something Went Wrong');
|
||||
@ -126,20 +114,11 @@ const AddDialog = () => {
|
||||
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.' });
|
||||
if (!validateFormConversion(formField, setErrors)) {
|
||||
return;
|
||||
}
|
||||
|
||||
doCreateConversion(e);
|
||||
// console.log(formField);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -171,12 +150,6 @@ const AddDialog = () => {
|
||||
</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">
|
||||
@ -185,11 +158,14 @@ const AddDialog = () => {
|
||||
</label>
|
||||
<Select
|
||||
value={formField.id_currency_origin}
|
||||
onValueChange={(id_currency_origin) =>
|
||||
setFormField((prev) => ({ ...prev, id_currency_origin }))
|
||||
}
|
||||
onValueChange={(id_currency_origin) => {
|
||||
setFormField((prev) => ({ ...prev, id_currency_origin }));
|
||||
setErrors((prev) => ({ ...prev, id_currency_destination: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger
|
||||
className={errors.id_currency_destination ? 'border-red-500' : ''}
|
||||
>
|
||||
<SelectValue placeholder="Select Currency" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -200,6 +176,11 @@ const AddDialog = () => {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.id_currency_destination && (
|
||||
<span className="text-red-500 text-xs mt-1">
|
||||
{errors.id_currency_destination}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -207,11 +188,12 @@ const AddDialog = () => {
|
||||
</label>
|
||||
<Select
|
||||
value={formField.id_currency_destination}
|
||||
onValueChange={(id_currency_destination) =>
|
||||
setFormField((prev) => ({ ...prev, id_currency_destination }))
|
||||
}
|
||||
onValueChange={(id_currency_destination) => {
|
||||
setFormField((prev) => ({ ...prev, id_currency_destination }));
|
||||
setErrors((prev) => ({ ...prev, id_currency_origin: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.id_currency_origin ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select Currency" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -222,13 +204,16 @@ const AddDialog = () => {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.id_currency_origin && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.id_currency_origin}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
Buy<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<NumericFormat
|
||||
className="input"
|
||||
className={`input ${errors.buy ? 'border-red-500' : ''}`}
|
||||
value={formField.buy}
|
||||
thousandSeparator="."
|
||||
decimalSeparator=","
|
||||
@ -236,11 +221,13 @@ const AddDialog = () => {
|
||||
onValueChange={(values) => {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
buy: values.floatValue || 0
|
||||
buy: values.floatValue ?? null
|
||||
}));
|
||||
setErrors((prev) => ({ ...prev, buy: '' }));
|
||||
}}
|
||||
placeholder="Enter Buy"
|
||||
/>
|
||||
{errors.buy && <span className="text-red-500 text-xs mt-1">{errors.buy}</span>}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -248,7 +235,7 @@ const AddDialog = () => {
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<NumericFormat
|
||||
className="input"
|
||||
className={`input ${errors.sell ? 'border-red-500' : ''}`}
|
||||
value={formField.sell}
|
||||
thousandSeparator="."
|
||||
decimalSeparator=","
|
||||
@ -256,11 +243,13 @@ const AddDialog = () => {
|
||||
onValueChange={(values) => {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
sell: values.floatValue || 0
|
||||
sell: values.floatValue ?? null
|
||||
}));
|
||||
setErrors((prev) => ({ ...prev, sell: '' }));
|
||||
}}
|
||||
placeholder="Enter Sell"
|
||||
/>
|
||||
{errors.sell && <span className="text-red-500 text-xs mt-1">{errors.sell}</span>}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -271,11 +260,12 @@ const AddDialog = () => {
|
||||
<div className="grow">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -283,6 +273,9 @@ const AddDialog = () => {
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-end gap-5">
|
||||
|
||||
@ -35,6 +35,7 @@ import {
|
||||
import { useManageConversionContext } from '../hooks/useManageConversionContext';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { initialStateConversion, validateFormConversion } from './Types';
|
||||
interface CurrencyProps {
|
||||
ID: string;
|
||||
name: string;
|
||||
@ -52,26 +53,15 @@ const EditDialog = () => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [isLoading, setIsLoading] = 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 [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const [formField, setFormField] = useState(initialStateConversion);
|
||||
const updated_time = new Date();
|
||||
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
setFormField(initialStateConversion);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const doUpdateConversion = useCallback(
|
||||
@ -79,6 +69,11 @@ const EditDialog = () => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
if (!validateFormConversion(formField, setErrors)) {
|
||||
setIsSubmitting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await PutData(`${API_URL}/dashboard/conversion/${selectedConversion}`, {
|
||||
...formField
|
||||
@ -97,8 +92,7 @@ const EditDialog = () => {
|
||||
doSaveLogActivity(createActivity);
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Error Create Conversion');
|
||||
setAlert({ show: true, message: response?.message });
|
||||
toast.error(response?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something went wrong');
|
||||
@ -185,12 +179,6 @@ const EditDialog = () => {
|
||||
</DialogHeader>
|
||||
<DialogBody ref={parentRef}>
|
||||
<div className="flex flex-col">
|
||||
{alert.show && (
|
||||
<Alert variant="danger">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{isLoading ? (
|
||||
<div className="flex flex-col items-center justify-center p-8">
|
||||
<div className="animate-pulse flex space-x-4 w-full">
|
||||
@ -213,11 +201,12 @@ const EditDialog = () => {
|
||||
</label>
|
||||
<Select
|
||||
value={formField.id_currency_origin}
|
||||
onValueChange={(id_currency_origin) =>
|
||||
setFormField((prev) => ({ ...prev, id_currency_origin }))
|
||||
}
|
||||
onValueChange={(id_currency_origin) => {
|
||||
setFormField((prev) => ({ ...prev, id_currency_origin }));
|
||||
setErrors((prev) => ({ ...prev, id_currency_origin: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.id_currency_origin ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select Currency" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -228,6 +217,9 @@ const EditDialog = () => {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.id_currency_origin && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.id_currency_origin}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -235,11 +227,14 @@ const EditDialog = () => {
|
||||
</label>
|
||||
<Select
|
||||
value={formField.id_currency_destination}
|
||||
onValueChange={(id_currency_destination) =>
|
||||
setFormField((prev) => ({ ...prev, id_currency_destination }))
|
||||
}
|
||||
onValueChange={(id_currency_destination) => {
|
||||
setFormField((prev) => ({ ...prev, id_currency_destination }));
|
||||
setErrors((prev) => ({ ...prev, id_currency_destination: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger
|
||||
className={errors.id_currency_destination ? 'border-red-500' : ''}
|
||||
>
|
||||
<SelectValue placeholder="Select Currency" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -250,6 +245,11 @@ const EditDialog = () => {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.id_currency_destination && (
|
||||
<span className="text-red-500 text-xs mt-1">
|
||||
{errors.id_currency_destination}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -264,11 +264,13 @@ const EditDialog = () => {
|
||||
onValueChange={(values) => {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
buy: values.floatValue || 0
|
||||
buy: values.floatValue ?? null
|
||||
}));
|
||||
setErrors((prev) => ({ ...prev, buy: '' }));
|
||||
}}
|
||||
placeholder="Enter Buy"
|
||||
/>
|
||||
{errors.buy && <span className="text-red-500 text-xs mt-1">{errors.buy}</span>}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -284,11 +286,15 @@ const EditDialog = () => {
|
||||
onValueChange={(values) => {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
sell: values.floatValue || 0
|
||||
sell: values.floatValue ?? null
|
||||
}));
|
||||
setErrors((prev) => ({ ...prev, sell: '' }));
|
||||
}}
|
||||
placeholder="Enter Sell"
|
||||
/>
|
||||
{errors.sell && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.sell}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
@ -299,11 +305,12 @@ const EditDialog = () => {
|
||||
<div className="grow">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -311,6 +318,9 @@ const EditDialog = () => {
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-end gap-5">
|
||||
|
||||
50
src/pages/master/conversion/blocks/Types.ts
Normal file
50
src/pages/master/conversion/blocks/Types.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { toast } from 'sonner';
|
||||
|
||||
export const validateFormConversion = (
|
||||
formField: typeof initialStateConversion,
|
||||
setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
|
||||
) => {
|
||||
const requiredFields = [
|
||||
{ key: 'id_currency_origin', label: 'Currency Origin' },
|
||||
{ key: 'id_currency_destination', label: 'Currency Destination' },
|
||||
{ key: 'buy', label: 'Buy' },
|
||||
{ key: 'sell', label: 'Sell' },
|
||||
{ key: 'status', label: 'Status' }
|
||||
];
|
||||
|
||||
const newErrors: Record<string, string> = {};
|
||||
let isValid = true;
|
||||
|
||||
requiredFields.forEach(({ key, label }) => {
|
||||
if (
|
||||
formField[key as keyof typeof formField] === '' ||
|
||||
formField[key as keyof typeof formField] === null ||
|
||||
formField[key as keyof typeof formField] === undefined
|
||||
) {
|
||||
newErrors[key] = `${label} is required`;
|
||||
toast.error(`${label} is required`);
|
||||
isValid = false;
|
||||
}
|
||||
});
|
||||
|
||||
setErrors(newErrors);
|
||||
return isValid;
|
||||
};
|
||||
|
||||
export const initialStateConversion: {
|
||||
status: string;
|
||||
id_currency_origin: string;
|
||||
id_currency_destination: string;
|
||||
buy: number | null;
|
||||
sell: number | null;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
} = {
|
||||
status: '',
|
||||
id_currency_origin: '',
|
||||
id_currency_destination: '',
|
||||
buy: null,
|
||||
sell: null,
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
@ -15,16 +15,6 @@ import {
|
||||
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,
|
||||
@ -33,9 +23,9 @@ import {
|
||||
SelectValue
|
||||
} from '@/components/ui/select';
|
||||
import { useManageCurrencyContext } from '../hooks/useManageCurrencyContext';
|
||||
import { prefix } from 'stylis';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { initialStateCurrency, validateFormCurrency } from './Types';
|
||||
interface CurrencyProps {
|
||||
ID: string;
|
||||
name: string;
|
||||
@ -50,27 +40,15 @@ const AddDialog = () => {
|
||||
const { PostData, GetData } = useCallApi();
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const initialState = {
|
||||
code: '',
|
||||
name: '',
|
||||
prefix: '',
|
||||
status: '',
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
const [formField, setFormField] = useState(initialState);
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
const [formField, setFormField] = useState(initialStateCurrency);
|
||||
const created_time = new Date();
|
||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
setFormField(initialStateCurrency);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const doCreateCurrency = useCallback(
|
||||
@ -94,8 +72,7 @@ const AddDialog = () => {
|
||||
doSaveLogActivity(createActivity);
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Error Create Currency');
|
||||
setAlert({ show: true, message: response?.message });
|
||||
toast.error(response?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something went wrong');
|
||||
@ -109,19 +86,11 @@ const AddDialog = () => {
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (
|
||||
formField.code === '' ||
|
||||
formField.name === '' ||
|
||||
formField.prefix === '' ||
|
||||
formField.status === ''
|
||||
) {
|
||||
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||
if (!validateFormCurrency(formField, setErrors)) {
|
||||
return;
|
||||
}
|
||||
|
||||
doCreateCurrency(e);
|
||||
// console.log(formField);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -144,17 +113,11 @@ const AddDialog = () => {
|
||||
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Cuurency - Create</DialogTitle>
|
||||
<DialogTitle>Currency - 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="grid grid-cols-8 gap-2 w-full items-center">
|
||||
@ -163,14 +126,20 @@ const AddDialog = () => {
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.code ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.code}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, code: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, code: target.value }));
|
||||
setErrors((prev) => ({ ...prev, code: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.code && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.code}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
@ -178,14 +147,20 @@ const AddDialog = () => {
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.name ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.name}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, name: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, name: target.value }));
|
||||
setErrors((prev) => ({ ...prev, name: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.name && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.name}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
@ -193,14 +168,20 @@ const AddDialog = () => {
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.prefix ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.prefix}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, prefix: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, prefix: target.value }));
|
||||
setErrors((prev) => ({ ...prev, prefix: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.prefix && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.prefix}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
@ -211,11 +192,12 @@ const AddDialog = () => {
|
||||
<div className="col-span-6">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectTrigger className={`w-full ${errors.status && 'border-red-500'}`}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -223,12 +205,12 @@ const AddDialog = () => {
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-end gap-5">
|
||||
<Button type="button" variant="outline" onClick={resetForm}>
|
||||
Reset
|
||||
</Button>
|
||||
<Button variant="default" type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting ? (
|
||||
<RefreshCw className="animate-spin h-8 w-8 text-white mx-3" />
|
||||
|
||||
@ -15,16 +15,6 @@ import {
|
||||
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,
|
||||
@ -35,6 +25,7 @@ import {
|
||||
import { useManageCurrencyContext } from '../hooks/useManageCurrencyContext';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { initialStateCurrency, validateFormCurrency } from './Types';
|
||||
interface CurrencyProps {
|
||||
ID: string;
|
||||
name: string;
|
||||
@ -49,28 +40,16 @@ const EditDialog = () => {
|
||||
const { PostData, GetData, PutData } = useCallApi();
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const initialState = {
|
||||
code: '',
|
||||
name: '',
|
||||
prefix: '',
|
||||
status: '',
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
const [formField, setFormField] = useState(initialState);
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
const [formField, setFormField] = useState(initialStateCurrency);
|
||||
const updated_time = new Date();
|
||||
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
setFormField(initialStateCurrency);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const doUpdateCurrency = useCallback(
|
||||
@ -78,6 +57,11 @@ const EditDialog = () => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
if (!validateFormCurrency(formField, setErrors)) {
|
||||
setIsSubmitting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await PutData(
|
||||
`${API_URL}/dashboard/currency/${selectedCurrency}`,
|
||||
@ -97,8 +81,7 @@ const EditDialog = () => {
|
||||
doSaveLogActivity(createActivity);
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Error Create Currency');
|
||||
setAlert({ show: true, message: response?.message });
|
||||
toast.error(response?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something went wrong');
|
||||
@ -115,7 +98,6 @@ const EditDialog = () => {
|
||||
const fetchData = GetData(`${API_URL}/dashboard/currency/${id}`, { id });
|
||||
const [response] = await Promise.all([fetchData, minDelay]);
|
||||
|
||||
// console.log('Transaction Type: ', response?.data);
|
||||
if (response?.status) {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
@ -126,7 +108,6 @@ const EditDialog = () => {
|
||||
}));
|
||||
}
|
||||
setIsLoading(false);
|
||||
// console.log('form fieldd Transaction Type: ', formField);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
@ -160,12 +141,6 @@ const EditDialog = () => {
|
||||
</DialogHeader>
|
||||
<DialogBody ref={parentRef}>
|
||||
<div className="flex flex-col">
|
||||
{alert.show && (
|
||||
<Alert variant="danger">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{isLoading ? (
|
||||
<div className="flex flex-col items-center justify-center p-8">
|
||||
<div className="animate-pulse flex space-x-4 w-full">
|
||||
@ -182,74 +157,104 @@ const EditDialog = () => {
|
||||
) : (
|
||||
<form onSubmit={doUpdateCurrency}>
|
||||
<div className="card-body grid gap-5">
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
Code
|
||||
<span className="text-red-500">*</span>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Code<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className={`input col-span-6 ${errors.code ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
placeholder="Code"
|
||||
autoComplete="off"
|
||||
value={formField.code}
|
||||
onChange={(e) => setFormField((prev) => ({ ...prev, code: e.target.value }))}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, code: target.value }));
|
||||
setErrors((prev) => ({ ...prev, code: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.code && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.code}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
Name
|
||||
<span className="text-red-500">*</span>
|
||||
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Name<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className={`input col-span-6 ${errors.name ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
placeholder="Code"
|
||||
autoComplete="off"
|
||||
value={formField.name}
|
||||
onChange={(e) => setFormField((prev) => ({ ...prev, name: e.target.value }))}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, name: target.value }));
|
||||
setErrors((prev) => ({ ...prev, name: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.name && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.name}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
Prefix
|
||||
<span className="text-red-500">*</span>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Prefix<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className={`input col-span-6 ${errors.prefix ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
placeholder="Code"
|
||||
autoComplete="off"
|
||||
value={formField.prefix}
|
||||
onChange={(e) =>
|
||||
setFormField((prev) => ({ ...prev, prefix: e.target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, prefix: target.value }));
|
||||
setErrors((prev) => ({ ...prev, prefix: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.prefix && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.prefix}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<label className="form-label">
|
||||
Status
|
||||
<span className="text-red-500">*</span>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Status<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Select" defaultValue={formField.status} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="Y">Active</SelectItem>
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<div className="col-span-6">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className={`w-full ${errors.status && 'border-red-500'}`}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="Y">Active</SelectItem>
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end gap-5">
|
||||
<Button variant="default" type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting ? (
|
||||
<RefreshCw className="animate-spin h-8 w-8 text-white mx-7" />
|
||||
<RefreshCw className="animate-spin h-8 w-8 text-white mx-3" />
|
||||
) : (
|
||||
'Save Changes'
|
||||
'Create'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
47
src/pages/master/currency/blocks/Types.ts
Normal file
47
src/pages/master/currency/blocks/Types.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { toast } from 'sonner';
|
||||
|
||||
export const initialStateCurrency: {
|
||||
code: string;
|
||||
name: string;
|
||||
prefix: string;
|
||||
status: string;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
} = {
|
||||
code: '',
|
||||
name: '',
|
||||
prefix: '',
|
||||
status: '',
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
|
||||
export const validateFormCurrency = (
|
||||
formField: typeof initialStateCurrency,
|
||||
setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
|
||||
) => {
|
||||
const requiredFields = [
|
||||
{ key: 'code', label: 'Code' },
|
||||
{ key: 'name', label: 'Name' },
|
||||
{ key: 'prefix', label: 'Prefix' },
|
||||
{ key: 'status', label: 'Status' }
|
||||
];
|
||||
|
||||
const newErrors: Record<string, string> = {};
|
||||
let isValid = true;
|
||||
|
||||
requiredFields.forEach(({ key, label }) => {
|
||||
if (
|
||||
formField[key as keyof typeof formField] === '' ||
|
||||
formField[key as keyof typeof formField] === null ||
|
||||
formField[key as keyof typeof formField] === undefined
|
||||
) {
|
||||
newErrors[key] = `${label} is required`;
|
||||
toast.error(`${label} is required`);
|
||||
isValid = false;
|
||||
}
|
||||
});
|
||||
|
||||
setErrors(newErrors);
|
||||
return isValid;
|
||||
};
|
||||
@ -155,7 +155,6 @@ const ManageCurrencyContextProvider = ({ children }: { children: React.ReactNode
|
||||
order_direction: sorting[0].desc ? 'ASC' : 'DESC'
|
||||
// filter: JSON.stringify(filter)
|
||||
});
|
||||
// console.log(response?.data);
|
||||
return { data: response?.data.list, totalCount: response?.data.total_count };
|
||||
};
|
||||
|
||||
@ -181,7 +180,7 @@ const ManageCurrencyContextProvider = ({ children }: { children: React.ReactNode
|
||||
pagination={{ size: 10 }}
|
||||
layout={{ card: true }}
|
||||
toolbar={<ListToolbar />}
|
||||
sorting={[{ id: 'ID', desc: true }]}
|
||||
sorting={[{ id: 'created_at', desc: false }]}
|
||||
serverSide={true}
|
||||
onFetchData={({ pageIndex, pageSize, sorting }) =>
|
||||
doGetCurrency(pageIndex, pageSize, sorting)
|
||||
|
||||
@ -25,6 +25,7 @@ import {
|
||||
} from '@/components/ui/select';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { initialStateReward, validateFormReward } from './Types';
|
||||
|
||||
const API_URL = apiConfig.service_master_data;
|
||||
|
||||
@ -34,28 +35,16 @@ const AddDialog = () => {
|
||||
const { reload } = useDataGrid();
|
||||
const { PostData } = useCallApi();
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const initialState = {
|
||||
name: '',
|
||||
type: '',
|
||||
amount: 0,
|
||||
status: '',
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
|
||||
const [formField, setFormField] = useState(initialState);
|
||||
const [formField, setFormField] = useState(initialStateReward);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const created_time = new Date();
|
||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
setFormField(initialStateReward);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const RewardType = {
|
||||
@ -88,8 +77,7 @@ const AddDialog = () => {
|
||||
|
||||
doSaveLogActivity(createActivity);
|
||||
} else {
|
||||
toast.error('Failed to create reward.');
|
||||
setAlert({ show: true, message: response?.message });
|
||||
toast.error(response?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something went wrong, please try again.');
|
||||
@ -103,19 +91,11 @@ const AddDialog = () => {
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (
|
||||
formField.name.trim() === '' ||
|
||||
formField.type.trim() === '' ||
|
||||
formField.amount === 0 ||
|
||||
formField.status.trim() === ''
|
||||
) {
|
||||
setAlert({ show: true, message: 'Please fill name field.' });
|
||||
if (!validateFormReward(formField, setErrors)) {
|
||||
return;
|
||||
}
|
||||
|
||||
doCreateReward(e);
|
||||
// console.log(formField);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -135,43 +115,52 @@ const AddDialog = () => {
|
||||
}, [showAddDialog]);
|
||||
|
||||
return (
|
||||
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||
<Dialog open={showAddDialog} onOpenChange={handleAddDialog}>
|
||||
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Reward - Create</DialogTitle>
|
||||
<DialogDescription></DialogDescription>
|
||||
<DialogDescription />
|
||||
</DialogHeader>
|
||||
<DialogBody ref={parentRef}>
|
||||
<div className="flex flex-col">
|
||||
{alert.show && <Alert variant="danger">{alert.message}</Alert>}
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="card-body grid gap-5">
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
{/* Reward Name */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Reward Name<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.name ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.name}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, name: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, name: target.value }));
|
||||
setErrors((prev) => ({ ...prev, name: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.name && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.name}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
|
||||
{/* Reward Type */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Reward Type<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="col-span-6">
|
||||
<Select
|
||||
value={formField.type}
|
||||
onValueChange={(value) => setFormField((prev) => ({ ...prev, type: value }))}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, type: value }));
|
||||
setErrors((prev) => ({ ...prev, type: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectTrigger className={`w-full ${errors.type ? 'border-red-500' : ''}`}>
|
||||
<SelectValue placeholder="Select Type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -185,14 +174,20 @@ const AddDialog = () => {
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{errors.type && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.type}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
|
||||
{/* Amount */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Amount<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<NumericFormat
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.amount ? 'border-red-500' : ''}`}
|
||||
value={formField.amount}
|
||||
thousandSeparator="."
|
||||
decimalSeparator=","
|
||||
@ -200,34 +195,49 @@ const AddDialog = () => {
|
||||
onValueChange={(values) => {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
amount: values.floatValue || 0
|
||||
amount: values.floatValue ?? null
|
||||
}));
|
||||
setErrors((prev) => ({ ...prev, amount: '' }));
|
||||
}}
|
||||
placeholder="Enter Amount"
|
||||
/>
|
||||
{errors.amount && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.amount}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
|
||||
{/* Status */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Status<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="col-span-6">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectTrigger className={`w-full ${errors.status ? 'border-red-500' : ''}`}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="Y">Active</SelectItem>
|
||||
<SelectItem value="N">InActive</SelectItem>
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.status}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex justify-end gap-5">
|
||||
<Button type="button" variant="outline" onClick={resetForm}>
|
||||
Reset
|
||||
|
||||
@ -25,6 +25,7 @@ import {
|
||||
import { useManageRewardContext } from '../hooks/useManageRewardContext';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { initialStateReward, validateFormReward } from './Types';
|
||||
|
||||
const API_URL = apiConfig.service_master_data;
|
||||
|
||||
@ -36,27 +37,15 @@ const EditDialog = () => {
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const initialState = {
|
||||
name: '',
|
||||
type: '',
|
||||
amount: 0,
|
||||
status: '',
|
||||
updated_by: '',
|
||||
updated_at: ''
|
||||
};
|
||||
|
||||
const [formField, setFormField] = useState(initialState);
|
||||
const [formField, setFormField] = useState(initialStateReward);
|
||||
const updated_time = new Date();
|
||||
const formattedTime = updated_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
setFormField(initialStateReward);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const RewardType = {
|
||||
@ -73,6 +62,11 @@ const EditDialog = () => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
if (!validateFormReward(formField, setErrors)) {
|
||||
setIsSubmitting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await PutData(`${API_URL}/reward/update/${selectedReward}`, formField);
|
||||
|
||||
@ -89,8 +83,7 @@ const EditDialog = () => {
|
||||
|
||||
doSaveLogActivity(editActivity);
|
||||
} else {
|
||||
toast.error('Error Update Reward');
|
||||
setAlert({ show: true, message: response?.message });
|
||||
toast.error(response?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('Something went wrong, please try again.');
|
||||
@ -106,7 +99,6 @@ const EditDialog = () => {
|
||||
const minDelay = new Promise((resolve) => setTimeout(resolve, 300));
|
||||
const fetchData = GetData(`${API_URL}/reward/getdata/${id}`, { id });
|
||||
const [response] = await Promise.all([fetchData, minDelay]);
|
||||
// console.log('API Response:', response);
|
||||
|
||||
if (response?.status) {
|
||||
setFormField((prev) => ({
|
||||
@ -120,24 +112,6 @@ const EditDialog = () => {
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
// const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
// e.preventDefault();
|
||||
|
||||
// if (
|
||||
// formField.name === '' ||
|
||||
// formField.type === '' ||
|
||||
// formField.amount === 0 ||
|
||||
// formField.status === ''
|
||||
// ) {
|
||||
// setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||
// return;
|
||||
// }
|
||||
|
||||
// doUpdateReward(e);
|
||||
// // console.log(formField);
|
||||
// setAlert({ show: false, message: '' });
|
||||
// };
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedReward) {
|
||||
doFetchData(selectedReward);
|
||||
@ -169,8 +143,6 @@ const EditDialog = () => {
|
||||
</DialogHeader>
|
||||
<DialogBody ref={parentRef}>
|
||||
<div className="flex flex-col">
|
||||
{alert.show && <Alert variant="danger">{alert.message}</Alert>}
|
||||
|
||||
{isLoading ? (
|
||||
<div className="flex flex-col items-center justify-center p-8">
|
||||
<div className="animate-pulse flex space-x-4 w-full">
|
||||
@ -187,31 +159,42 @@ const EditDialog = () => {
|
||||
) : (
|
||||
<form onSubmit={doUpdateReward}>
|
||||
<div className="card-body grid gap-5">
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
{/* Reward Name */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Reward Name<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<Input
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.name ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.name}
|
||||
onChange={(e) => setFormField((prev) => ({ ...prev, name: e.target.value }))}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, name: target.value }));
|
||||
setErrors((prev) => ({ ...prev, name: '' }));
|
||||
}}
|
||||
/>
|
||||
{errors.name && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.name}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
|
||||
{/* Reward Type */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Reward Type<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="col-span-6">
|
||||
<Select
|
||||
value={formField.type}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, type: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, type: value }));
|
||||
setErrors((prev) => ({ ...prev, type: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectTrigger className={`w-full ${errors.type ? 'border-red-500' : ''}`}>
|
||||
<SelectValue placeholder="Select Type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -225,14 +208,20 @@ const EditDialog = () => {
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{errors.type && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.type}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
|
||||
{/* Amount */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Amount<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<NumericFormat
|
||||
className="input col-span-6"
|
||||
className={`input col-span-6 ${errors.amount ? 'border-red-500' : ''}`}
|
||||
value={formField.amount}
|
||||
thousandSeparator="."
|
||||
decimalSeparator=","
|
||||
@ -240,40 +229,60 @@ const EditDialog = () => {
|
||||
onValueChange={(values) => {
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
amount: values.floatValue || 0
|
||||
amount: values.floatValue ?? null
|
||||
}));
|
||||
setErrors((prev) => ({ ...prev, amount: '' }));
|
||||
}}
|
||||
placeholder="Enter Amount"
|
||||
/>
|
||||
{errors.amount && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.amount}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid grid-cols-8 gap-2 w-full items-center">
|
||||
|
||||
{/* Status */}
|
||||
<div className="grid grid-cols-8 gap-2 items-center">
|
||||
<label className="form-label flex items-center gap-1 col-span-2">
|
||||
Status<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="col-span-6">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Select" defaultValue={formField.status} />
|
||||
<SelectTrigger
|
||||
className={`w-full ${errors.status ? 'border-red-500' : ''}`}
|
||||
>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="Y">Active</SelectItem>
|
||||
<SelectItem value="N">InActive</SelectItem>
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1 col-span-8 ml-[calc(25%+0.5rem)]">
|
||||
{errors.status}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex justify-end gap-5">
|
||||
<Button type="button" variant="outline" onClick={resetForm}>
|
||||
Reset
|
||||
</Button>
|
||||
<Button variant="default" type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting ? (
|
||||
<RefreshCw className="animate-spin h-8 w-8 text-white mx-7" />
|
||||
<RefreshCw className="animate-spin h-8 w-8 text-white mx-3" />
|
||||
) : (
|
||||
'Save Changes'
|
||||
'Create'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
47
src/pages/master/reward/blocks/Types.ts
Normal file
47
src/pages/master/reward/blocks/Types.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { toast } from 'sonner';
|
||||
|
||||
export const initialStateReward: {
|
||||
name: string;
|
||||
type: string;
|
||||
amount: number | null;
|
||||
status: string;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
} = {
|
||||
name: '',
|
||||
type: '',
|
||||
amount: null,
|
||||
status: '',
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
|
||||
export const validateFormReward = (
|
||||
formField: typeof initialStateReward,
|
||||
setErrors: React.Dispatch<React.SetStateAction<Record<string, string>>>
|
||||
) => {
|
||||
const requiredFields = [
|
||||
{ key: 'name', label: 'Name' },
|
||||
{ key: 'type', label: 'Type' },
|
||||
{ key: 'amount', label: 'Amount' },
|
||||
{ key: 'status', label: 'Status' }
|
||||
];
|
||||
|
||||
const newErrors: Record<string, string> = {};
|
||||
let isValid = true;
|
||||
|
||||
requiredFields.forEach(({ key, label }) => {
|
||||
if (
|
||||
formField[key as keyof typeof formField] === '' ||
|
||||
formField[key as keyof typeof formField] === null ||
|
||||
formField[key as keyof typeof formField] === undefined
|
||||
) {
|
||||
newErrors[key] = `${label} is required`;
|
||||
toast.error(`${label} is required`);
|
||||
isValid = false;
|
||||
}
|
||||
});
|
||||
|
||||
setErrors(newErrors);
|
||||
return isValid;
|
||||
};
|
||||
@ -159,8 +159,6 @@ const ManageRewardContextProvider = ({ children }: { children: React.ReactNode }
|
||||
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
|
||||
filter: JSON.stringify(filter)
|
||||
});
|
||||
// console.log('API Response:', response?.data.list);
|
||||
// console.log('reward list :', response);
|
||||
return { data: response?.data.list, totalCount: response?.data.total_count };
|
||||
} catch (error) {
|
||||
console.error('Error fethcing reward', error);
|
||||
@ -185,7 +183,7 @@ const ManageRewardContextProvider = ({ children }: { children: React.ReactNode }
|
||||
pagination={{ size: 5 }}
|
||||
toolbar={<ListToolbar />}
|
||||
layout={{ card: true }}
|
||||
sorting={[{ id: 'id', desc: false }]}
|
||||
sorting={[{ id: 'created_at', desc: true }]}
|
||||
serverSide={true}
|
||||
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
||||
getRewardList(pageIndex, pageSize, sorting, columnFilters)
|
||||
|
||||
@ -0,0 +1,187 @@
|
||||
import { useTransactionContext } from '../hooks/useTransactionContext';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import {
|
||||
Dialog,
|
||||
DialogBody,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
} from '@/components/ui/dialog';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||
import { toast } from 'sonner';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||
import Swal from 'sweetalert2'; // ✅ IMPORT sweetalert2
|
||||
|
||||
const API_URL = apiConfig.transaction;
|
||||
|
||||
const RollbackTransaction = () => {
|
||||
const { GetData, PostData } = useCallApi();
|
||||
const { reload } = useDataGrid();
|
||||
|
||||
const {
|
||||
showRollbackDialog,
|
||||
setShowRollbackDialog,
|
||||
selectedTransactionId,
|
||||
} = useTransactionContext();
|
||||
|
||||
|
||||
const [formField, setFormField] = useState({
|
||||
transaction_code: '',
|
||||
notes: '',
|
||||
pin: ''
|
||||
});
|
||||
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: '',
|
||||
});
|
||||
|
||||
const doApproval = useCallback(
|
||||
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
// ✅ TUTUP Dialog sebelum munculkan SweetAlert
|
||||
setShowRollbackDialog(false);
|
||||
|
||||
// ✅ TAMPILKAN SWEETALERT
|
||||
const result = await Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
text: "You want to save changes?",
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Yes, save it!',
|
||||
cancelButtonText: 'Cancel'
|
||||
});
|
||||
|
||||
if (result.isConfirmed) {
|
||||
// ✅ Kalau tekan YES, baru tembak API
|
||||
const response = await PostData(`${API_URL}/transaction/rollback`, {
|
||||
id_transaction: selectedTransactionId,
|
||||
notes: formField.notes,
|
||||
pin: formField.pin,
|
||||
});
|
||||
|
||||
// console.log(response?.message?.error?.message,response?.status);
|
||||
|
||||
if (response?.status === false) {
|
||||
// console.log(response);
|
||||
toast.error(JSON.stringify(response));
|
||||
return;
|
||||
}
|
||||
|
||||
if (response?.status) {
|
||||
toast.success('Success Update Approval');
|
||||
const createActivity = {
|
||||
module: 'History Transaction',
|
||||
description: `Rollback Transaction for transaction => ${selectedTransactionId}`,
|
||||
action: 'U',
|
||||
};
|
||||
doSaveLogActivity(createActivity);
|
||||
reload();
|
||||
}
|
||||
} else {
|
||||
// ✅ Kalau tekan Cancel
|
||||
console.log('User cancelled');
|
||||
}
|
||||
},
|
||||
[formField]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (showRollbackDialog) {
|
||||
setFormField({
|
||||
transaction_code: '',
|
||||
notes: '',
|
||||
pin: ''
|
||||
});
|
||||
setAlert({ show: false, message: '' });
|
||||
}
|
||||
}, [showRollbackDialog]);
|
||||
|
||||
|
||||
return (
|
||||
<Dialog open={showRollbackDialog} onOpenChange={setShowRollbackDialog}>
|
||||
<DialogContent className="container-fixed max-w-[1024px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Rollback Transaction</DialogTitle>
|
||||
</DialogHeader>
|
||||
<DialogDescription></DialogDescription>
|
||||
<DialogBody>
|
||||
<form onSubmit={doApproval}>
|
||||
<div className="card-body grid gap-5 p-0">
|
||||
<div className="w-full">
|
||||
<div className="flex items-center flex-wrap gap-2.5 mt-4">
|
||||
<label className="form-label max-w-56">Notes</label>
|
||||
<div className="grow">
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="Notes"
|
||||
name="notes"
|
||||
id="notes"
|
||||
value={formField.notes}
|
||||
onChange={(e) =>
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
notes: e.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center flex-wrap gap-2.5 mt-3 mb-3">
|
||||
<label className="form-label max-w-56">PIN</label>
|
||||
<div className="grow">
|
||||
<Input
|
||||
required
|
||||
type="password"
|
||||
placeholder="PIN"
|
||||
name="pin"
|
||||
id="pin"
|
||||
value={formField.pin}
|
||||
onChange={(e) =>
|
||||
setFormField((prev) => ({
|
||||
...prev,
|
||||
pin: e.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{alert.show && (
|
||||
<div className="mt-4">
|
||||
<span className="inline-block bg-red-100 text-red-800 text-sm font-medium px-4 py-2 rounded-md">
|
||||
{alert.message}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
<div className="flex justify-end">
|
||||
<Button className="btn btn-primary" type="submit">
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</DialogBody>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default RollbackTransaction;
|
||||
@ -8,6 +8,7 @@ import ListToolbar from '../blocks/ListToolbar';
|
||||
import { useNavigate } from 'react-router';
|
||||
import DetailTransaction from '../blocks/DetailTransaction';
|
||||
import ResendTransaction from '../blocks/ResendTransaction';
|
||||
import RollbackTransaction from '../blocks/RollbackTransaction';
|
||||
|
||||
interface TransactionProps {
|
||||
id: number;
|
||||
@ -25,6 +26,8 @@ interface ContextProps {
|
||||
) => Promise<{ data: TransactionProps[]; totalCount: number } | undefined>;
|
||||
showDetailDialog: boolean;
|
||||
setShowDetailDialog: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
showRollbackDialog: boolean;
|
||||
setShowRollbackDialog: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
selectedTransactionId: number | null;
|
||||
setSelectedTransactionId: React.Dispatch<React.SetStateAction<number | null>>;
|
||||
}
|
||||
@ -33,6 +36,8 @@ const initialProps: ContextProps = {
|
||||
getTransactionLists: async () => ({ data: [], totalCount: 0 }),
|
||||
showDetailDialog: false,
|
||||
setShowDetailDialog: () => { },
|
||||
showRollbackDialog: false,
|
||||
setShowRollbackDialog: () => { },
|
||||
selectedTransactionId: null,
|
||||
setSelectedTransactionId: () => { }
|
||||
};
|
||||
@ -42,6 +47,7 @@ const API_URL = apiConfig.transaction;
|
||||
|
||||
const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const [showDetailDialog, setShowDetailDialog] = useState(false);
|
||||
const [showRollbackDialog, setShowRollbackDialog] = useState(false);
|
||||
const [selectedTransactionId, setSelectedTransactionId] = useState<number | null>(null);
|
||||
const [transaction, setTransaction] = useState<TransactionProps[]>([]);
|
||||
const { GetData } = useCallApi();
|
||||
@ -222,7 +228,7 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
enableSorting: false,
|
||||
enableHiding: false,
|
||||
cell: (data) => {
|
||||
const row = data.row.original;
|
||||
const row = data.row.original;
|
||||
const isVisible = (row.status === 'F' ? true : false || row.status === 'P' ? true : false) && row.status_approve !== 'W' ? true : false;
|
||||
return (
|
||||
<div key={`actions-${row.id}`}>
|
||||
@ -235,6 +241,17 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
>
|
||||
<KeenIcon icon="eye" />
|
||||
</button>
|
||||
|
||||
<button
|
||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||
onClick={() => {
|
||||
setSelectedTransactionId(row.id);
|
||||
setShowRollbackDialog(true);
|
||||
}}
|
||||
disabled={row.status !== 'C'}
|
||||
>
|
||||
<KeenIcon icon="notepad-edit" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
@ -339,6 +356,8 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
getTransactionLists,
|
||||
showDetailDialog,
|
||||
setShowDetailDialog,
|
||||
showRollbackDialog,
|
||||
setShowRollbackDialog,
|
||||
selectedTransactionId,
|
||||
setSelectedTransactionId
|
||||
}}
|
||||
@ -358,6 +377,7 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
}
|
||||
>
|
||||
<DetailTransaction />
|
||||
<RollbackTransaction />
|
||||
<ResendTransaction
|
||||
isOpen={false}
|
||||
onClose={() => console.log('Retry Transaction closed')}
|
||||
|
||||
359
src/pages/transaction/withdrawl-saldo/TransactionWithdraw.tsx
Normal file
359
src/pages/transaction/withdrawl-saldo/TransactionWithdraw.tsx
Normal file
@ -0,0 +1,359 @@
|
||||
import { Alert, Container, DataGridInner } from '@/components';
|
||||
import { TransactionWithdrawProvider } from './hooks/TransactionWithdrawContext';
|
||||
import { Breadcrumbs, Link } from '@mui/material';
|
||||
import { Helmet } from 'react-helmet';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { toast } from 'sonner';
|
||||
import { getAuth } from '@/auth';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
|
||||
const TransactionWithdraw = () => {
|
||||
const initialForm: {
|
||||
msisdn: string;
|
||||
amount: string;
|
||||
pin: string;
|
||||
purpose: string;
|
||||
} = {
|
||||
msisdn: '',
|
||||
amount: '',
|
||||
pin: '',
|
||||
purpose: ''
|
||||
};
|
||||
|
||||
const [form, setForm] = useState(initialForm);
|
||||
const [wallets, setWallets] = useState([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [customerMsisdn, setCustomerMsisdn] = useState<{ value: string; label: string }[]>([]);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
const [dropdownOpen, setDropdownOpen] = useState(false);
|
||||
const { GetData, PostData } = useCallApi();
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [showConfirmation, setShowConfirmation] = useState(false);
|
||||
const parsedUser = getAuth()?.user;
|
||||
const API_URL = apiConfig.transaction;
|
||||
const API_URL_WALLET = apiConfig.service_wallet;
|
||||
const API_URL_CUSTOMER = apiConfig.service_customer;
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
|
||||
const fetchWallets = async () => {
|
||||
try {
|
||||
const response = await GetData(
|
||||
`${API_URL_WALLET}/dashboard/balance/account/${parsedUser.customer.id}`,
|
||||
{}
|
||||
);
|
||||
if (response?.status === true) {
|
||||
setWallets(response.data || []);
|
||||
} else {
|
||||
toast.warning(response?.message || 'Failed to fetch wallet data');
|
||||
}
|
||||
} catch (error) {
|
||||
toast.warning('Failed to fetch wallet data');
|
||||
}
|
||||
};
|
||||
|
||||
const fetchCustomerMsisdn = async (sorting: any, filterValue: string) => {
|
||||
const filter: any =
|
||||
filterValue.trim().length === 0
|
||||
? {}
|
||||
: {
|
||||
or: [
|
||||
{ msisdn: { like: `%${filterValue}%` } },
|
||||
{ fullname: { like: `%${filterValue}%` } }
|
||||
]
|
||||
};
|
||||
|
||||
const query: any = {
|
||||
limit: 100,
|
||||
page: 1,
|
||||
with_deleted: false,
|
||||
order_field: sorting[0].id,
|
||||
order_direction: sorting[0].desc ? 'DESC' : 'ASC'
|
||||
};
|
||||
|
||||
if (filter && Object.keys(filter).length > 0) {
|
||||
query.filter = JSON.stringify(filter);
|
||||
// query.page = page + 1;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, query);
|
||||
setCustomerMsisdn(
|
||||
response?.data.list.map((item: any) => ({
|
||||
value: item.msisdn,
|
||||
label: `${item.msisdn} - ${item.fullname}`
|
||||
}))
|
||||
);
|
||||
} catch (error) {
|
||||
toast.error('Failed to fetch customer msisdn');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const doPostData = async (form: typeof initialForm) => {
|
||||
setIsSubmitting(true);
|
||||
|
||||
try {
|
||||
let response = await PostData(`${API_URL}/transaction/transfer`, {
|
||||
msisdn_destination: form.msisdn,
|
||||
amount: form.amount,
|
||||
pin: form.pin,
|
||||
purpose: form.purpose,
|
||||
id_transaction_type: "20c8a690-dc02-463d-b391-324184d1fefa",
|
||||
id_origin_customer: getAuth()?.id
|
||||
});
|
||||
if (response?.status == true) {
|
||||
await fetchWallets();
|
||||
toast.success('Success Request Topup');
|
||||
} else {
|
||||
toast.error(`${response?.message?.message}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
const errorMessage =
|
||||
error?.response?.data?.message || error?.message || 'Something went wrong';
|
||||
toast.error(errorMessage);
|
||||
setAlert({ show: true, message: errorMessage });
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
setShowConfirmation(false);
|
||||
ResetForm();
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (form.amount == '' || form.msisdn == '' || form.pin == '') {
|
||||
setAlert({
|
||||
show: true,
|
||||
message: 'Please fill in all required fields.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
setAlert({ show: false, message: '' });
|
||||
setShowConfirmation(true);
|
||||
// TODO: Kirim ke backend atau proses lainnya
|
||||
};
|
||||
|
||||
const ResetForm = () => {
|
||||
setForm(initialForm);
|
||||
setAlert({ show: false, message: '' });
|
||||
setSearchTerm('');
|
||||
};
|
||||
|
||||
const handleCancelSubmit = () => {
|
||||
setShowConfirmation(false);
|
||||
};
|
||||
|
||||
const handleMsisdnSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setIsLoading(true);
|
||||
setSearchTerm(e.target.value);
|
||||
setDropdownOpen(true);
|
||||
const timer = setTimeout(() => {
|
||||
fetchCustomerMsisdn([{ id: 'msisdn', desc: false }], e.target.value);
|
||||
}, 500);
|
||||
return () => clearTimeout(timer);
|
||||
};
|
||||
|
||||
const handleMsisdnSelect = (msisdn: string) => {
|
||||
setForm({ ...form, msisdn });
|
||||
setDropdownOpen(false);
|
||||
setSearchTerm(msisdn);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchWallets();
|
||||
fetchCustomerMsisdn([{ id: 'msisdn', desc: false }], '');
|
||||
|
||||
const handleClickOutside = (event: any) => {
|
||||
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
|
||||
setDropdownOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const filteredMsisdn = customerMsisdn
|
||||
.filter((item) => item.label.toLowerCase().includes(searchTerm.toLowerCase()))
|
||||
.slice(0, 10);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Helmet>
|
||||
<title>TPAY | Transaction Withdraw Saldo</title>
|
||||
</Helmet>
|
||||
<TransactionWithdrawProvider>
|
||||
<Container className="mb-7">
|
||||
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">
|
||||
MANAGE TRANSACTION WITHDRAW SALDO
|
||||
</h1>
|
||||
<Breadcrumbs sx={{ mb: 2 }}>
|
||||
<Link underline="none" color="inherit" href="/">
|
||||
<span className="text-sm hover:underline">Dashboard</span>
|
||||
</Link>
|
||||
<Link underline="none" color="inherit">
|
||||
<span className="text-sm">Transaction</span>
|
||||
</Link>
|
||||
<Link underline="none" color="inherit">
|
||||
<span className="text-sm">Withdraw Saldo</span>
|
||||
</Link>
|
||||
</Breadcrumbs>
|
||||
{/* Wallet Section */}
|
||||
<div className="mb-6">
|
||||
<h2 className="text-md font-semibold text-gray-700 mb-3">Your Wallets</h2>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
|
||||
{wallets.map((wallet: any) => (
|
||||
<div key={wallet.id_wallet} className="border rounded-lg p-4 bg-white">
|
||||
<p className="text-sm text-gray-500">{wallet.wallet}</p>
|
||||
<p className="text-lg font-semibold text-green-600">
|
||||
{new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(
|
||||
wallet.amount
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Container className="flex items-center justify-center">
|
||||
<div className="card max-w-[750px] w-full">
|
||||
<div className="card-body p-10">
|
||||
{alert.show && (
|
||||
<Alert variant="danger">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
)}
|
||||
{/* form */}
|
||||
<form onSubmit={handleSubmit} className="space-y-6 mt-5">
|
||||
<div className="relative" ref={dropdownRef}>
|
||||
<label htmlFor="msisdn">MSISDN (Phone Number)</label>
|
||||
<span className="text-red-500">*</span>
|
||||
<div className="relative">
|
||||
<Input
|
||||
id="msisdn"
|
||||
type="text"
|
||||
value={searchTerm}
|
||||
onChange={handleMsisdnSearch}
|
||||
placeholder="Search MSISDN"
|
||||
onClick={() => setDropdownOpen(true)}
|
||||
/>
|
||||
{dropdownOpen && (
|
||||
<div className="absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-y-auto">
|
||||
{filteredMsisdn.length > 0 ? (
|
||||
filteredMsisdn.map((item, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="px-4 py-2 hover:bg-gray-100 cursor-pointer"
|
||||
onClick={() => handleMsisdnSelect(item.value)}
|
||||
>
|
||||
{item.label}
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<div className="px-4 py-2 text-gray-500">
|
||||
{isLoading ? 'Loading...' : 'No results found'}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="amount">Amount</label>
|
||||
<span className="text-red-500">*</span>
|
||||
<Input
|
||||
id="topupAmount"
|
||||
type="number"
|
||||
min={0}
|
||||
value={form.amount}
|
||||
onChange={(e) => {
|
||||
const value = Number(e.target.value);
|
||||
if (value >= 0) {
|
||||
setForm({ ...form, amount: String(value) });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="pin">PIN</label>
|
||||
<span className="text-red-500">*</span>
|
||||
<Input
|
||||
id="pin"
|
||||
type="password"
|
||||
value={form.pin}
|
||||
onChange={(e) => setForm({ ...form, pin: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="pin">Purpose</label>
|
||||
<span className="text-red-500">*</span>
|
||||
<Input
|
||||
id="purpose"
|
||||
type="text"
|
||||
value={form.purpose}
|
||||
onChange={(e) => setForm({ ...form, purpose: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Button type="button" onClick={handleSubmit}>
|
||||
Submit
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</Container>
|
||||
|
||||
{showConfirmation && (
|
||||
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
|
||||
<div className="bg-white p-6 rounded-lg shadow-lg max-w-md w-full">
|
||||
<h3 className="text-lg font-semibold mb-4">Confirm Transaction</h3>
|
||||
<p className="mb-6">
|
||||
Are you sure you want to withdraw saldo of{' '}
|
||||
<span className="font-semibold">
|
||||
{new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(
|
||||
Number(form.amount)
|
||||
)}{' '}
|
||||
</span>
|
||||
?
|
||||
</p>
|
||||
<div className="flex justify-end space-x-3">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={handleCancelSubmit}
|
||||
className="border-gray-300 text-gray-700"
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={() => doPostData(form)} disabled={isSubmitting}>
|
||||
{isSubmitting ? (
|
||||
<RefreshCw className="animate-spin h-8 w-8 text-white mx-3" />
|
||||
) : (
|
||||
'Confirm'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Container>
|
||||
</TransactionWithdrawProvider>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default TransactionWithdraw;
|
||||
61
src/pages/transaction/withdrawl-saldo/blocks/ListToolbar.tsx
Normal file
61
src/pages/transaction/withdrawl-saldo/blocks/ListToolbar.tsx
Normal file
@ -0,0 +1,61 @@
|
||||
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useCallback, useState, useEffect } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
const ListToolbar = () => {
|
||||
const { table, reload } = useDataGrid();
|
||||
|
||||
// Set the initial state for trxDate
|
||||
const [trxDate, settrxDate] = useState({ from: '', to: '' });
|
||||
|
||||
// Function to format date to YYYY-MM-DD
|
||||
const formatDate = (date: Date): string => {
|
||||
return date.toISOString().split('T')[0];
|
||||
};
|
||||
|
||||
// useEffect to set the default date values
|
||||
useEffect(() => {
|
||||
const today = new Date();
|
||||
const nextWeek = new Date(today);
|
||||
nextWeek.setDate(today.getDate() + 7);
|
||||
|
||||
settrxDate({
|
||||
from: formatDate(today), // Set 'from' to today
|
||||
to: formatDate(nextWeek), // Set 'to' to 7 days later
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleFilterData = useCallback(() => {
|
||||
try {
|
||||
table.getColumn('transaction_date')?.setFilterValue(trxDate);
|
||||
} catch (error) {
|
||||
toast.error('Error applying filter');
|
||||
console.error('Error applying filter:', error);
|
||||
}
|
||||
}, [trxDate, table]);
|
||||
|
||||
useEffect(() => {
|
||||
if (trxDate.from && trxDate.to) {
|
||||
handleFilterData();
|
||||
}
|
||||
}, [trxDate]);
|
||||
|
||||
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 gap-3 items-center ml-auto">
|
||||
<DefaultTooltip title={'Refresh'} placement={'top'}>
|
||||
<Button variant="outline" className="h-7.5" onClick={() => reload()}>
|
||||
<KeenIcon icon="arrows-circle" />
|
||||
</Button>
|
||||
</DefaultTooltip>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ListToolbar;
|
||||
@ -0,0 +1,80 @@
|
||||
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||
import { Toaster } from '@/components/ui/sonner';
|
||||
import { toast } from 'sonner';
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { ColumnDef } from '@tanstack/react-table';
|
||||
import { createContext, useCallback, useMemo, useState } from 'react';
|
||||
import ListToolbar from '../blocks/ListToolbar';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import moment from 'moment';
|
||||
|
||||
interface TransactionWithdrawProps {
|
||||
id: string;
|
||||
customers_id: string;
|
||||
group_id: string;
|
||||
username: string;
|
||||
fullname: string;
|
||||
email: string;
|
||||
status: string;
|
||||
created_at: Date;
|
||||
}
|
||||
|
||||
interface ContextProps {
|
||||
|
||||
}
|
||||
|
||||
const initialProps: ContextProps = {
|
||||
|
||||
};
|
||||
|
||||
const TransactionWithdrawContext = createContext<ContextProps>(initialProps);
|
||||
const API_URL = apiConfig.service_customer;
|
||||
|
||||
type StatusCode = 'W' | 'Y' | 'N' | 'T';
|
||||
|
||||
interface StatusInfo {
|
||||
label: string;
|
||||
bg: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
const statusMap: Record<StatusCode, StatusInfo> = {
|
||||
W: { label: 'Waiting Approval', bg: 'bg-yellow-100', text: 'text-yellow-600' },
|
||||
T: { label: 'No Need', bg: 'bg-blue-100', text: 'text-blue-600' },
|
||||
N: { label: 'Reject', bg: 'bg-red-100', text: 'text-red-600' },
|
||||
Y: { label: 'Approve', bg: 'bg-green-100', text: 'text-green-600' },
|
||||
};
|
||||
|
||||
export const renderStatusBadge = (statusRaw: string | null | undefined) => {
|
||||
const status = statusRaw as StatusCode;
|
||||
const { label, bg, text } = statusMap[status] ?? {
|
||||
label: 'Unknown',
|
||||
bg: 'bg-gray-100',
|
||||
text: 'text-gray-600',
|
||||
};
|
||||
|
||||
return (
|
||||
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${bg} ${text}`}>
|
||||
{label}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
// const { reload } = useDataGrid();
|
||||
|
||||
const TransactionWithdrawProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
|
||||
return (
|
||||
<TransactionWithdrawContext.Provider
|
||||
value={{}}
|
||||
>
|
||||
<Toaster expand visibleToasts={9} duration={3000} />
|
||||
<div>
|
||||
{children}
|
||||
</div>
|
||||
</TransactionWithdrawContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { TransactionWithdrawProvider, TransactionWithdrawContext };
|
||||
export type { TransactionWithdrawProps };
|
||||
2
src/pages/transaction/withdrawl-saldo/hooks/index.tsx
Normal file
2
src/pages/transaction/withdrawl-saldo/hooks/index.tsx
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './TransactionWithdrawContext';
|
||||
export * from './useTransactionWithdrawContext';
|
||||
@ -0,0 +1,12 @@
|
||||
import { useContext } from 'react';
|
||||
import { TransactionWithdrawContext } from './TransactionWithdrawContext';
|
||||
|
||||
const useTransactionWithdrawContext = () => {
|
||||
const context = useContext(TransactionWithdrawContext);
|
||||
|
||||
if (!context) throw new Error('useTransactionWithdrawContext must be used within AuthProvider');
|
||||
|
||||
return context;
|
||||
};
|
||||
|
||||
export { useTransactionWithdrawContext };
|
||||
@ -2,7 +2,6 @@ import { apiConfig } from '@/config/api.config';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useManageTransferTypeContext } from '../hooks/useManageTransferTypeContext';
|
||||
import {
|
||||
Alert,
|
||||
Container,
|
||||
DataGridColumnHeader,
|
||||
DataGridInner,
|
||||
@ -65,17 +64,14 @@ const AddDialog = () => {
|
||||
const [customers, setCustomers] = useState<CustomerProps[]>([]);
|
||||
const { reload } = useDataGrid();
|
||||
const { PostData, PutData } = useCallApi();
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const initialState = {
|
||||
name: '',
|
||||
description: '',
|
||||
wallet_origin: '',
|
||||
wallet_destination: '',
|
||||
|
||||
minimum_amount: 0,
|
||||
maximum_amount: 0,
|
||||
max_transaction_per_day: 0,
|
||||
@ -92,38 +88,39 @@ const AddDialog = () => {
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const parsedUser = getAuth()?.user;
|
||||
|
||||
const validateForm = () => {
|
||||
const requiredFields = [
|
||||
'name',
|
||||
'description',
|
||||
'wallet_origin',
|
||||
'wallet_destination',
|
||||
'status',
|
||||
'status_approval',
|
||||
'status_kind'
|
||||
{ key: 'name', label: 'Transfer Type Name' },
|
||||
{ key: 'description', label: 'Description' },
|
||||
{ key: 'wallet_origin', label: 'From Account' },
|
||||
{ key: 'wallet_destination', label: 'To Account' },
|
||||
{ key: 'status', label: 'Status' },
|
||||
{ key: 'status_approval', label: 'Status Approval' },
|
||||
{ key: 'status_kind', label: 'Status Kind' }
|
||||
];
|
||||
|
||||
const missingFields = requiredFields.filter(
|
||||
(field) =>
|
||||
formField[field as keyof typeof formField] === '' ||
|
||||
formField[field as keyof typeof formField] === null ||
|
||||
formField[field as keyof typeof formField] === undefined
|
||||
);
|
||||
const newErrors: Record<string, string> = {};
|
||||
let isValid = true;
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
setAlert({
|
||||
show: true,
|
||||
message: `Please fill out all required fields: ${missingFields.join(', ')}`
|
||||
});
|
||||
return false;
|
||||
}
|
||||
requiredFields.forEach(({ key, label }) => {
|
||||
if (
|
||||
formField[key as keyof typeof formField] === '' ||
|
||||
formField[key as keyof typeof formField] === null ||
|
||||
formField[key as keyof typeof formField] === undefined
|
||||
) {
|
||||
newErrors[key] = `${label} is required`;
|
||||
toast.error(`${label} is required`);
|
||||
isValid = false;
|
||||
}
|
||||
});
|
||||
|
||||
setAlert({ show: false, message: '' });
|
||||
return true;
|
||||
setErrors(newErrors);
|
||||
return isValid;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
@ -162,20 +159,17 @@ const AddDialog = () => {
|
||||
const response = await PostData(`${API_URL}/transactiontype/create`, data);
|
||||
|
||||
if (response?.status) {
|
||||
setAlert({ show: false, message: '' });
|
||||
return true;
|
||||
} else {
|
||||
setAlert({ show: true, message: response?.message || 'Failed to create transfer type' });
|
||||
toast.error(response?.message || 'Failed to create transfer type');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
setAlert({
|
||||
show: true,
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'An error occurred while creating transfer type'
|
||||
});
|
||||
toast.error(
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'An error occurred while creating transfer type'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@ -226,7 +220,6 @@ const AddDialog = () => {
|
||||
order_direction: 'ASC'
|
||||
};
|
||||
const response = await GetData(`${API_URL_MASTERDATA}/wallet/list`, params);
|
||||
// console.log(response)
|
||||
if (response?.status && response?.data) {
|
||||
setWallets(response.data.list);
|
||||
} else {
|
||||
@ -238,7 +231,13 @@ const AddDialog = () => {
|
||||
if (!showAddDialog) return;
|
||||
fetchWallets();
|
||||
}, [showAddDialog]);
|
||||
// console.log(formField)
|
||||
|
||||
useEffect(() => {
|
||||
if (!showAddDialog) {
|
||||
resetForm();
|
||||
}
|
||||
}, [showAddDialog]);
|
||||
|
||||
return (
|
||||
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||
<DialogContent className="container-fixed max-w-[1080px] flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||
@ -256,7 +255,6 @@ const AddDialog = () => {
|
||||
className="cursor-pointer hover:opacity-100 opacity-50"
|
||||
onClick={() => {
|
||||
handleAddDialog(false);
|
||||
resetForm();
|
||||
}}
|
||||
>
|
||||
<KeenIcon icon="cross" className="text-1.5xl" />
|
||||
@ -265,13 +263,6 @@ const AddDialog = () => {
|
||||
</DialogHeader>
|
||||
<DialogBody className="scrollable-y px-0 pb-0" ref={parentRef}>
|
||||
<div className="flex flex-col px-0">
|
||||
{alert.show && (
|
||||
<div className="sticky top-0 z-10 bg-white p-3">
|
||||
<Alert variant="danger" className="mb-3">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
</div>
|
||||
)}
|
||||
<form action="" onSubmit={handleSubmit}>
|
||||
<div className="card-body grid gap-5 p-0">
|
||||
<div className="w-full">
|
||||
@ -280,16 +271,20 @@ const AddDialog = () => {
|
||||
Transfer Type Name
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Input
|
||||
className="input"
|
||||
className={`input ${errors.name ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.name}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, name: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, name: target.value }));
|
||||
if (target.value) {
|
||||
setErrors((prev) => ({ ...prev, name: '' }));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{errors.name && <span className="text-red-500 text-xs mt-1">{errors.name}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -300,16 +295,20 @@ const AddDialog = () => {
|
||||
Description
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Input
|
||||
className="input"
|
||||
className={`input ${errors.description ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.description}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, description: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, description: target.value }));
|
||||
if (target.value) {
|
||||
setErrors((prev) => ({ ...prev, description: '' }));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{errors.description && <span className="text-red-500 text-xs mt-1">{errors.description}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -390,24 +389,26 @@ const AddDialog = () => {
|
||||
From Account
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.wallet_origin}
|
||||
onValueChange={(wallet_origin) =>
|
||||
setFormField((prev) => ({ ...prev, wallet_origin }))
|
||||
}
|
||||
onValueChange={(wallet_origin) => {
|
||||
setFormField((prev) => ({ ...prev, wallet_origin }));
|
||||
setErrors((prev) => ({ ...prev, wallet_origin: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.wallet_origin ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select Wallet" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{wallets.map((wallet, idx) => (
|
||||
{wallets.map((wallet) => (
|
||||
<SelectItem value={wallet.id} key={wallet.name}>
|
||||
{wallet.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.wallet_origin && <span className="text-red-500 text-xs mt-1">{errors.wallet_origin}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -418,24 +419,26 @@ const AddDialog = () => {
|
||||
To Account
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.wallet_destination}
|
||||
onValueChange={(wallet_destination) =>
|
||||
setFormField((prev) => ({ ...prev, wallet_destination }))
|
||||
}
|
||||
onValueChange={(wallet_destination) => {
|
||||
setFormField((prev) => ({ ...prev, wallet_destination }));
|
||||
setErrors((prev) => ({ ...prev, wallet_destination: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.wallet_destination ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select Wallet" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{wallets.map((wallet, idx) => (
|
||||
{wallets.map((wallet) => (
|
||||
<SelectItem value={wallet.id} key={wallet.id}>
|
||||
{wallet.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.wallet_destination && <span className="text-red-500 text-xs mt-1">{errors.wallet_destination}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -447,14 +450,15 @@ const AddDialog = () => {
|
||||
<span className="text-red-500"> *</span>
|
||||
</label>
|
||||
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.type}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, type: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, type: value }));
|
||||
setErrors((prev) => ({ ...prev, type: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.type ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -480,6 +484,7 @@ const AddDialog = () => {
|
||||
<SelectItem value="IC">Income Merchant</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.type && <span className="text-red-500 text-xs mt-1">{errors.type}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -490,14 +495,15 @@ const AddDialog = () => {
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.status_approval}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status_approval: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status_approval: value }));
|
||||
setErrors((prev) => ({ ...prev, status_approval: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status_approval ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -505,6 +511,7 @@ const AddDialog = () => {
|
||||
<SelectItem value="N">No</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status_approval && <span className="text-red-500 text-xs mt-1">{errors.status_approval}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -515,14 +522,15 @@ const AddDialog = () => {
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.status_kind}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status_kind: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status_kind: value }));
|
||||
setErrors((prev) => ({ ...prev, status_kind: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status_kind ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -534,6 +542,7 @@ const AddDialog = () => {
|
||||
<SelectItem value="N">Top Up Patner</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status_kind && <span className="text-red-500 text-xs mt-1">{errors.status_kind}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -545,14 +554,15 @@ const AddDialog = () => {
|
||||
<span className="text-red-500"> *</span>
|
||||
</label>
|
||||
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -560,6 +570,7 @@ const AddDialog = () => {
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status && <span className="text-red-500 text-xs mt-1">{errors.status}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -587,4 +598,4 @@ const AddDialog = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default AddDialog;
|
||||
export default AddDialog;
|
||||
@ -67,6 +67,10 @@ const EditDialog = () => {
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const [isLoadingWallets, setIsLoadingWallets] = useState(false);
|
||||
|
||||
|
||||
const initialState: {
|
||||
name: string;
|
||||
@ -106,6 +110,7 @@ const EditDialog = () => {
|
||||
setFormField(initialState);
|
||||
setSelectedGroups([]);
|
||||
setAlert({ show: false, message: '' });
|
||||
setErrors({});
|
||||
};
|
||||
|
||||
const handleGroupChange = (groupId: string) => {
|
||||
@ -128,42 +133,33 @@ const EditDialog = () => {
|
||||
|
||||
const validateForm = () => {
|
||||
const requiredFields = [
|
||||
'name',
|
||||
'description',
|
||||
'wallet_origin',
|
||||
'wallet_destination',
|
||||
'status',
|
||||
'status_approval',
|
||||
'status_kind',
|
||||
'type'
|
||||
{ key: 'name', label: 'Transfer Type Name' },
|
||||
{ key: 'description', label: 'Description' },
|
||||
{ key: 'wallet_origin', label: 'From Account' },
|
||||
{ key: 'wallet_destination', label: 'To Account' },
|
||||
{ key: 'status', label: 'Status' },
|
||||
{ key: 'status_approval', label: 'Status Approval' },
|
||||
{ key: 'status_kind', label: 'Status Kind' }
|
||||
];
|
||||
|
||||
const missingFields = requiredFields.filter((field) => {
|
||||
return (
|
||||
formField[field as keyof typeof formField] === '' ||
|
||||
formField[field as keyof typeof formField] === null ||
|
||||
formField[field as keyof typeof formField] === undefined
|
||||
);
|
||||
const newErrors: Record<string, string> = {};
|
||||
let isValid = true;
|
||||
|
||||
requiredFields.forEach(({ key, label }) => {
|
||||
if (
|
||||
formField[key as keyof typeof formField] === '' ||
|
||||
formField[key as keyof typeof formField] === null ||
|
||||
formField[key as keyof typeof formField] === undefined
|
||||
) {
|
||||
newErrors[key] = `${label} is required`;
|
||||
// Show toast for each required field
|
||||
toast.error(`${label} is required`);
|
||||
isValid = false;
|
||||
}
|
||||
});
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
setAlert({
|
||||
show: true,
|
||||
message: `Please fill out all required fields: ${missingFields.join(', ')}`
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (formField.permission.length === 0) {
|
||||
setAlert({
|
||||
show: true,
|
||||
message: 'Please select at least one group permission'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
setAlert({ show: false, message: '' });
|
||||
return true;
|
||||
setErrors(newErrors);
|
||||
return isValid;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -202,7 +198,7 @@ const EditDialog = () => {
|
||||
|
||||
const createActivity = {
|
||||
module: 'Manage Transfer Type',
|
||||
description: `Edit Transfer Type => ${selectedTransferType}`,
|
||||
description: `Edit Transfer Type => ${formField.name}`,
|
||||
action: 'U'
|
||||
};
|
||||
|
||||
@ -227,17 +223,18 @@ const EditDialog = () => {
|
||||
);
|
||||
|
||||
if (response?.status) {
|
||||
setAlert({ show: false, message: '' });
|
||||
return true;
|
||||
} else {
|
||||
setAlert({ show: true, message: response?.message || 'Failed to update transfer type' });
|
||||
toast.error(response?.message || 'Failed to update transfer type');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error(
|
||||
error instanceof Error ? error.message : 'Failed to Update Transfer Type'
|
||||
);
|
||||
setAlert({
|
||||
show: true,
|
||||
message:
|
||||
error instanceof Error ? error.message : 'An error occurred while updating transfer type'
|
||||
message: error instanceof Error ? error.message : 'Failed to Update Transfer Type'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
@ -376,6 +373,37 @@ const EditDialog = () => {
|
||||
resetForm();
|
||||
}
|
||||
}, [showEditDialog]);
|
||||
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 (
|
||||
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
|
||||
@ -418,36 +446,48 @@ const EditDialog = () => {
|
||||
Transfer Type Name
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Input
|
||||
className="input"
|
||||
className={`input ${errors.name ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.name}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, name: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, name: target.value }));
|
||||
if (target.value) {
|
||||
setErrors((prev) => ({ ...prev, name: '' }));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{errors.name && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.name}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<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">
|
||||
Description
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Input
|
||||
className="input"
|
||||
className={`input ${errors.description ? 'border-red-500' : ''}`}
|
||||
type="text"
|
||||
autoComplete="off"
|
||||
value={formField.description}
|
||||
onChange={({ target }) =>
|
||||
setFormField((prev) => ({ ...prev, description: target.value }))
|
||||
}
|
||||
onChange={({ target }) => {
|
||||
setFormField((prev) => ({ ...prev, description: target.value }));
|
||||
if (target.value) {
|
||||
setErrors((prev) => ({ ...prev, description: '' }));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{errors.description && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.description}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -528,24 +568,17 @@ const EditDialog = () => {
|
||||
From Account
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<Select
|
||||
value={formField.wallet_origin}
|
||||
onValueChange={(wallet_origin) =>
|
||||
setFormField((prev) => ({ ...prev, wallet_origin }))
|
||||
}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select Wallet" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{wallets.map((wallet) => (
|
||||
<SelectItem value={wallet.id} key={wallet.id}>
|
||||
{wallet.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<div className="grow flex flex-col">
|
||||
{renderSelectWithLoading(
|
||||
formField.wallet_origin,
|
||||
(value) => setFormField({ ...formField, wallet_origin: value }),
|
||||
wallets,
|
||||
'Select Wallet',
|
||||
isLoadingWallets
|
||||
)}
|
||||
{errors.wallet_origin && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.wallet_origin}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -556,14 +589,15 @@ const EditDialog = () => {
|
||||
To Account
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.wallet_destination}
|
||||
onValueChange={(wallet_destination) =>
|
||||
setFormField((prev) => ({ ...prev, wallet_destination }))
|
||||
}
|
||||
onValueChange={(wallet_destination) => {
|
||||
setFormField((prev) => ({ ...prev, wallet_destination }));
|
||||
setErrors((prev) => ({ ...prev, wallet_destination: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.wallet_destination ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select Wallet" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -574,6 +608,9 @@ const EditDialog = () => {
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.wallet_destination && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.wallet_destination}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -584,14 +621,15 @@ const EditDialog = () => {
|
||||
Status Transaction Type
|
||||
<span className="text-red-500"> *</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.type}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, type: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, type: value }));
|
||||
setErrors((prev) => ({ ...prev, type: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.type ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -617,6 +655,9 @@ const EditDialog = () => {
|
||||
<SelectItem value="IC">Income Merchant</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.type && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.type}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -627,14 +668,15 @@ const EditDialog = () => {
|
||||
Status Approval
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.status_approval}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status_approval: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status_approval: value }));
|
||||
setErrors((prev) => ({ ...prev, status_approval: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status_approval ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -642,6 +684,9 @@ const EditDialog = () => {
|
||||
<SelectItem value="N">No</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status_approval && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status_approval}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -652,14 +697,15 @@ const EditDialog = () => {
|
||||
<span className="text-red-500">*</span>
|
||||
</label>
|
||||
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.status_kind}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status_kind: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status_kind: value }));
|
||||
setErrors((prev) => ({ ...prev, status_kind: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status_kind ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -671,6 +717,9 @@ const EditDialog = () => {
|
||||
<SelectItem value="N">Top Up Patner</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status_kind && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status_kind}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -680,14 +729,15 @@ const EditDialog = () => {
|
||||
Status
|
||||
<span className="text-red-500"> *</span>
|
||||
</label>
|
||||
<div className="grow">
|
||||
<div className="grow flex flex-col">
|
||||
<Select
|
||||
value={formField.status}
|
||||
onValueChange={(value) =>
|
||||
setFormField((prev) => ({ ...prev, status: value }))
|
||||
}
|
||||
onValueChange={(value) => {
|
||||
setFormField((prev) => ({ ...prev, status: value }));
|
||||
setErrors((prev) => ({ ...prev, status: '' }));
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectTrigger className={errors.status ? 'border-red-500' : ''}>
|
||||
<SelectValue placeholder="Select" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
@ -695,6 +745,9 @@ const EditDialog = () => {
|
||||
<SelectItem value="N">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.status && (
|
||||
<span className="text-red-500 text-xs mt-1">{errors.status}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -738,6 +791,13 @@ const EditDialog = () => {
|
||||
</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>
|
||||
@ -761,4 +821,4 @@ const EditDialog = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export { EditDialog };
|
||||
export { EditDialog };
|
||||
@ -346,7 +346,7 @@ const ManageTransferTypeContextProvider = ({ children }: { children: React.React
|
||||
setSearchTerm
|
||||
}}
|
||||
>
|
||||
<Toaster expand visibleToasts={9} duration={3000} />
|
||||
{/* <Toaster expand visibleToasts={9} duration={3000} /> */}
|
||||
|
||||
<div className="px-4">
|
||||
<DataGridProvider
|
||||
|
||||
@ -12,6 +12,7 @@ import Transaction from '@/pages/transaction/history-transaction/Transaction';
|
||||
import ApprovalTransaction from '@/pages/transaction/approval-transaction/ApprovalTransaction';
|
||||
import TransactionTopup from '@/pages/transaction/topup/TransactionTopup';
|
||||
import TransactionDisbursement from '@/pages/transaction/disbursement-saldo/TransactionDisbursement';
|
||||
import TransactionWithdraw from '@/pages/transaction/withdrawl-saldo/TransactionWithdraw';
|
||||
import LogActivityPage from '@/pages/settings/user/log-activity/LogActivityPage';
|
||||
import ManagePositionPage from '@/pages/settings/user/manage-position/ManagePositionPage';
|
||||
import ManageAccount from '@/pages/account/manage-account/ManageAccount';
|
||||
@ -46,6 +47,7 @@ import FeedbackMemberMaster from '@/pages/members/feedback-member/FeedbackMember
|
||||
|
||||
// DISBURSEMENT
|
||||
import HistoryTransactionDisbursement from '@/pages/disbursement/history-transaction/HistoryTransaction';
|
||||
|
||||
// DISBURSEMENT
|
||||
|
||||
const AppRoutingSetup = (): ReactElement => {
|
||||
@ -102,6 +104,7 @@ const AppRoutingSetup = (): ReactElement => {
|
||||
<Route path="/approval-transaction" element={<ApprovalTransaction />} />
|
||||
<Route path="/transaction/topup" element={<TransactionTopup />} />
|
||||
<Route path="/transaction/disbursement-saldo" element={<TransactionDisbursement />} />
|
||||
<Route path="/transaction/withdrawl-saldo" element={<TransactionWithdraw />} />
|
||||
<Route path="/menu/menu-management" element={<ManageMenu />} />
|
||||
<Route path="/menu/welcome" element={<Welcome />} />
|
||||
<Route path="/message/inbox" element={<Inbox />} />
|
||||
|
||||
Reference in New Issue
Block a user