feat: publish local notes

This commit is contained in:
Artemy 2023-04-29 10:16:04 +03:00
parent f8365ce2ee
commit 3963e435e8
2 changed files with 54 additions and 38 deletions

View file

@ -18,6 +18,7 @@
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { import {
ChevronDoubleLeftIcon, ChevronDoubleLeftIcon,
ChevronDoubleRightIcon,
PencilIcon, PencilIcon,
TrashIcon, TrashIcon,
} from "@heroicons/react/24/outline"; } from "@heroicons/react/24/outline";
@ -93,6 +94,12 @@ function NotePage() {
setEdit(!edit); setEdit(!edit);
}} }}
/> />
<ButtonWithIcon
className="mt-4"
text={locals.Publish}
icon={ChevronDoubleRightIcon}
href={`/notes/publish?local_id=${params.id}`}
/>
{!edit && ( {!edit && (
<ButtonWithIcon <ButtonWithIcon
className="mt-4" className="mt-4"

View file

@ -17,54 +17,63 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { useSearchParams } from "react-router-dom";
function Publish() { function Publish() {
const navigate = useNavigate(); const navigate = useNavigate();
let done = false; const [searchParams] = useSearchParams();
useEffect(() => { useEffect(() => {
if (!done) { let err = false;
done = true;
let err = false; let note;
const 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"), name: localStorage.getItem("NoteName"),
text: localStorage.getItem("NoteText"), 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 <div />; return <div />;
} }