Skip to content

Commit 49e3048

Browse files
committed
imp: Chapter18
1 parent 3057eae commit 49e3048

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

Chapter18/todoapp/frontend/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Chapter18/todoapp/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@testing-library/user-event": "^13.5.0",
99
"axios": "^0.25.0",
1010
"bootstrap": "^5.1.3",
11+
"moment": "^2.29.1",
1112
"react": "^17.0.2",
1213
"react-bootstrap": "^2.1.1",
1314
"react-dom": "^17.0.2",

Chapter18/todoapp/frontend/src/components/todos-list.js

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,68 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
2+
import TodoDataService from '../services/todos';
3+
import { Link } from 'react-router-dom';
4+
import Card from 'react-bootstrap/Card';
5+
import Container from 'react-bootstrap/Container';
6+
import Button from 'react-bootstrap/Button';
7+
import Alert from 'react-bootstrap/Alert';
8+
import moment from 'moment';
9+
10+
const TodosList = props => {
11+
const [todos, setTodos] = useState([]);
12+
13+
useEffect(() => {
14+
retrieveTodos();
15+
}, [props.token]);
16+
17+
const retrieveTodos = () => {
18+
TodoDataService.getAll(props.token)
19+
.then(response => {
20+
setTodos(response.data);
21+
})
22+
.catch(e => {
23+
console.log(e);
24+
});
25+
}
226

3-
function TodosList() {
427
return (
5-
<div className="App">
6-
Todos List
7-
</div>
28+
<Container>
29+
{props.token == null || props.token === "" ? (
30+
<Alert variant='warning'>
31+
You are not logged in. Please <Link to={"/login"}>login</Link> to see your todos.
32+
</Alert>
33+
) : (
34+
<div>
35+
{todos.map((todo) => {
36+
return (
37+
<Card key={todo.id} className="mb-3">
38+
<Card.Body>
39+
<div>
40+
<Card.Title>{todo.title}</Card.Title>
41+
<Card.Text><b>Memo:</b> {todo.memo}</Card.Text>
42+
<Card.Text>
43+
Date created: {moment(todo.created).format("Do MMMM YYYY")}
44+
</Card.Text>
45+
</div>
46+
<Link to={{
47+
pathname: "/todos/" + todo.id,
48+
state: {
49+
currentTodo: todo
50+
}
51+
}}>
52+
<Button variant="outline-info" className="me-2">
53+
Edit
54+
</Button>
55+
</Link>
56+
<Button variant="outline-danger">
57+
Delete
58+
</Button>
59+
</Card.Body>
60+
</Card>
61+
)
62+
})}
63+
</div>
64+
)}
65+
</Container>
866
);
967
}
1068

0 commit comments

Comments
 (0)