diff --git a/src/pages/transaction/disbursement-saldo/TransactionDisbursement.tsx b/src/pages/transaction/disbursement-saldo/TransactionDisbursement.tsx index 72c660d..984a8ed 100644 --- a/src/pages/transaction/disbursement-saldo/TransactionDisbursement.tsx +++ b/src/pages/transaction/disbursement-saldo/TransactionDisbursement.tsx @@ -1,31 +1,47 @@ -import { Container, DataGridInner } from '@/components'; +import { Alert, Container, DataGridInner } from '@/components'; import { TransactionDisbursementProvider } from './hooks/TransactionDisbursementContext'; 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 } from 'react'; +import { useState, useEffect } 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 TransactionDisbursement = () => { - const [form, setForm] = useState({ + const initialForm: { + msisdn: string; + amount: string; + pin: string; + } = { msisdn: '', amount: '', pin: '' - }); + }; + + const [form, setForm] = useState(initialForm); const [wallets, setWallets] = useState([]); 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_WALLET = apiConfig.service_wallet; + const [alert, setAlert] = useState({ + show: false, + message: '' + }); useEffect(() => { const fetchWallets = async () => { try { - const response = await GetData(`${API_URL_WALLET}/dashboard/balance/account/${parsedUser.customer.id}`, {}); + const response = await GetData( + `${API_URL_WALLET}/dashboard/balance/account/${parsedUser.customer.id}`, + {} + ); if (response?.status === true) { setWallets(response.data || []); } else { @@ -39,30 +55,50 @@ const TransactionDisbursement = () => { fetchWallets(); }, []); - const handleSubmit = async (e: any) => { - e.preventDefault(); - console.log('Submitted Data:', form); - if (form.amount == '' || form.msisdn == '' || form.pin == '') { - toast.warning('Please fill in all required fields.') - return - } + const doPostData = async (form: typeof initialForm) => { + setIsSubmitting(true); + try { - let requestTopup = await PostData(`${API_URL}/transaction/topup-downline`, { + let response = await PostData(`${API_URL}/transaction/topup-downline`, { msisdn_destination: form.msisdn, amount: form.amount, pin: form.pin - }) - if (requestTopup?.status == true) { - toast.success('Success Request Topup') + }); + if (response?.status == true) { + toast.success('Success Request Topup'); } else { - toast.warning(`${requestTopup?.message}`) + toast.error(`${response?.message?.message}`); } - } catch (error) { - toast.warning('Failed') + } 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); } + }; + + 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 handleCancelSubmit = () => { + setShowConfirmation(false); + }; + return ( <> @@ -70,7 +106,9 @@ const TransactionDisbursement = () => { -

MANAGE TRANSACTION DISBURSEMENT SALDO

+

+ MANAGE TRANSACTION DISBURSEMENT SALDO +

Dashboard @@ -90,51 +128,97 @@ const TransactionDisbursement = () => {

{wallet.wallet}

- {new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(wallet.amount)} + {new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format( + wallet.amount + )}

))} +
+ {alert.show && ( + +

{alert.message}

+
+ )} {/* form */} -
-
- * - setForm({ ...form, msisdn: e.target.value })} - /> -
-
- * - setForm({ ...form, amount: e.target.value })} - /> -
-
- * - setForm({ ...form, pin: e.target.value })} - /> -
-
- -
-
+
+
+ + * + setForm({ ...form, msisdn: e.target.value })} + /> +
+
+ + * + setForm({ ...form, amount: e.target.value })} + /> +
+
+ + * + setForm({ ...form, pin: e.target.value })} + /> +
+
+ +
+
+ + {showConfirmation && ( +
+
+

Confirm Transaction

+

+ Are you sure you want to disbursement saldo of{' '} + + {new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format( + Number(form.amount) + )}{' '} + + ? +

+
+ + +
+
+
+ )}
diff --git a/src/pages/transaction/topup/TransactionTopup.tsx b/src/pages/transaction/topup/TransactionTopup.tsx index a05fb95..1b339a0 100644 --- a/src/pages/transaction/topup/TransactionTopup.tsx +++ b/src/pages/transaction/topup/TransactionTopup.tsx @@ -1,31 +1,46 @@ -import { Container, DataGridInner } from '@/components'; +import { Alert, Container, DataGridInner } from '@/components'; import { TransactionTopupProvider } from './hooks/TransactionTopupContext'; 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 } from 'react'; +import React, { useState, useEffect } 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 TransactionTopup = () => { - const [form, setForm] = useState({ + const initialState: { + topupAmount: string; + pin: string; + } = { topupAmount: '', pin: '' + }; + + const [form, setForm] = useState(initialState); + const [alert, setAlert] = useState({ + show: false, + message: '' }); const [wallets, setWallets] = useState([]); 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_WALLET = apiConfig.service_wallet; useEffect(() => { const fetchWallets = async () => { try { - const response = await GetData(`${API_URL_WALLET}/dashboard/balance/account/${parsedUser.customer.id}`, {}); + const response = await GetData( + `${API_URL_WALLET}/dashboard/balance/account/${parsedUser.customer.id}`, + {} + ); if (response?.status === true) { setWallets(response.data || []); } else { @@ -39,30 +54,49 @@ const TransactionTopup = () => { fetchWallets(); }, []); - const handleSubmit = async (e: any) => { - e.preventDefault(); - console.log('Submitted Data:', form); + const doPostData = async (form: typeof initialState) => { + setIsSubmitting(true); - if (form.pin == '' || form.topupAmount == '') { - toast.warning('Please fill in all required fields.') - return - } try { - let requestTopup = await PostData(`${API_URL}/transaction/request-topup`, { + let response = await PostData(`${API_URL}/transaction/request-topup`, { amount: form.topupAmount, pin: form.pin - }) - if (requestTopup?.status == true) { - toast.success('Success Request Topup') + }); + console.log(response); + if (response?.status == true) { + toast.success('Success Request Topup'); } else { - toast.warning(`${requestTopup?.message}`) + toast.warning(`${response?.message}`); } - } catch (error) { - toast.warning('Failed') + } catch (error: any) { + const errorMessage = + error?.response?.data?.message || error?.message || 'Something went wrong'; + toast.error(errorMessage); + } finally { + setIsSubmitting(false); + setShowConfirmation(false); } + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + // console.log('Submitted Data:', form); + + if (form.pin == '' || form.topupAmount == '') { + 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 handleCancelSubmit = () => { + setShowConfirmation(false); + }; + return ( <> @@ -70,7 +104,9 @@ const TransactionTopup = () => { -

MANAGE TRANSACTION TOPUP REQUEST

+

+ MANAGE TRANSACTION TOPUP REQUEST +

Dashboard @@ -82,6 +118,7 @@ const TransactionTopup = () => { Topup + {/* Wallet Section */}

Your Wallets

@@ -90,7 +127,9 @@ const TransactionTopup = () => {

{wallet.wallet}

- {new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(wallet.amount)} + {new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format( + wallet.amount + )}

))} @@ -99,10 +138,16 @@ const TransactionTopup = () => {
+ {alert.show && ( + +

{alert.message}

+
+ )} {/* form */} -
+
- * + + * { />
- * + + * { />
- +
+ {showConfirmation && ( +
+
+

Confirm Transaction

+

+ Are you sure you want to request a topup of{' '} + + {new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format( + Number(form.topupAmount) + )}{' '} + {' '} + ? +

+
+ + +
+
+
+ )}