|
| 1 | +import React, { useState, useReducer } from "react" |
| 2 | +import ReactDOM from "react-dom" |
| 3 | + |
| 4 | +const style = { |
| 5 | + table: { |
| 6 | + borderCollapse: "collapse", |
| 7 | + }, |
| 8 | + tableCell: { |
| 9 | + border: "1px solid gray", |
| 10 | + margin: 0, |
| 11 | + padding: "5px 10px", |
| 12 | + width: "max-content", |
| 13 | + minWidth: "150px", |
| 14 | + }, |
| 15 | + form: { |
| 16 | + container: { |
| 17 | + padding: "20px", |
| 18 | + border: "1px solid #F0F8FF", |
| 19 | + borderRadius: "15px", |
| 20 | + width: "max-content", |
| 21 | + marginBottom: "40px", |
| 22 | + }, |
| 23 | + inputs: { |
| 24 | + marginBottom: "5px", |
| 25 | + }, |
| 26 | + submitBtn: { |
| 27 | + marginTop: "10px", |
| 28 | + padding: "10px 15px", |
| 29 | + border: "none", |
| 30 | + backgroundColor: "lightseagreen", |
| 31 | + fontSize: "14px", |
| 32 | + borderRadius: "5px", |
| 33 | + }, |
| 34 | + }, |
| 35 | +} |
| 36 | + |
| 37 | +function PhoneBookForm({ addEntryToPhoneBook }) { |
| 38 | + const initialFormState = { |
| 39 | + userFirstname: "Coder", |
| 40 | + userLastname: "Byte", |
| 41 | + userPhone: "8885559999", |
| 42 | + } |
| 43 | + |
| 44 | + const formReducer = (state, { type, payload }) => { |
| 45 | + switch (type) { |
| 46 | + case "RESET": |
| 47 | + return { userFirstname: "", userLastname: "", userPhone: "" } |
| 48 | + default: |
| 49 | + return { ...state, [type]: payload } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const [formState, dispatch] = useReducer(formReducer, initialFormState) |
| 54 | + |
| 55 | + return ( |
| 56 | + <form |
| 57 | + onSubmit={e => { |
| 58 | + e.preventDefault() |
| 59 | + addEntryToPhoneBook(formState) |
| 60 | + dispatch({ type: "RESET" }) |
| 61 | + }} |
| 62 | + style={style.form.container} |
| 63 | + > |
| 64 | + <label>First name:</label> |
| 65 | + <br /> |
| 66 | + <input |
| 67 | + style={style.form.inputs} |
| 68 | + className="userFirstname" |
| 69 | + name="userFirstname" |
| 70 | + type="text" |
| 71 | + onChange={({ target: { name, value } }) => |
| 72 | + dispatch({ type: name, payload: value }) |
| 73 | + } |
| 74 | + value={formState.userFirstname} |
| 75 | + /> |
| 76 | + <br /> |
| 77 | + <label>Last name:</label> |
| 78 | + <br /> |
| 79 | + <input |
| 80 | + style={style.form.inputs} |
| 81 | + className="userLastname" |
| 82 | + name="userLastname" |
| 83 | + type="text" |
| 84 | + onChange={({ target: { name, value } }) => |
| 85 | + dispatch({ type: name, payload: value }) |
| 86 | + } |
| 87 | + value={formState.userLastname} |
| 88 | + /> |
| 89 | + <br /> |
| 90 | + <label>Phone:</label> |
| 91 | + <br /> |
| 92 | + <input |
| 93 | + style={style.form.inputs} |
| 94 | + className="userPhone" |
| 95 | + name="userPhone" |
| 96 | + type="text" |
| 97 | + onChange={({ target: { name, value } }) => |
| 98 | + dispatch({ type: name, payload: value }) |
| 99 | + } |
| 100 | + value={formState.userPhone} |
| 101 | + /> |
| 102 | + <br /> |
| 103 | + <input |
| 104 | + style={style.form.submitBtn} |
| 105 | + className="submitButton" |
| 106 | + type="submit" |
| 107 | + value="Add User" |
| 108 | + /> |
| 109 | + </form> |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +function InformationTable({ entries }) { |
| 114 | + return ( |
| 115 | + <table style={style.table} className="informationTable"> |
| 116 | + <thead> |
| 117 | + <tr key={`row-0`}> |
| 118 | + <th style={style.tableCell}>First name</th> |
| 119 | + <th style={style.tableCell}>Last name</th> |
| 120 | + <th style={style.tableCell}>Phone</th> |
| 121 | + </tr> |
| 122 | + {entries.map( |
| 123 | + ({ userFirstname, userLastname, userPhone }, index) => ( |
| 124 | + <tr key={`row-${index + 1}`}> |
| 125 | + <td style={style.tableCell}>{userFirstname}</td> |
| 126 | + <td style={style.tableCell}>{userLastname}</td> |
| 127 | + <td style={style.tableCell}>{userPhone}</td> |
| 128 | + </tr> |
| 129 | + ) |
| 130 | + )} |
| 131 | + </thead> |
| 132 | + </table> |
| 133 | + ) |
| 134 | +} |
| 135 | + |
| 136 | +function Application() { |
| 137 | + const [entries, setEntries] = useState([]) |
| 138 | + |
| 139 | + const addEntryToPhoneBook = ({ |
| 140 | + userFirstname, |
| 141 | + userLastname, |
| 142 | + userPhone, |
| 143 | + }) => { |
| 144 | + const newEntries = [ |
| 145 | + ...entries, |
| 146 | + { userFirstname, userLastname, userPhone }, |
| 147 | + ] |
| 148 | + const newSortedEntries = newEntries.sort((a, b) => { |
| 149 | + const userLastnameA = a.userLastname.toLowerCase() |
| 150 | + const userLastnameB = b.userLastname.toLowerCase() |
| 151 | + return userLastnameA < userLastnameB |
| 152 | + ? -1 |
| 153 | + : userLastnameA > userLastnameB |
| 154 | + ? 1 |
| 155 | + : 0 |
| 156 | + }) |
| 157 | + setEntries(newSortedEntries) |
| 158 | + } |
| 159 | + |
| 160 | + return ( |
| 161 | + <section> |
| 162 | + <PhoneBookForm addEntryToPhoneBook={addEntryToPhoneBook} /> |
| 163 | + <InformationTable entries={entries} /> |
| 164 | + </section> |
| 165 | + ) |
| 166 | +} |
| 167 | + |
| 168 | +ReactDOM.render(<Application />, document.getElementById("root")) |
0 commit comments