Skip to content

Commit 76221e6

Browse files
author
NicholasKyleHoffman
committed
added EditUserForm (toggle button functionality for updating/saving a user within the list)
1 parent 7c5dc20 commit 76221e6

File tree

4 files changed

+84
-28
lines changed

4 files changed

+84
-28
lines changed

src/App.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,64 @@
11
import React, { useState } from 'react';
22
import UserTable from './tables/UserTable';
33
import AddUserForm from './forms/AddUserForm';
4+
import EditUserForm from './forms/EditUserForm'
45

5-
// simple functional component returns app skeleton
66
const App = () => {
77
const usersData = [
88
{ 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' }
1112
]
1213

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
1416

17+
const [users, setUsers] = useState(usersData)
1518

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.
2319

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
2423

25-
// Passed through
2624
const addUser = (user) => {
2725
user.id = user.length + 1
2826
setUsers([ ...users, user ])
2927
}
3028

3129
// pass deleteUser through props to UserTable
30+
// use setter to take ID of user & filter them out of the users array
3231
const deleteUser = (id) => {
3332
setUsers(users.filter(user => user.id !== id))
3433
}
3534

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+
3650

3751
return (
3852
<div className="container">
39-
<h1>CRUD App with Hooks</h1>
53+
<h1>CRUD React App with Hooks Instead of Classes</h1>
4054
<div className="flex-row">
4155
<div className="flex-large">
4256
<h2>Add User</h2>
4357
<AddUserForm addUser={addUser} />
4458
</div>
4559
<div className="flex-large">
4660
<h2>View Users</h2>
47-
<UserTable users={users} deleteUser={deleteUser} />
61+
<UserTable users={users} editRow={editRow} deleteUser={deleteUser} />
4862
</div>
4963
</div>
5064
</div>

src/forms/AddUserForm.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
import React, { useState } from 'react'
1+
import React, { useState } from 'react';
22

3-
const AddUserForm = props => {
3+
const AddUserForm = (props) => {
44
const initialFormState = { id: null, name: '', username: '' }
55
const [ user, setUser ] = useState(initialFormState)
66

7-
const handleInputChange = event => {
7+
// update state based on event in input
8+
const handleInputChange = (event) => {
89
const { name, value } = event.target
9-
console.log(event)
1010
setUser({ ...user, [name]: value })
1111
}
1212

1313
return (
1414
<form
15-
onSubmit={event => {
16-
event.preventDefault()
17-
if (!user.name || !user.username) return
18-
19-
props.addUser(user)
20-
setUser(initialFormState)
21-
}}
15+
onSubmit = {
16+
(event) => {
17+
// default event for form is submit (preventing with a check)
18+
event.preventDefault()
19+
if (!user.name || !user.username) {
20+
alert("Enter both name and username")
21+
return;
22+
} else {
23+
props.addUser(user)
24+
setUser(initialFormState)
25+
}
26+
}
27+
}
2228
>
2329
<label>Name</label>
2430
<input type="text" name="name" value={user.name} onChange={handleInputChange} />
2531
<label>Username</label>
2632
<input type="text" name="username" value={user.username} onChange={handleInputChange} />
27-
<button>Add new user</button>
33+
<button>Add New User</button>
2834
</form>
2935
)
3036
}
3137

32-
export default AddUserForm
38+
export default AddUserForm;

src/forms/EditUserForm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const EditUserForm = props => {
2+
const [ user, setUser ] = useState(props.currentUser)
3+
4+
const handleInputChange = event => {
5+
const { name, value } = event.target
6+
7+
setUser({ ...user, [name]: value })
8+
}
9+
10+
return (
11+
<form
12+
onSubmit={event => {
13+
event.preventDefault()
14+
15+
props.updateUser(user.id, user)
16+
}}
17+
>
18+
<label>Name</label>
19+
<input type="text" name="name" value={user.name} onChange={handleInputChange} />
20+
<label>Username</label>
21+
<input type="text" name="username" value={user.username} onChange={handleInputChange} />
22+
<button>Update user</button>
23+
<button onClick={() => props.setEditing(false)} className="button muted-button">
24+
Cancel
25+
</button>
26+
</form>
27+
)
28+
}
29+
30+
export default EditUserForm;

src/tables/UserTable.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ const UserTable = (props) => (
1616
<td>{user.name}</td>
1717
<td>{user.username}</td>
1818
<td>
19-
<button className="button muted-button">Edit</button>
19+
<button
20+
onClick = {() => {
21+
props.editRow(user)
22+
}}
23+
className="button muted-button"
24+
>Edit
25+
</button>
2026
<button onClick={() => props.deleteUser(user.id)} className="button muted-button">Delete</button>
2127
</td>
2228
</tr>

0 commit comments

Comments
 (0)