Skip to content

Commit

Permalink
admin/payments: Fix formik extPaymentIntentId being empty string some…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
sashko9807 committed Apr 2, 2024
1 parent 65be49c commit 1c0f7f0
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/components/admin/payments/modals/RefundModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import React, { useState } from 'react'
import React from 'react'
import { useTranslation } from 'next-i18next'

import { Dialog, Typography, DialogTitle, DialogContent, Grid, CardContent } from '@mui/material'
import {
Dialog,
Typography,
DialogTitle,
DialogContent,
Grid,
CardContent,
CircularProgress,
} from '@mui/material'
import { useRefundStripeDonation } from 'service/donation'
import { AlertStore } from 'stores/AlertStore'
import { UseQueryResult, useMutation } from '@tanstack/react-query'
Expand All @@ -17,15 +25,9 @@ import { fromMoney } from 'common/util/money'
export default observer(function RefundModal() {
const { t } = useTranslation('donations')
const { isRefundOpen, hideRefund, selectedRecord } = RefundStore
const { data }: UseQueryResult<TPaymentResponse> = useGetPayment(selectedRecord.id)

const initialValues: StripeRefundRequest = {
extPaymentIntentId: '',
}

if (data) {
initialValues.extPaymentIntentId = data.extPaymentIntentId
}
const { data, isLoading, isError }: UseQueryResult<TPaymentResponse> = useGetPayment(
selectedRecord.id,
)

const refundMutation = useMutation({
mutationFn: useRefundStripeDonation(),
Expand All @@ -35,15 +37,25 @@ export default observer(function RefundModal() {
hideRefund()
},
})
const [loading, setLoading] = useState(false)

if (isLoading) {
return (
<Dialog open={true}>
<CircularProgress />
</Dialog>
)
}
if (isError) {
AlertStore.show(t('alerts.error'), 'error')
return
}

const initialValues: StripeRefundRequest = {
extPaymentIntentId: data.extPaymentIntentId,
}

async function onSubmit(values: StripeRefundRequest) {
setLoading(true)
try {
await refundMutation.mutateAsync(values.extPaymentIntentId)
} finally {
setLoading(false)
}
refundMutation.mutate(values.extPaymentIntentId)
}

return (
Expand Down Expand Up @@ -80,7 +92,11 @@ export default observer(function RefundModal() {
{t('refund.email')} {data?.billingEmail}
</Typography>
<Grid item xs={12} marginTop={3}>
<SubmitButton fullWidth label={t('refund.confirm-button')} loading={loading} />
<SubmitButton
fullWidth
label={t('refund.confirm-button')}
loading={refundMutation.isLoading}
/>
</Grid>
</CardContent>
</GenericForm>
Expand Down

0 comments on commit 1c0f7f0

Please sign in to comment.