diff --git a/src/App.jsx b/src/App.jsx index 54e7b55..9573a2b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -2,13 +2,15 @@ import "./App.css"; import { Routes, Route } from "react-router-dom"; import Menu from "./components/menu"; import CreateNote from "./pages/create"; +import Save from "./pages/save-local"; +import Note from "./pages/note"; function App() { Storage.prototype.setObj = function (key, obj) { return this.setItem(key, JSON.stringify(obj)); }; Storage.prototype.getObj = function (key) { - return JSON.parse(this.getItem(key)); + return JSON.parse(this.getItem(key)) || {}; }; return (
@@ -16,13 +18,15 @@ function App() {
} /> + } /> + } /> О сервисе
} /> Заметки со всего мира
} + element={
Заметки
} /> diff --git a/src/components/notePlaceholder.jsx b/src/components/notePlaceholder.jsx new file mode 100644 index 0000000..4dae934 --- /dev/null +++ b/src/components/notePlaceholder.jsx @@ -0,0 +1,14 @@ +function NotePlaceholder() { + return ( +
+
+ Название... +
+
+ Описание... +
+
+ ); +} + +export default NotePlaceholder; diff --git a/src/pages/create.jsx b/src/pages/create.jsx index 257f813..625927d 100644 --- a/src/pages/create.jsx +++ b/src/pages/create.jsx @@ -1,4 +1,4 @@ -import { ButtonWithAction, IconWithButton } from "../components/button"; +import { Button, IconWithButton } from "../components/button"; import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline"; import { CheckBox } from "../components/checkbox"; import { useState } from "react"; @@ -29,7 +29,7 @@ function CreateNote() { type="text" className={`mb-2 md:w-1/6 w-full ${inputStyle}`} placeholder="Название заметки..." - value={localStorage.getItem("NoteName")} + value={localStorage.getItem("NoteName") || ""} onChange={(e) => { localStorage.setItem("NoteName", e.target.value); setName(e.target.value); @@ -48,9 +48,8 @@ function CreateNote() { localStorage.setItem("NoteText", e.target.value); setText(e.target.value); }} - > - {localStorage.getItem("NoteText")} - + value={localStorage.getItem("NoteText") || ""} + > {preview && (
@@ -70,7 +69,10 @@ function CreateNote() { checked={localStorage.getItem("private")} />
- +
diff --git a/src/pages/note.jsx b/src/pages/note.jsx new file mode 100644 index 0000000..34284a4 --- /dev/null +++ b/src/pages/note.jsx @@ -0,0 +1,52 @@ +import ReactMarkdown from "react-markdown"; +import NotePlaceholder from "../components/notePlaceholder"; +import { useState, useEffect } from "react"; +import { useParams } from "react-router-dom"; + +function printDate(time) { + time = new Date(time); + function padStr(i) { + return i < 10 ? "0" + i : "" + i; + } + + let dateStr = `${padStr(time.getHours())}:${padStr( + time.getMinutes() + )}:${padStr(time.getSeconds())} ${padStr(time.getDate())}.${padStr( + 1 + time.getMonth() + )}.${padStr(time.getFullYear())}`; + + return dateStr; +} + +function Note() { + let params = useParams(); + const [note, setNote] = useState(false); + + useEffect(() => { + setNote(localStorage.getObj("Notes")[params.id]); + }, []); + + if (note) { + console.log(note); + + return ( +
+
+

+ {note.name} +

+
+ {printDate(note.time)} +
+
+
+ {note.text} +
+
+ ); + } + + return ; +} + +export default Note; diff --git a/src/pages/save-local.jsx b/src/pages/save-local.jsx new file mode 100644 index 0000000..c3b575e --- /dev/null +++ b/src/pages/save-local.jsx @@ -0,0 +1,43 @@ +import { useState, useEffect } from "react"; +import { useNavigate } from "react-router-dom"; +import NotePlaceholder from "../components/notePlaceholder"; + +function uuidv4() { + return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => + ( + c ^ + (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))) + ).toString(16) + ); +} + +function Save() { + const navigate = useNavigate(); + let done = false; + + useEffect(() => { + if (!done) { + done = true; + let id = uuidv4(); + + let notesObj = localStorage.getObj("Notes"); + + notesObj[`${id}`] = { + name: localStorage.getItem("NoteName"), + text: localStorage.getItem("NoteText"), + time: Date.now(), + }; + + localStorage.setObj("Notes", notesObj); + + localStorage.removeItem("NoteName"); + localStorage.removeItem("NoteText"); + + navigate(`/notes/${id}`); + } + }, []); + + return ; +} + +export default Save;