@@ -3,10 +3,7 @@ import { useDispatch } from "react-redux";
3
3
import PT from "prop-types" ;
4
4
import Modal from "components/Modal" ;
5
5
import Spinner from "components/Spinner" ;
6
- import { makeToast } from "components/ToastrMessage" ;
7
- import { setWorkPeriodPaymentData } from "store/actions/workPeriods" ;
8
- import { loadWorkPeriodAfterPaymentCancel } from "store/thunks/workPeriods" ;
9
- import { cancelWorkPeriodPayment } from "services/workPeriods" ;
6
+ import { cancelWorkPeriodPayment } from "store/thunks/workPeriods" ;
10
7
11
8
/**
12
9
* Displays a Cancel button. Shows a modal with payment cancelling confirmation
@@ -16,61 +13,35 @@ import { cancelWorkPeriodPayment } from "services/workPeriods";
16
13
* @param {Object } props.payment payment object with id, workPeriodId and status
17
14
* @param {() => void } props.removeModal function called when the closing
18
15
* animation of the modal is finished
19
- * @param {number } [props.timeout] timeout the delay after cancelling payment
20
- * after which an attempt will be made to update working period's data from the server
21
16
* @returns {JSX.Element }
22
17
*/
23
- const PaymentModalCancel = ( { payment, removeModal, timeout = 3000 } ) => {
18
+ const PaymentModalCancel = ( { payment, removeModal } ) => {
24
19
const [ isModalOpen , setIsModalOpen ] = useState ( true ) ;
25
- const [ isCancelPending , setIsCancelPending ] = useState ( false ) ;
26
- const [ isCancelSuccess , setIsCancelSuccess ] = useState ( false ) ;
20
+ const [ isProcessing , setIsProcessing ] = useState ( false ) ;
27
21
const dispatch = useDispatch ( ) ;
28
22
const { id : paymentId , workPeriodId : periodId } = payment ;
29
23
30
24
const onApprove = useCallback ( ( ) => {
31
- setIsCancelPending ( true ) ;
25
+ setIsProcessing ( true ) ;
32
26
} , [ ] ) ;
33
27
34
28
const onDismiss = useCallback ( ( ) => {
35
29
setIsModalOpen ( false ) ;
36
30
} , [ ] ) ;
37
31
38
32
useEffect ( ( ) => {
39
- if ( ! isCancelPending ) {
33
+ if ( ! isProcessing ) {
40
34
return ;
41
35
}
42
- cancelWorkPeriodPayment ( paymentId )
43
- . then ( ( paymentData ) => {
44
- dispatch ( setWorkPeriodPaymentData ( paymentData ) ) ;
45
- setIsCancelSuccess ( true ) ;
46
- } )
47
- . catch ( ( error ) => {
48
- makeToast ( error . toString ( ) ) ;
49
- setIsCancelPending ( false ) ;
50
- } ) ;
51
- } , [ isCancelPending , paymentId , dispatch ] ) ;
52
-
53
- useEffect ( ( ) => {
54
- let timeoutId = 0 ;
55
- if ( ! isCancelSuccess ) {
56
- return ;
57
- }
58
- timeoutId = window . setTimeout ( async ( ) => {
59
- timeoutId = 0 ;
60
- await dispatch ( loadWorkPeriodAfterPaymentCancel ( periodId , paymentId ) ) ;
61
- setIsModalOpen ( false ) ;
62
- setIsCancelSuccess ( false ) ;
63
- setIsCancelPending ( false ) ;
64
- } , timeout ) ;
65
- return ( ) => {
66
- if ( timeoutId ) {
67
- clearTimeout ( timeoutId ) ;
68
- }
69
- } ;
70
- } , [ isCancelSuccess , paymentId , periodId , timeout , dispatch ] ) ;
36
+ ( async function ( ) {
37
+ let ok = await dispatch ( cancelWorkPeriodPayment ( periodId , paymentId ) ) ;
38
+ setIsModalOpen ( ! ok ) ;
39
+ setIsProcessing ( false ) ;
40
+ } ) ( ) ;
41
+ } , [ isProcessing , paymentId , periodId , dispatch ] ) ;
71
42
72
43
let title , controls ;
73
- if ( isCancelPending ) {
44
+ if ( isProcessing ) {
74
45
controls = null ;
75
46
title = "Marking as cancelled..." ;
76
47
} else {
@@ -83,13 +54,14 @@ const PaymentModalCancel = ({ payment, removeModal, timeout = 3000 }) => {
83
54
approveText = "Mark as cancelled"
84
55
dismissText = "Cancel cancelling"
85
56
title = { title }
57
+ isDisabled = { isProcessing }
86
58
isOpen = { isModalOpen }
87
59
controls = { controls }
88
60
onApprove = { onApprove }
89
61
onClose = { removeModal }
90
62
onDismiss = { onDismiss }
91
63
>
92
- { isCancelPending ? (
64
+ { isProcessing ? (
93
65
< Spinner />
94
66
) : (
95
67
`Cancelling payment here will only mark it as cancelled in TaaS system.
@@ -107,7 +79,6 @@ PaymentModalCancel.propTypes = {
107
79
workPeriodId : PT . string . isRequired ,
108
80
} ) . isRequired ,
109
81
removeModal : PT . func . isRequired ,
110
- timeout : PT . number ,
111
82
} ;
112
83
113
84
export default PaymentModalCancel ;
0 commit comments