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 { getObjectUrl } from "../libs";
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.S as string);
if (data.attachment) {
setAttachment(data.attachment.S);
setAttachmentURL(await getObjectUrl(data.attachment.S));
}
} catch (error) {
// Navigate to 404 page, as noteId probably not present
navigate("/404");
} finally {
setIsLoading(false);
}
};
fetchNote(noteId || "");
}, [noteId]);
return (