-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathListNotes.tsx
82 lines (74 loc) · 2.14 KB
/
ListNotes.tsx
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useState, useEffect } from "react";
import { Link, RouteComponentProps } from "@reach/router";
import { GATEWAY_URL } from "../config.json";
import { Card, Alert, CardGroup, Button } from "react-bootstrap";
import { Loading, PageContainer } from "../components";
interface Note {
noteId: string;
createdAt: string;
content: string;
attachment: boolean;
}
const ListNotes = (props: RouteComponentProps) => {
const [isLoading, setIsLoading] = useState(true);
const [errorMsg, setErrorMsg] = useState("");
const [notes, setNotes] = useState([]);
useEffect(() => {
const fetchNotes = async () => {
setIsLoading(true);
const fetchURL = `${GATEWAY_URL}notes`;
try {
const response = await fetch(fetchURL);
const data = await response.json();
setNotes(data);
} catch (error) {
setErrorMsg(`${error.toString()} - ${fetchURL}`);
} finally {
setIsLoading(false);
}
};
fetchNotes();
}, []);
const renderNotes = (notes: Note[]) =>
notes.map((note) => (
<Card>
<Card.Body>
<Card.Title>
{note.attachment && (
<span role="img" aria-label="attachment" className="mr-1">
📎
</span>
)}
{note.content}
</Card.Title>
<Card.Subtitle className="text-muted">
Created: {new Date(parseInt(note.createdAt)).toLocaleString()}
</Card.Subtitle>
<Card.Link key={note.noteId} href={`/notes/${note.noteId}`}>
edit
</Card.Link>
</Card.Body>
</Card>
));
const createNewNote = () => (
<Link key="new" to="note/new">
<div className="d-grid">
<Button variant="primary">Create New Note</Button>
</div>
</Link>
);
return (
<PageContainer header={<div>Your Notes</div>}>
{errorMsg && <Alert variant="danger">{errorMsg}</Alert>}
{isLoading ? (
<Loading />
) : (
<div>
<CardGroup>{renderNotes(notes)}</CardGroup>
{createNewNote()}
</div>
)}
</PageContainer>
);
};
export default ListNotes;