1
- import { useEffect , useState } from "react" ;
2
1
import { Formik , ErrorMessage } from "formik" ;
3
- import { useHistory } from "react-router-dom" ;
4
2
import * as yup from "yup" ;
5
- import Swal from "sweetalert2" ;
6
3
import moment from "moment" ;
7
4
import { Form , Button } from "react-bootstrap" ;
8
5
import { DatePicker } from "antd" ;
9
6
import TimeSelector from "../TimeSelector/TimeSelector" ;
10
- import { RecaptchaVerifier , signInWithPhoneNumber } from "firebase/auth" ;
11
- import { auth } from "../../database/firebase-config" ;
12
7
13
8
import "./BookingForm.css" ;
14
9
@@ -17,89 +12,12 @@ const koreanPhoneRegex = /^((\+82))((10\d{7,8})|(2\d{8}))$/;
17
12
const schema = yup . object ( ) . shape ( {
18
13
name : yup . string ( ) . required ( "Name is required!" ) ,
19
14
phone : yup . string ( ) . min ( 8 , "*Enter a valid phone number" ) . matches ( koreanPhoneRegex , "*Enter a valid phone number!" ) . required ( "*Phone number is required!" ) ,
20
- email : yup . string ( ) . email ( "Must be a valid email" ) . max ( 255 ) . required ( "*Email is required!" ) ,
15
+ email : yup . string ( ) ,
21
16
date : yup . string ( ) . required ( "*Booking date is required!" ) ,
22
17
time : yup . string ( ) . required ( "*Booking time is required!" ) ,
23
18
} ) ;
24
19
25
20
const BookingForm = ( { onCancel, onConfirm, oldData, slots, onGetSlots, slotStatus, settings } ) => {
26
- const [ phoneIsVerified , setPhoneIsVerified ] = useState ( true ) ;
27
- const [ isVerifyingPhone , setIsVerifyingPhone ] = useState ( true ) ;
28
- const [ phoneVerificationCode , setPhoneVerificationCode ] = useState ( "" ) ;
29
- const [ isRecaptchaVerified , setIsRecaptchaVerified ] = useState ( null ) ;
30
- const [ phoneVerificationError , setPhoneVerificationError ] = useState ( "" ) ;
31
- const [ phoneConfirmationObject , setPhoneConfirmationObject ] = useState ( null ) ;
32
- const history = useHistory ( ) ;
33
-
34
- const handleConfirmButtonClick = ( ) => {
35
- if ( ! phoneIsVerified ) {
36
- Swal . fire ( {
37
- icon : "info" ,
38
- title : "Phone not verified!" ,
39
- text : "Please verify your phone number before confirming your booking." ,
40
- } ) ;
41
- return ;
42
- }
43
- } ;
44
-
45
- const handlePhoneVerification = async ( phoneNumber ) => {
46
- if ( ! koreanPhoneRegex . test ( phoneNumber ) ) {
47
- setPhoneVerificationError ( "Please enter a valid Korean phone number." ) ;
48
- Swal . fire ( {
49
- icon : "info" ,
50
- title : "Invalid phone number!" ,
51
- text : "Please enter a valid Korean phone number starting with +82." ,
52
- } ) ;
53
- return ;
54
- }
55
- setIsVerifyingPhone ( true ) ;
56
- try {
57
- const appVerifier = window . recaptchaVerifier ;
58
- const confirmationResult = await signInWithPhoneNumber ( auth , phoneNumber , appVerifier ) ;
59
- // SMS sent. Prompt user to type the code from the message, then sign the
60
- // user in with confirmationResult.confirm(code).
61
- setPhoneConfirmationObject ( confirmationResult ) ;
62
- } catch ( err ) {
63
- // history.go(0);
64
- Swal . fire ( {
65
- icon : "error" ,
66
- title : "An error occurred!" ,
67
- text : "Please refresh the page and try again." ,
68
- } ) ;
69
- }
70
- } ;
71
-
72
- function handlePhoneCode ( e ) {
73
- e . preventDefault ( ) ;
74
- if ( ! phoneConfirmationObject ) {
75
- //history.go(0);
76
- Swal . fire ( {
77
- icon : "error" ,
78
- title : "An error occurred!" ,
79
- text : "Please refresh the page and try again." ,
80
- } ) ;
81
- return ;
82
- }
83
- phoneConfirmationObject
84
- . confirm ( phoneVerificationCode )
85
- . then ( ( result ) => {
86
- // User signed in successfully with their phone.
87
- const user = result . user ; //not an admin user
88
- setPhoneIsVerified ( true ) ;
89
- setIsVerifyingPhone ( false ) ;
90
- } )
91
- . catch ( ( error ) => {
92
- // User couldn't sign in (bad verification code?)
93
- setPhoneIsVerified ( false ) ;
94
- setIsVerifyingPhone ( false ) ;
95
- Swal . fire ( {
96
- icon : "info" ,
97
- title : "Invalid verification code!" ,
98
- text : "Please enter a valid verification code sent to your phone." ,
99
- } ) ;
100
- } ) ;
101
- }
102
-
103
21
function handleGetSlots ( date ) {
104
22
onGetSlots ( date ) ;
105
23
}
@@ -111,51 +29,6 @@ const BookingForm = ({ onCancel, onConfirm, oldData, slots, onGetSlots, slotStat
111
29
return submittedValue . valueOf ( ) < moment ( ) . add ( - 1 , "day" ) || submittedValue . valueOf ( ) >= moment ( ) . add ( 31 , "day" ) ;
112
30
} ;
113
31
114
- // useEffect(() => {
115
- // if (isRecaptchaVerified !== null && !isRecaptchaVerified) {
116
- // history.push("/");
117
- // return;
118
- // }
119
- // }, [isRecaptchaVerified]);
120
-
121
- // useEffect(() => {
122
- // try {
123
- // window.recaptchaVerifier = new RecaptchaVerifier(
124
- // "sign-in-button",
125
- // {
126
- // size: "invisible",
127
- // callback: (response) => {
128
- // setIsRecaptchaVerified(true);
129
- // // reCAPTCHA solved, allow signInWithPhoneNumber.
130
- // console.log("reCAPTCHA solved, allow signInWithPhoneNumber.");
131
- // //Now handlePhoneVerification(phoneNumber);
132
- // },
133
- // "expired-callback": () => {
134
- // setIsRecaptchaVerified(false);
135
- // console.log("reCAPTCHA expired");
136
- // // history.go(0);
137
- // Swal.fire({
138
- // icon: "error",
139
- // title: "An error occurred!",
140
- // text: "Please refresh the page and try again.",
141
- // });
142
- // },
143
- // },
144
- // auth
145
- // );
146
-
147
- // window.recaptchaVerifier.render().then(function (widgetId) {
148
- // window.recaptchaWidgetId = widgetId;
149
- // });
150
- // } catch (e) {
151
- // console.log("recapture error", e);
152
- // }
153
-
154
- // return () => {
155
- // window.recaptchaVerifier.clear();
156
- // };
157
- // }, []);
158
-
159
32
return (
160
33
< Formik
161
34
validationSchema = { schema }
@@ -178,10 +51,7 @@ const BookingForm = ({ onCancel, onConfirm, oldData, slots, onGetSlots, slotStat
178
51
// validated={!errors}
179
52
onSubmit = { ( e ) => {
180
53
e . preventDefault ( ) ;
181
- if ( phoneIsVerified ) {
182
- handleSubmit ( ) ;
183
- } else {
184
- }
54
+ handleSubmit ( ) ;
185
55
} }
186
56
className = "appointmentForm mx-auto p-3 "
187
57
>
@@ -201,44 +71,14 @@ const BookingForm = ({ onCancel, onConfirm, oldData, slots, onGetSlots, slotStat
201
71
placeholder = "Phone number"
202
72
value = { values . phone }
203
73
onChange = { ( e ) => {
204
- // setPhoneNumber("01098999793") ;
205
- handleChange ( e ) ;
74
+ const value = e . target . value ;
75
+ setFieldValue ( "phone" , value . replace ( / [ ^ 0 - 9 + ] / g , "" ) ) ;
206
76
} }
207
77
isValid = { touched . phone && ! errors . phone }
208
78
/>
209
79
< div className = "text-danger font-italic" >
210
80
< ErrorMessage name = "phone" />
211
81
</ div >
212
- { /* {!phoneIsVerified && !isVerifyingPhone && (
213
- <Button
214
- id="sign-in-button"
215
- onClick={() => {
216
- handlePhoneVerification(values.phone);
217
- }}
218
- variant="outline-primary"
219
- className="mt-2 btn-sm"
220
- >
221
- Verify phone number!
222
- </Button>
223
- )}
224
- {isVerifyingPhone && !phoneIsVerified && <p className="mb-0">Enter verification code on your phone</p>}
225
- {phoneIsVerified && !isVerifyingPhone && <p className="text-success ">Your phone was verified successfully! </p>}
226
- {isVerifyingPhone && (
227
- <div className="d-flex justify-content-between">
228
- <input className="form-control form-control-sm w-50" type="text" value={phoneVerificationCode} onChange={(e) => setPhoneVerificationCode(e.target.value)} />
229
- <button className="btn btn-outline-primary btn-sm w-50 ms-2" onClick={(e) => handlePhoneCode(e)}>
230
- Verify code
231
- </button>
232
- </div>
233
- )} */ }
234
- </ Form . Group >
235
-
236
- < Form . Group className = "mb-3" >
237
- < Form . Label className = "fw-bold" > Email</ Form . Label >
238
- < Form . Control type = "email" name = "email" placeholder = "Email" value = { values . email } onChange = { handleChange } isValid = { touched . email && ! errors . email } />
239
- < div className = "text-danger font-italic" >
240
- < ErrorMessage name = "email" />
241
- </ div >
242
82
</ Form . Group >
243
83
244
84
< Form . Group >
@@ -285,15 +125,7 @@ const BookingForm = ({ onCancel, onConfirm, oldData, slots, onGetSlots, slotStat
285
125
</ Form . Group >
286
126
287
127
< div className = "d-flex justify-content-around p-2" >
288
- < Button
289
- variant = "success"
290
- type = "submit"
291
- className = "w-100 me-1"
292
- // onClick={() => {
293
- // handleConfirmButtonClick();
294
- // }}
295
- // disabled={!phoneIsVerified}
296
- >
128
+ < Button variant = "success" type = "submit" className = "w-100 me-1" >
297
129
Confirm Booking
298
130
</ Button >
299
131
0 commit comments