From 3963e435e8faca9b93b2e481ac799d78ed863f8c Mon Sep 17 00:00:00 2001 From: Artemy Date: Sat, 29 Apr 2023 10:16:04 +0300 Subject: [PATCH] feat: publish local notes --- src/pages/note.jsx | 7 ++++ src/pages/publish.jsx | 85 ++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/pages/note.jsx b/src/pages/note.jsx index 50fab77..d176d9d 100644 --- a/src/pages/note.jsx +++ b/src/pages/note.jsx @@ -18,6 +18,7 @@ import { useParams } from "react-router-dom"; import { ChevronDoubleLeftIcon, + ChevronDoubleRightIcon, PencilIcon, TrashIcon, } from "@heroicons/react/24/outline"; @@ -93,6 +94,12 @@ function NotePage() { setEdit(!edit); }} /> + {!edit && ( { - if (!done) { - done = true; + let err = false; - let err = false; - const note = { + let note; + + if (searchParams.get("local_id")) { + let localNote = + localStorage.getObj("Notes")[searchParams.get("local_id")]; + note = { + name: localNote.name, + text: localNote.text, + }; + } else { + note = { name: localStorage.getItem("NoteName"), text: localStorage.getItem("NoteText"), }; - - if (!note.name) { - err = locals.PubErrorMsgNoName; - } - if (!note.text) { - err = locals.PubErrorMsgNoText; - } - - fetch(`/publish`, { - method: "POST", - headers: { - "Content-Type": "application/json;charset=utf-8", - }, - body: JSON.stringify(note), - }) - .then((data) => { - data - .json() - .then((data) => { - localStorage.removeItem("NoteName"); - localStorage.removeItem("NoteText"); - navigate(`/pubNotesSafe/${data.id}`, { replace: true }); - }) - .catch(() => { - if (err == false) { - navigate(`/pubError`, { replace: true }); - } else navigate(`/pubError?err=${err}`, { replace: true }); - }); - }) - .catch(() => { - navigate(`/pubError`, { replace: true }); - }); } - }, []); + + if (!note.name) { + err = locals.PubErrorMsgNoName; + } + if (!note.text) { + err = locals.PubErrorMsgNoText; + } + + fetch(`/publish`, { + method: "POST", + headers: { + "Content-Type": "application/json;charset=utf-8", + }, + body: JSON.stringify(note), + }) + .then((data) => { + data + .json() + .then((data) => { + localStorage.removeItem("NoteName"); + localStorage.removeItem("NoteText"); + navigate(`/pubNotesSafe/${data.id}`, { replace: true }); + }) + .catch(() => { + if (err == false) { + navigate(`/pubError`, { replace: true }); + } else navigate(`/pubError?err=${err}`, { replace: true }); + }); + }) + .catch(() => { + navigate(`/pubError`, { replace: true }); + }); + }); return
; }