-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckout.js
More file actions
105 lines (81 loc) · 3.63 KB
/
Checkout.js
File metadata and controls
105 lines (81 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import classes from './Checkout.module.css';
import {useRef, useState} from "react";
const isEmpty =value =>value.trim()===""
const isFiveChars =value =>value.trim().length===5
const Checkout = (props) => {
const [formInputsValidity,setFormInputsValidity] = useState({
name:true,
street:true,
city:true,
postalCode :true
})
const nameInputRef =useRef()
const streetInputRef =useRef()
const postalCodeInputRef =useRef()
const cityInputRef =useRef()
const confirmHandler = (event) => {
event.preventDefault();
const enteredName =nameInputRef.current.value
const enteredStreet =streetInputRef.current.value
const enteredPostalCode =postalCodeInputRef.current.value
const enteredCity =cityInputRef.current.value
const enteredNameIsValid = !isEmpty(enteredName)
const enteredStreetIsValid = !isEmpty(enteredStreet)
const enteredPostalCodeIsValid = isFiveChars(enteredPostalCode)
const enteredCityIsValid = !isEmpty(enteredCity)
setFormInputsValidity({
name:enteredNameIsValid,
street: enteredStreetIsValid,
city: enteredCityIsValid,
postalCode: enteredPostalCodeIsValid
})
const formIsValid =enteredNameIsValid && enteredStreetIsValid
&& enteredPostalCodeIsValid && enteredCityIsValid
if(!formIsValid){
return
}
props.onConfirm({
name :enteredName,
street:enteredStreet,
city:enteredCity,
postalCode:enteredPostalCode
})
};
const nameControlClasses =`${classes.control} ${formInputsValidity.name ? "" :classes.invalid}`
const streetControlClasses =`${classes.control} ${formInputsValidity.street ? "" :classes.invalid}`
const postalCodeControlClasses =`${classes.control} ${formInputsValidity.postalCode ? "" :classes.invalid}`
const cityControlClasses =`${classes.control} ${formInputsValidity.city ? "" :classes.invalid}`
return (
<form className={classes.form} onSubmit={confirmHandler}>
<div>
<div className={nameControlClasses}>
<label htmlFor='name'>Your Name</label>
<input type='text' id='name' ref={nameInputRef} />
{!formInputsValidity.name && <p>Please enter a valid name !</p>}
</div>
<div className={streetControlClasses}>
<label htmlFor='street'>Street</label>
<input type='text' id='street' ref={streetInputRef}/>
{!formInputsValidity.street && <p>Please enter a valid street name !</p>}
</div>
<div className={postalCodeControlClasses}>
<label htmlFor='postal'>Postal Code</label>
<input type='text' id='postal' ref={postalCodeInputRef}/>
{!formInputsValidity.postalCode && <p>Please enter a valid postal code !</p>}
</div>
<div className={cityControlClasses}>
<label htmlFor='city'>City</label>
<input type='text' id='city' ref={cityInputRef} />
{!formInputsValidity.city && <p>Please enter a valid city name !</p>}
</div>
<div className={classes.actions}>
<button type='button' onClick={props.onCancel}>
Cancel
</button>
<button className={classes.submit}>Confirm</button>
</div>
</div>
</form>
);
};
export default Checkout;