|
1 | 1 | import React, { useState } from 'react'; |
2 | 2 | import UserTable from './tables/UserTable'; |
3 | 3 | import AddUserForm from './forms/AddUserForm'; |
| 4 | +import EditUserForm from './forms/EditUserForm' |
4 | 5 |
|
5 | | -// simple functional component returns app skeleton |
6 | 6 | const App = () => { |
7 | 7 | const usersData = [ |
8 | 8 | { id:1, name:'Nick', username:'ebb_n_flow' }, |
9 | | - { id:2, name:'Shane O.', username:'tootired' }, |
10 | | - { id:3, name:'Benjamin', username:'rootsrevival' }, |
| 9 | + { id:2, name:'Shane O.', username:'too-tired' }, |
| 10 | + { id:3, name:'Shawna', username:'rootsrevival' }, |
| 11 | + { id:4, name:'Sheena, W.P.', username:'sheenaWp312' } |
11 | 12 | ] |
12 | 13 |
|
13 | | - const [users, setUsers] = useState(usersData) |
| 14 | +// Hooks syntax - you define the two params in [x, y]. |
| 15 | +// useState comes from React import for handling state |
14 | 16 |
|
| 17 | + const [users, setUsers] = useState(usersData) |
15 | 18 |
|
16 | | -// https://www.taniarascia.com/crud-app-in-react-with-hooks/ |
17 | | - // Since we’re not using a real API and database, which would |
18 | | - // probably have an auto-incrementing ID, I’m going to |
19 | | - // increment the ID of the new user manually. This function will |
20 | | - // take a user object as a parameter, and add them to the users array |
21 | | - // of objects. The ...users code ensures that all the previous users |
22 | | - // remain in the array. |
23 | 19 |
|
| 20 | +// increment the ID of the new user manually - function will |
| 21 | +// take a user object as a parameter, add them to the users array of objects |
| 22 | +// the ...users code ensures that all the previous users remain in the array |
24 | 23 |
|
25 | | -// Passed through |
26 | 24 | const addUser = (user) => { |
27 | 25 | user.id = user.length + 1 |
28 | 26 | setUsers([ ...users, user ]) |
29 | 27 | } |
30 | 28 |
|
31 | 29 | // pass deleteUser through props to UserTable |
| 30 | + // use setter to take ID of user & filter them out of the users array |
32 | 31 | const deleteUser = (id) => { |
33 | 32 | setUsers(users.filter(user => user.id !== id)) |
34 | 33 | } |
35 | 34 |
|
| 35 | + const [editing, setEditing] = useState(false) |
| 36 | + const initialFormState = {id:null, name:'', username:'' } |
| 37 | + const [ currentUser, setCurrentUser ] = useState(initialFormState) |
| 38 | + |
| 39 | + const editRow = (user) => { |
| 40 | + setEditing(true) |
| 41 | + setCurrentUser({ id:user.id, name:user.name, username:user.username}) |
| 42 | + } |
| 43 | + |
| 44 | + const updateUser = (id, updatedUser) => { |
| 45 | + setEditing(false) |
| 46 | + |
| 47 | + setUsers(users.map(user=> (user.id === id ? updatedUser : user))) |
| 48 | + } |
| 49 | + |
36 | 50 |
|
37 | 51 | return ( |
38 | 52 | <div className="container"> |
39 | | - <h1>CRUD App with Hooks</h1> |
| 53 | + <h1>CRUD React App with Hooks Instead of Classes</h1> |
40 | 54 | <div className="flex-row"> |
41 | 55 | <div className="flex-large"> |
42 | 56 | <h2>Add User</h2> |
43 | 57 | <AddUserForm addUser={addUser} /> |
44 | 58 | </div> |
45 | 59 | <div className="flex-large"> |
46 | 60 | <h2>View Users</h2> |
47 | | - <UserTable users={users} deleteUser={deleteUser} /> |
| 61 | + <UserTable users={users} editRow={editRow} deleteUser={deleteUser} /> |
48 | 62 | </div> |
49 | 63 | </div> |
50 | 64 | </div> |
|
0 commit comments