[skip ci] fix payload retry transaction on history transaction
This commit is contained in:
@ -3,13 +3,13 @@ import { useCallApi } from '@/hooks';
|
|||||||
import { apiConfig } from '@/config/api.config';
|
import { apiConfig } from '@/config/api.config';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogBody,
|
DialogBody,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
DialogDescription,
|
DialogDescription,
|
||||||
DialogFooter,
|
DialogFooter,
|
||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle
|
DialogTitle
|
||||||
} from '@/components/ui/dialog';
|
} from '@/components/ui/dialog';
|
||||||
import { Alert, useDataGrid } from '@/components';
|
import { Alert, useDataGrid } from '@/components';
|
||||||
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
import { doSaveLogActivity } from '@/actions/GlobalActions';
|
||||||
@ -19,216 +19,219 @@ import { Input } from '@/components/ui/input';
|
|||||||
const API_URL = apiConfig.transaction;
|
const API_URL = apiConfig.transaction;
|
||||||
|
|
||||||
interface ResendTransactionProps {
|
interface ResendTransactionProps {
|
||||||
isOpen: boolean | null;
|
isOpen: boolean | null;
|
||||||
onClose: () => void | null;
|
onClose: () => void | null;
|
||||||
selectedTransactionForResend: any | null; // Adjust the type as per your requirements
|
selectedTransactionForResend: any | null; // Adjust the type as per your requirements
|
||||||
}
|
}
|
||||||
|
|
||||||
const ResendTransaction = ({ isOpen, onClose, selectedTransactionForResend }: ResendTransactionProps) => {
|
const ResendTransaction = ({
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
|
selectedTransactionForResend
|
||||||
|
}: ResendTransactionProps) => {
|
||||||
|
// const { showResendDialog, handleResendDialog, selectedTransactionForResend } = useTransactionContext();
|
||||||
|
const { GetData, PostData } = useCallApi();
|
||||||
|
const { showDetailDialog, setShowDetailDialog, selectedTransactionId } = useTransactionContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const [transactionDetails, setTransactionDetails] = useState<any>(null);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialStatePin = {
|
||||||
|
pin: ''
|
||||||
|
};
|
||||||
|
const [formField, setFormField] = useState(initialStatePin);
|
||||||
|
|
||||||
// const { showResendDialog, handleResendDialog, selectedTransactionForResend } = useTransactionContext();
|
useEffect(() => {
|
||||||
const { GetData, PostData } = useCallApi();
|
// if (showResendDialog) {
|
||||||
const {
|
if (isOpen) {
|
||||||
showDetailDialog,
|
// Reset form fields when dialog opens
|
||||||
setShowDetailDialog,
|
setFormField({
|
||||||
selectedTransactionId
|
|
||||||
} = useTransactionContext();
|
|
||||||
const { reload } = useDataGrid();
|
|
||||||
const [transactionDetails, setTransactionDetails] = useState<any>(null);
|
|
||||||
const [alert, setAlert] = useState({
|
|
||||||
show: false,
|
|
||||||
message: ''
|
|
||||||
});
|
|
||||||
const initialStatePin = {
|
|
||||||
pin: ''
|
pin: ''
|
||||||
};
|
});
|
||||||
const [formField, setFormField] = useState(initialStatePin);
|
setAlert({ show: false, message: '' });
|
||||||
|
// setTransactionDetails(null); // Optional reset
|
||||||
|
}
|
||||||
|
}, [isOpen]);
|
||||||
|
|
||||||
useEffect(() => {
|
// useEffect(() => {
|
||||||
// if (showResendDialog) {
|
// const fetchTransactionDetails = async () => {
|
||||||
if (isOpen) {
|
// if (selectedTransactionForResend) {
|
||||||
// Reset form fields when dialog opens
|
// try {
|
||||||
setFormField({
|
// const response = await GetData(
|
||||||
pin: ''
|
// `${API_URL}/transaction/history/detail/${selectedTransactionForResend}`,
|
||||||
});
|
// {
|
||||||
setAlert({ show: false, message: '' });
|
// id: selectedTransactionForResend,
|
||||||
// setTransactionDetails(null); // Optional reset
|
// }
|
||||||
}
|
// );
|
||||||
}, [isOpen]);
|
// setTransactionDetails(response?.data);
|
||||||
|
// } catch (error) {
|
||||||
|
// console.error('Error fetching transaction', error);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// useEffect(() => {
|
// };
|
||||||
// const fetchTransactionDetails = async () => {
|
|
||||||
// if (selectedTransactionForResend) {
|
|
||||||
// try {
|
|
||||||
// const response = await GetData(
|
|
||||||
// `${API_URL}/transaction/history/detail/${selectedTransactionForResend}`,
|
|
||||||
// {
|
|
||||||
// id: selectedTransactionForResend,
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// setTransactionDetails(response?.data);
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error('Error fetching transaction', error);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// };
|
// if (isOpen && selectedTransactionForResend) {
|
||||||
|
// fetchTransactionDetails();
|
||||||
|
// }
|
||||||
|
// }, [isOpen, selectedTransactionForResend, GetData]);
|
||||||
|
|
||||||
// if (isOpen && selectedTransactionForResend) {
|
// console.log(selectedTransactionForResend);
|
||||||
// fetchTransactionDetails();
|
|
||||||
// }
|
|
||||||
// }, [isOpen, selectedTransactionForResend, GetData]);
|
|
||||||
|
|
||||||
// console.log(selectedTransactionForResend);
|
const doResendTransaction = useCallback(
|
||||||
|
async (data: any | null, pintransactiion: string) => {
|
||||||
|
if (pintransactiion.trim() === '') {
|
||||||
|
setAlert({ show: true, message: 'Please fill pin.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pintransactiion.length < 6) {
|
||||||
|
setAlert({ show: true, message: 'Pin length must be 6 characters long.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const doResendTransaction = useCallback(async (data: any | null, pintransactiion: string) => {
|
if (!data) {
|
||||||
|
toast.error('No Transaction selected');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (pintransactiion.trim() === '') {
|
if (!data.type) {
|
||||||
setAlert({ show: true, message: 'Please fill pin.' });
|
toast.error('No Transaction Type selected');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pintransactiion.length < 6) {
|
let apiEndpoint: string | null | undefined = null;
|
||||||
setAlert({ show: true, message: 'Pin length must be 6 characters long.' });
|
let apiJsonData: any | null | undefined = null;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!data) {
|
switch (data.kind) {
|
||||||
toast.error('No Transaction selected');
|
case 'T':
|
||||||
return;
|
apiEndpoint = `${API_URL}/transaction/transfer`;
|
||||||
}
|
apiJsonData = {
|
||||||
|
id_origin_customer: data.origin_customer.id,
|
||||||
|
// msisdn_destination: data.transfer.destination_customer.msisdn,
|
||||||
|
msisdn_destination: data.transfer.destination_msisdn,
|
||||||
|
id_transaction_type: data.type.id,
|
||||||
|
amount: String(data.transfer.amount),
|
||||||
|
pin: pintransactiion,
|
||||||
|
type: data.transfer.type
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case 'U':
|
||||||
|
apiEndpoint = `${API_URL}/transaction/topup`;
|
||||||
|
apiJsonData = {
|
||||||
|
id_origin_customer: data.origin_customer.id,
|
||||||
|
id_transaction_type: data.type.id,
|
||||||
|
pin_p24: data.transfer.pin_p24,
|
||||||
|
amount: String(data.transfer.amount),
|
||||||
|
pin: pintransactiion
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case 'W':
|
||||||
|
apiEndpoint = `${API_URL}/transaction/withdraw`;
|
||||||
|
apiJsonData = {
|
||||||
|
id_origin_customer: data.origin_customer.id,
|
||||||
|
id_transaction_type: data.type.id,
|
||||||
|
destination_iban: data.transfer.destination_iban,
|
||||||
|
amount: String(data.transfer.amount),
|
||||||
|
pin: pintransactiion
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case 'P':
|
||||||
|
let walletParam = '';
|
||||||
|
apiEndpoint = `${API_URL}/transaction/purchase`;
|
||||||
|
if (data.origin_wallet.name === 'Emoney Account') {
|
||||||
|
walletParam = 'emoney';
|
||||||
|
} else if (data.origin_wallet.name === 'Point Account') {
|
||||||
|
walletParam = 'point';
|
||||||
|
}
|
||||||
|
apiJsonData = {
|
||||||
|
id_origin_customer: data.origin_customer.id,
|
||||||
|
code_product: data.purchase.product.code,
|
||||||
|
wallet: walletParam,
|
||||||
|
destination_number: data.purchase.destination_number,
|
||||||
|
destination_amount: data.purchase.destination_amount ?? 0,
|
||||||
|
pin: pintransactiion,
|
||||||
|
purpose: data.purchase.purpose ?? 'Purchase'
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (!data.type) {
|
if (!apiEndpoint || !apiJsonData) {
|
||||||
toast.error('No Transaction Type selected');
|
toast.error('Invalid transaction');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let apiEndpoint: string | null | undefined = null;
|
const response = await PostData(apiEndpoint, apiJsonData);
|
||||||
let apiJsonData: any | null | undefined = null;
|
|
||||||
|
|
||||||
switch (data.kind) {
|
if (response?.status) {
|
||||||
case 'T':
|
setAlert({ show: false, message: '' });
|
||||||
apiEndpoint = `${API_URL}/transaction/transfer`;
|
// handleResendDialog(false, null);
|
||||||
apiJsonData = {
|
onClose();
|
||||||
id_origin_customer: data.origin_customer.id,
|
setShowDetailDialog(false);
|
||||||
// msisdn_destination: data.transfer.destination_customer.msisdn,
|
reload();
|
||||||
msisdn_destination: data.transfer.destination_msisdn,
|
toast.success('Success Retry Transaction');
|
||||||
id_transaction_type: data.type.id,
|
|
||||||
amount: String(data.transfer.amount),
|
|
||||||
pin: pintransactiion,
|
|
||||||
type: data.transfer.type
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'U':
|
|
||||||
apiEndpoint = `${API_URL}/transaction/topup`;
|
|
||||||
apiJsonData = {
|
|
||||||
id_origin_customer: data.origin_customer.id,
|
|
||||||
id_transaction_type: data.type.id,
|
|
||||||
pin_p24: data.transfer.pin_p24,
|
|
||||||
amount: String(data.transfer.amount),
|
|
||||||
pin: pintransactiion
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'W':
|
|
||||||
apiEndpoint = `${API_URL}/transaction/withdraw`;
|
|
||||||
apiJsonData = {
|
|
||||||
id_origin_customer: data.origin_customer.id,
|
|
||||||
id_transaction_type: data.type.id,
|
|
||||||
destination_iban: data.transfer.destination_iban,
|
|
||||||
amount: String(data.transfer.amount),
|
|
||||||
pin: pintransactiion
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'P':
|
|
||||||
let walletParam = "";
|
|
||||||
apiEndpoint = `${API_URL}/transaction/purchase`;
|
|
||||||
if (data.origin_wallet.name === 'Emoney Account') {
|
|
||||||
walletParam = "emoney";
|
|
||||||
} else if (data.origin_wallet.name === 'Point Account') {
|
|
||||||
walletParam = "point";
|
|
||||||
}
|
|
||||||
apiJsonData = {
|
|
||||||
id_origin_customer: data.origin_customer.id,
|
|
||||||
code_product: data.purchase.code,
|
|
||||||
wallet: walletParam,
|
|
||||||
destination_number: data.purchase.destination_number,
|
|
||||||
destination_amount: data.purchase.destination_amount,
|
|
||||||
pin: pintransactiion,
|
|
||||||
purpose: data.purchase.purpose
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!apiEndpoint || !apiJsonData) {
|
const createActivity = {
|
||||||
toast.error('Invalid transaction');
|
module: 'History Transaction',
|
||||||
return;
|
description: `Retry Transaction => ${data.code}`,
|
||||||
}
|
action: 'U'
|
||||||
|
};
|
||||||
|
|
||||||
const response = await PostData(apiEndpoint, apiJsonData);
|
doSaveLogActivity(createActivity);
|
||||||
|
} else {
|
||||||
|
setAlert({ show: true, message: response?.message });
|
||||||
|
toast.error('Failed Retry Transaction');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[selectedTransactionForResend, PostData]
|
||||||
|
);
|
||||||
|
|
||||||
if (response?.status) {
|
if (!isOpen) return null;
|
||||||
setAlert({ show: false, message: '' });
|
|
||||||
// handleResendDialog(false, null);
|
|
||||||
onClose();
|
|
||||||
setShowDetailDialog(false);
|
|
||||||
reload();
|
|
||||||
toast.success('Success Retry Transaction');
|
|
||||||
|
|
||||||
const createActivity = {
|
return (
|
||||||
module: 'History Transaction',
|
<Dialog open={isOpen} onOpenChange={onClose}>
|
||||||
description: `Retry Transaction => ${data.code}`,
|
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||||
action: 'U'
|
<DialogHeader className="p-0 border-0 block">
|
||||||
};
|
<DialogTitle></DialogTitle>
|
||||||
|
<DialogDescription></DialogDescription>
|
||||||
doSaveLogActivity(createActivity);
|
<Alert variant="warning">
|
||||||
} else {
|
<h3 className="text-lg">Are you sure?</h3>
|
||||||
setAlert({ show: true, message: response?.message.message });
|
<p className="text-sm">You will retry this transaction!</p>
|
||||||
toast.error('Failed Retry Transaction');
|
</Alert>
|
||||||
}
|
{alert.show && (
|
||||||
}, [selectedTransactionForResend, PostData]);
|
<Alert variant="danger" className="my-2">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
if (!isOpen) return null;
|
</Alert>
|
||||||
|
)}
|
||||||
return (
|
</DialogHeader>
|
||||||
<Dialog open={isOpen} onOpenChange={onClose}>
|
<DialogBody>
|
||||||
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
<DialogHeader className="p-0 border-0 block">
|
Pin<span className="text-red-500">*</span>
|
||||||
<DialogTitle></DialogTitle>
|
</label>
|
||||||
<DialogDescription></DialogDescription>
|
<Input
|
||||||
<Alert variant="warning">
|
className="input"
|
||||||
<h3 className="text-lg">Are you sure?</h3>
|
type="password"
|
||||||
<p className="text-sm">You will retry this transaction!</p>
|
value={formField.pin}
|
||||||
</Alert>
|
onChange={(e) => setFormField({ ...formField, pin: e.target.value })}
|
||||||
{alert.show && (
|
/>
|
||||||
<Alert variant="danger">
|
</DialogBody>
|
||||||
<h3>{alert.message}</h3>
|
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||||
</Alert>
|
<button onClick={onClose}>Close</button>
|
||||||
)}
|
{/* <Button variant="outline" onClick={() => handleResendDialog(false, null)}>
|
||||||
</DialogHeader>
|
|
||||||
<DialogBody>
|
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
|
||||||
Pin<span className="text-red-500">*</span>
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
className="input"
|
|
||||||
type="password"
|
|
||||||
value={formField.pin}
|
|
||||||
onChange={(e) => setFormField({ ...formField, pin: e.target.value })}
|
|
||||||
/>
|
|
||||||
</DialogBody>
|
|
||||||
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
|
||||||
<button onClick={onClose}>Close</button>
|
|
||||||
{/* <Button variant="outline" onClick={() => handleResendDialog(false, null)}>
|
|
||||||
Cancel
|
Cancel
|
||||||
</Button> */}
|
</Button> */}
|
||||||
<button className="btn btn-primary" onClick={() => doResendTransaction(selectedTransactionForResend, formField.pin)}>
|
<button
|
||||||
Retry
|
className="btn btn-primary"
|
||||||
</button>
|
onClick={() => doResendTransaction(selectedTransactionForResend, formField.pin)}
|
||||||
</DialogFooter>
|
>
|
||||||
</DialogContent>
|
Retry
|
||||||
</Dialog>
|
</button>
|
||||||
);
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ResendTransaction;
|
export default ResendTransaction;
|
||||||
|
|||||||
Reference in New Issue
Block a user