forked from aws-samples/aws-sdk-js-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathShowNote.tsx
More file actions
74 lines (69 loc) · 2.34 KB
/
ShowNote.tsx
File metadata and controls
74 lines (69 loc) · 2.34 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
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
import React, { useState, useEffect } from "react";
import { RouteComponentProps, navigate } from "@reach/router";
import { Form, Card } from "react-bootstrap";
import { GATEWAY_URL } from "../config.json";
import { DeleteNoteButton, SaveNoteButton } from "./";
import { HomeButton, Loading, PageContainer } from "../components";
const ShowNote = (props: RouteComponentProps<{ noteId: string }>) => {
const { noteId } = props;
const [isLoading, setIsLoading] = useState(true);
const [noteContent, setNoteContent] = useState("");
const [attachment, setAttachment] = useState("");
const [attachmentURL, setAttachmentURL] = useState("");
useEffect(() => {
const fetchNote = async (noteId: string) => {
setIsLoading(true);
const fetchURL = `${GATEWAY_URL}notes/${noteId}`;
try {
const response = await fetch(fetchURL);
const data = await response.json();
setNoteContent(data.content);
} catch (error) {
// Navigate to 404 page, as noteId probably not present
navigate("/404");
} finally {
setIsLoading(false);
}
};
fetchNote(noteId || "");
}, [noteId]);
return (
<PageContainer header={<HomeButton />}>
{isLoading ? (
<Loading />
) : (
<form>
<Form.Group controlId="content">
<Form.Label>Note Content</Form.Label>
<Form.Control
as="textarea"
defaultValue={noteContent}
onChange={(e) => {
const content = e.currentTarget.value;
if (content) {
setNoteContent(content);
}
}}
/>
</Form.Group>
{attachmentURL && (
<Form.Group>
<Form.Label>Attachment</Form.Label>
<Form.Text>
<Card.Link href={attachmentURL}>
<span role="img" aria-label="attachment" className="mr-1">
📎
</span>
{attachment.replace(/^\w+-/, "")}
</Card.Link>
</Form.Text>
</Form.Group>
)}
<SaveNoteButton noteId={noteId || ""} noteContent={noteContent} />
<DeleteNoteButton noteId={noteId || ""} attachment={attachment} />
</form>
)}
</PageContainer>
);
};
export default ShowNote;