-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLists.js
More file actions
48 lines (39 loc) · 1.66 KB
/
Lists.js
File metadata and controls
48 lines (39 loc) · 1.66 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
import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { saveAsync, selectLists, selectActiveList, activateList } from "./listSlice";
function DisplayList(props) {
const dispatch = useDispatch();
const activeList = useSelector(selectActiveList);
const [active, setActive] = useState('');
const list = props.list;
useEffect(() => {
setActive(list.id === activeList.id ? 'active' : '');
}, [activeList, list.id]);
return (
<button type="button" className={`list-group-item text-start list-group-item-action ${ active }`} onClick={() => dispatch(activateList(list))}>{list.name}</button>
)
}
export function Lists() {
const dispatch = useDispatch();
const lists = useSelector(selectLists);
const [newList, setNewList] = useState({ name: '' });
function onSubmitNewList(event) {
// prevent form from submitting
event.preventDefault();
// save data
dispatch(saveAsync(newList));
// reset item
setNewList({ name: '' });
}
return (
<section className="d-grid gap-4">
<form onSubmit={onSubmitNewList} className="input-group">
<input type="text" className="form-control" onChange={(event) => setNewList({ name: event.target.value })} value={newList.name} placeholder="create new list" />
<button type="submit" className="btn"><i className="bi bi-save" title="save"></i></button>
</form>
<ul className="list-group">
{ lists.map(list => <DisplayList key={list.id} list={list} />) }
</ul>
</section>
)
}