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

View file

@ -17,54 +17,63 @@
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { useSearchParams } from "react-router-dom";
function Publish() {
const navigate = useNavigate();
let done = false;
const [searchParams] = useSearchParams();
useEffect(() => {
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 <div />;
}