|
15 | 15 | // - Save the state of the form and restore it when the page first loads, in |
16 | 16 | // case the user accidentally closes the tab before the form is submitted |
17 | 17 | //////////////////////////////////////////////////////////////////////////////// |
18 | | -import React from "react"; |
| 18 | +import React, { useEffect, useState } from "react"; |
19 | 19 | import ReactDOM from "react-dom"; |
20 | 20 | import serializeForm from "form-serialize"; |
21 | 21 |
|
22 | | -class CheckoutForm extends React.Component { |
23 | | - constructor(props) { |
24 | | - super(props); |
| 22 | +function CheckoutForm() { |
| 23 | + const [billingName, setBillingName] = useState("Michael Jackson"); |
| 24 | + const [billingState, setBillingState] = useState("CA"); |
| 25 | + const [shippingName, setShippingName] = useState(""); |
| 26 | + const [shippingState, setShippingState] = useState(""); |
| 27 | + const [shippingSameAsBilling, setShippingSameAsBilling] = useState( |
| 28 | + false |
| 29 | + ); |
25 | 30 |
|
26 | | - this.state = { |
27 | | - shippingSameAsBilling: true, |
28 | | - billingName: "", |
29 | | - billingState: "", |
30 | | - shippingName: "", |
31 | | - shippingState: "" |
32 | | - }; |
33 | | - |
34 | | - this.handleSubmit = event => { |
35 | | - event.preventDefault(); |
36 | | - const values = serializeForm(event.target, { hash: true }); |
37 | | - console.log(values); |
38 | | - }; |
| 31 | + function handleSubmit(event) { |
| 32 | + event.preventDefault(); |
| 33 | + const values = serializeForm(event.target, { hash: true }); |
| 34 | + console.log(values); |
| 35 | + } |
39 | 36 |
|
40 | | - this.handleBeforeUnload = () => { |
41 | | - localStorage.formState = JSON.stringify(this.state); |
42 | | - }; |
| 37 | + // Read the values from localStorage on the initial page load |
| 38 | + useEffect(() => { |
| 39 | + if (localStorage.formValues) { |
| 40 | + const { |
| 41 | + billingName, |
| 42 | + billingState, |
| 43 | + shippingName, |
| 44 | + shippingState, |
| 45 | + shippingSameAsBilling |
| 46 | + } = JSON.parse(localStorage.formValues); |
43 | 47 |
|
44 | | - if (localStorage.formState) { |
45 | | - Object.assign(this.state, JSON.parse(localStorage.formState)); |
| 48 | + setBillingName(billingName); |
| 49 | + setBillingState(billingState); |
| 50 | + setShippingName(shippingName); |
| 51 | + setShippingState(shippingState); |
| 52 | + setShippingSameAsBilling(shippingSameAsBilling); |
46 | 53 | } |
47 | | - } |
| 54 | + }, []); |
48 | 55 |
|
49 | | - componentDidMount() { |
50 | | - window.addEventListener("beforeunload", this.handleBeforeUnload); |
51 | | - } |
| 56 | + // Store the values in localStorage before the page unloads |
| 57 | + useEffect( |
| 58 | + () => { |
| 59 | + function handleBeforeUnload() { |
| 60 | + localStorage.formValues = JSON.stringify({ |
| 61 | + billingName, |
| 62 | + billingState, |
| 63 | + shippingName, |
| 64 | + shippingState, |
| 65 | + shippingSameAsBilling |
| 66 | + }); |
| 67 | + } |
52 | 68 |
|
53 | | - componentWillUnmount() { |
54 | | - window.removeEventListener("beforeunload", this.handleBeforeUnload); |
55 | | - } |
| 69 | + window.addEventListener("beforeunload", handleBeforeUnload); |
56 | 70 |
|
57 | | - render() { |
58 | | - return ( |
59 | | - <div> |
60 | | - <h1>Checkout</h1> |
61 | | - <form onSubmit={this.handleSubmit}> |
62 | | - <fieldset> |
63 | | - <legend>Billing Address</legend> |
64 | | - <p> |
65 | | - <label> |
66 | | - Billing Name:{" "} |
67 | | - <input |
68 | | - type="text" |
69 | | - name="billing_name" |
70 | | - defaultValue={this.state.billingName} |
71 | | - onChange={event => |
72 | | - this.setState({ billingName: event.target.value }) |
73 | | - } |
74 | | - /> |
75 | | - </label> |
76 | | - </p> |
77 | | - {this.state.billingState.length > 2 && ( |
78 | | - <p style={{ color: "red" }}> |
79 | | - Please use the 2-character abbreviation for the state. |
80 | | - </p> |
81 | | - )} |
82 | | - <p> |
83 | | - <label> |
84 | | - Billing State:{" "} |
85 | | - <input |
86 | | - type="text" |
87 | | - size="2" |
88 | | - name="billing_state" |
89 | | - defaultValue={this.state.billingState} |
90 | | - onChange={event => |
91 | | - this.setState({ billingState: event.target.value }) |
92 | | - } |
93 | | - /> |
94 | | - </label> |
| 71 | + return () => { |
| 72 | + window.removeEventListener("beforeunload", handleBeforeUnload); |
| 73 | + }; |
| 74 | + }, |
| 75 | + [ |
| 76 | + billingName, |
| 77 | + billingState, |
| 78 | + shippingName, |
| 79 | + shippingState, |
| 80 | + shippingSameAsBilling |
| 81 | + ] |
| 82 | + ); |
| 83 | + |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + <h1>Checkout</h1> |
| 87 | + <form onSubmit={handleSubmit}> |
| 88 | + <fieldset> |
| 89 | + <legend>Billing Address</legend> |
| 90 | + <p> |
| 91 | + <label> |
| 92 | + Billing Name:{" "} |
| 93 | + <input |
| 94 | + type="text" |
| 95 | + name="billingName" |
| 96 | + defaultValue={billingName} |
| 97 | + onChange={event => setBillingName(event.target.value)} |
| 98 | + /> |
| 99 | + </label> |
| 100 | + </p> |
| 101 | + {billingState.length > 2 && ( |
| 102 | + <p style={{ color: "red" }}> |
| 103 | + Please use the 2-character abbreviation for the state. |
95 | 104 | </p> |
96 | | - </fieldset> |
| 105 | + )} |
| 106 | + <p> |
| 107 | + <label> |
| 108 | + Billing State:{" "} |
| 109 | + <input |
| 110 | + type="text" |
| 111 | + size="3" |
| 112 | + name="billingState" |
| 113 | + defaultValue={billingState} |
| 114 | + onChange={event => setBillingState(event.target.value)} |
| 115 | + /> |
| 116 | + </label> |
| 117 | + </p> |
| 118 | + </fieldset> |
97 | 119 |
|
98 | | - <br /> |
| 120 | + <br /> |
99 | 121 |
|
100 | | - <fieldset> |
| 122 | + <fieldset> |
| 123 | + <label> |
| 124 | + <input |
| 125 | + type="checkbox" |
| 126 | + defaultChecked={shippingSameAsBilling} |
| 127 | + onChange={event => |
| 128 | + setShippingSameAsBilling(event.target.checked) |
| 129 | + } |
| 130 | + />{" "} |
| 131 | + Same as billing |
| 132 | + </label> |
| 133 | + <legend>Shipping Address</legend> |
| 134 | + <p> |
101 | 135 | <label> |
| 136 | + Shipping Name:{" "} |
102 | 137 | <input |
103 | | - type="checkbox" |
104 | | - defaultChecked={this.state.shippingSameAsBilling} |
105 | | - onChange={event => |
106 | | - this.setState({ |
107 | | - shippingSameAsBilling: event.target.checked |
108 | | - }) |
| 138 | + type="text" |
| 139 | + name="shippingName" |
| 140 | + value={ |
| 141 | + shippingSameAsBilling ? billingName : shippingName |
109 | 142 | } |
110 | | - />{" "} |
111 | | - Same as billing |
| 143 | + onChange={event => setShippingName(event.target.value)} |
| 144 | + readOnly={shippingSameAsBilling} |
| 145 | + /> |
112 | 146 | </label> |
113 | | - <legend>Shipping Address</legend> |
114 | | - <p> |
115 | | - <label> |
116 | | - Shipping Name:{" "} |
117 | | - <input |
118 | | - type="text" |
119 | | - name="shipping_name" |
120 | | - value={ |
121 | | - this.state.shippingSameAsBilling |
122 | | - ? this.state.billingName |
123 | | - : this.state.shippingName |
124 | | - } |
125 | | - onChange={event => |
126 | | - this.setState({ shippingName: event.target.value }) |
127 | | - } |
128 | | - readOnly={this.state.shippingSameAsBilling} |
129 | | - /> |
130 | | - </label> |
131 | | - </p> |
132 | | - <p> |
133 | | - <label> |
134 | | - Shipping State:{" "} |
135 | | - <input |
136 | | - type="text" |
137 | | - size="2" |
138 | | - name="shipping_state" |
139 | | - value={ |
140 | | - this.state.shippingSameAsBilling |
141 | | - ? this.state.billingState |
142 | | - : this.state.shippingState |
143 | | - } |
144 | | - onChange={event => |
145 | | - this.setState({ shippingState: event.target.value }) |
146 | | - } |
147 | | - readOnly={this.state.shippingSameAsBilling} |
148 | | - /> |
149 | | - </label> |
150 | | - </p> |
151 | | - </fieldset> |
152 | | - |
| 147 | + </p> |
153 | 148 | <p> |
154 | | - <button>Submit</button> |
| 149 | + <label> |
| 150 | + Shipping State:{" "} |
| 151 | + <input |
| 152 | + type="text" |
| 153 | + size="2" |
| 154 | + name="shippingState" |
| 155 | + value={ |
| 156 | + shippingSameAsBilling ? billingState : shippingState |
| 157 | + } |
| 158 | + onChange={event => setShippingState(event.target.value)} |
| 159 | + readOnly={shippingSameAsBilling} |
| 160 | + /> |
| 161 | + </label> |
155 | 162 | </p> |
156 | | - </form> |
157 | | - </div> |
158 | | - ); |
159 | | - } |
| 163 | + </fieldset> |
| 164 | + |
| 165 | + <p> |
| 166 | + <button>Submit</button> |
| 167 | + </p> |
| 168 | + </form> |
| 169 | + </div> |
| 170 | + ); |
160 | 171 | } |
161 | 172 |
|
162 | 173 | ReactDOM.render(<CheckoutForm />, document.getElementById("app")); |
0 commit comments