8
8
useElements ,
9
9
} from "@stripe/react-stripe-js" ;
10
10
import { navigate } from "@reach/router" ;
11
+ import _ from "lodash" ;
11
12
import { toastr } from "react-redux-toastr" ;
12
13
import { makeStyles } from "@material-ui/core/styles" ;
13
14
import Typography from "@material-ui/core/Typography" ;
@@ -19,6 +20,7 @@ import StripeElement from "../StripeElement";
19
20
import FormField from "../FormField" ;
20
21
import SelectField from "../SelectField" ;
21
22
import ConfirmationModal from "../../../components/ConfirmationModal" ;
23
+ import PaymentResultPopup from "../PaymentResultPopup" ;
22
24
23
25
import "./styles.module.scss" ;
24
26
@@ -50,9 +52,15 @@ const PaymentForm = ({ calculatedAmount }) => {
50
52
const [ formValues , setFormValues ] = useState ( initialFormValues ) ;
51
53
const [ dropdownValue , setDropdownValue ] = useState ( "" ) ;
52
54
const [ processing , setProcessing ] = useState ( false ) ;
55
+ const [ showPaymentResultPopup , setShowPaymentResultPopup ] = useState ( false ) ;
53
56
const [ requestLoading , setRequestLoading ] = useState ( false ) ;
54
- const [ errors , setErrors ] = useState ( { card : true , cardExpire : true , cardCvc : true } ) ;
57
+ const [ errors , setErrors ] = useState ( {
58
+ card : true ,
59
+ cardExpire : true ,
60
+ cardCvc : true ,
61
+ } ) ;
55
62
const [ clicked , setClicked ] = useState ( true ) ;
63
+ const [ projectId , setProjectId ] = useState ( null ) ;
56
64
const stripe = useStripe ( ) ;
57
65
const elements = useElements ( ) ;
58
66
const dispatch = useDispatch ( ) ;
@@ -86,11 +94,11 @@ const PaymentForm = ({ calculatedAmount }) => {
86
94
} ) ;
87
95
} ;
88
96
const handleStripeElementError = ( fieldName , error ) => {
89
- errors [ fieldName ] = error ? true : false
97
+ errors [ fieldName ] = error ? true : false ;
90
98
setErrors ( {
91
99
...errors ,
92
100
} ) ;
93
- }
101
+ } ;
94
102
const handleInputValue = ( e ) => {
95
103
const { name, value } = e . target ;
96
104
setFormValues ( {
@@ -101,6 +109,11 @@ const PaymentForm = ({ calculatedAmount }) => {
101
109
[ name ] : value ,
102
110
} ) ;
103
111
} ;
112
+ const goToTassProject = ( ) => {
113
+ dispatch ( clearSearchedRoles ( ) ) ;
114
+ setShowPaymentResultPopup ( false ) ;
115
+ navigate ( `/taas/myteams/${ projectId } ` ) ;
116
+ } ;
104
117
const handleFormSubmit = async ( e ) => {
105
118
e . preventDefault ( ) ;
106
119
if ( formIsValid ( ) && clicked ) {
@@ -130,12 +143,9 @@ const PaymentForm = ({ calculatedAmount }) => {
130
143
toastr . success ( "Payment is successful" ) ;
131
144
setRequestLoading ( true ) ;
132
145
postTeamRequest ( teamObject )
133
- . then ( ( ) => {
134
- setTimeout ( ( ) => {
135
- dispatch ( clearSearchedRoles ( ) ) ;
136
- // Backend api create project has sync issue, so delay 2 seconds
137
- navigate ( "/taas/myteams" ) ;
138
- } , 2000 ) ;
146
+ . then ( ( res ) => {
147
+ setProjectId ( _ . get ( res , "data.projectId" ) ) ;
148
+ setShowPaymentResultPopup ( true ) ;
139
149
} )
140
150
. catch ( ( err ) => {
141
151
setRequestLoading ( false ) ;
@@ -154,14 +164,16 @@ const PaymentForm = ({ calculatedAmount }) => {
154
164
155
165
const formIsValid = ( fieldValues = formValues ) => {
156
166
// check card valid
157
- const cardValid = ! errors [ 'card' ] && ! errors [ 'cardExpire' ] && ! errors [ 'cvc' ]
167
+ const cardValid =
168
+ ! errors [ "card" ] && ! errors [ "cardExpire" ] && ! errors [ "cvc" ] ;
158
169
const dropdown = dropdownValue === "" ? false : true ;
159
170
const isValid =
160
171
fieldValues . email &&
161
172
fieldValues . name &&
162
173
fieldValues . zipcode &&
163
- dropdown && cardValid
164
- Object . values ( errors ) . every ( ( x ) => x === "" ) ;
174
+ dropdown &&
175
+ cardValid ;
176
+ Object . values ( errors ) . every ( ( x ) => x === "" ) ;
165
177
166
178
return isValid ;
167
179
} ;
@@ -181,9 +193,20 @@ const PaymentForm = ({ calculatedAmount }) => {
181
193
Card Information
182
194
</ Typography >
183
195
</ Box >
184
- < StripeElement onErrorChange = { handleStripeElementError } element = { CardNumberElement } name = "card" icon = "card" />
196
+ < StripeElement
197
+ onErrorChange = { handleStripeElementError }
198
+ element = { CardNumberElement }
199
+ name = "card"
200
+ icon = "card"
201
+ />
185
202
< div styleName = "horizontal" >
186
- < StripeElement onErrorChange = { handleStripeElementError } element = { CardExpiryElement } name = "" width = "150px" name = 'cardExpire' />
203
+ < StripeElement
204
+ onErrorChange = { handleStripeElementError }
205
+ element = { CardExpiryElement }
206
+ name = ""
207
+ width = "150px"
208
+ name = "cardExpire"
209
+ />
187
210
< StripeElement
188
211
className = { classes . cvc }
189
212
name = "cvc"
@@ -217,6 +240,13 @@ const PaymentForm = ({ calculatedAmount }) => {
217
240
</ button >
218
241
</ form >
219
242
< ConfirmationModal open = { requestLoading } isLoading = { requestLoading } />
243
+ < PaymentResultPopup
244
+ open = { showPaymentResultPopup }
245
+ onContinueClick = { goToTassProject }
246
+ onClose = { ( ) => {
247
+ setShowPaymentResultPopup ( false ) ;
248
+ } }
249
+ />
220
250
</ >
221
251
) ;
222
252
} ;
0 commit comments