Skip to content

Commit ae5a38c

Browse files
offline working mode finished starting app version two
1 parent 5c00232 commit ae5a38c

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed

components/Note.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import { v4 as uuid } from "uuid";
1010
type Props = {
1111
note?: Note;
1212
id?: string;
13+
redirect: string;
1314
};
1415

15-
function Note({ note, id }: Props) {
16+
function Note({ note, id, redirect }: Props) {
1617
const [showMenu, setshowMenu] = useState(false);
18+
console.log(redirect);
1719

1820
const [Id, setId] = useState(() => {
1921
if (typeof window !== "undefined") {
@@ -26,7 +28,12 @@ function Note({ note, id }: Props) {
2628

2729
const [StoredNote, setNote] = useState(() => {
2830
if (typeof window !== "undefined") {
29-
const Notes = JSON.parse(localStorage.getItem("NOTES")!) as Note[];
31+
var Notes = [] as Note[];
32+
if (redirect == "/recycle") {
33+
Notes = JSON.parse(localStorage.getItem("DELETED_NOTES")!) as Note[];
34+
} else {
35+
Notes = JSON.parse(localStorage.getItem("NOTES")!) as Note[];
36+
}
3037
if (id) {
3138
return Notes.find((note) => note.id == id);
3239
} else {
@@ -37,7 +44,12 @@ function Note({ note, id }: Props) {
3744

3845
const [title, settitle] = useState(() => {
3946
if (typeof window !== "undefined") {
40-
const Notes = JSON.parse(localStorage.getItem("NOTES")!) as Note[];
47+
var Notes = [] as Note[];
48+
if (redirect == "/recycle") {
49+
Notes = JSON.parse(localStorage.getItem("DELETED_NOTES")!) as Note[];
50+
} else {
51+
Notes = JSON.parse(localStorage.getItem("NOTES")!) as Note[];
52+
}
4153
if (Notes) {
4254
if (id) {
4355
return Notes.find((note) => note.id == id)?.title;
@@ -126,8 +138,8 @@ function Note({ note, id }: Props) {
126138
<div
127139
style={{ cursor: "pointer" }}
128140
onClick={() => {
129-
localStorage.removeItem("CURRENTID")
130-
Router.push("/home");
141+
localStorage.removeItem("CURRENTID");
142+
Router.push(`${redirect}`);
131143
}}
132144
>
133145
<Image
@@ -164,7 +176,7 @@ function Note({ note, id }: Props) {
164176
setshowMenu(false);
165177
}}
166178
>
167-
<TipTap id={id} Id={Id} setId={setId} markdown={StoredNote?.text}/>
179+
<TipTap id={id} Id={Id} setId={setId} markdown={StoredNote?.text} />
168180
</div>
169181
</div>
170182
);

components/SideBar.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useEffect, useState } from "react";
22
import styles from "./Components.module.css";
33
import SideBarLink from "./SideBarLink";
44
import Image from "next/image";
@@ -8,6 +8,18 @@ type Props = {
88
};
99

1010
function SideBar({ showSidebar }: Props) {
11+
12+
const [storedNotesCount, setstoredNotesCount] = useState("")
13+
const [deletedNotesCount, setdeletedNotesCount] = useState("")
14+
15+
useEffect(() => {
16+
const Notes = JSON.parse(localStorage.getItem("NOTES")!)
17+
const deletedNotes = JSON.parse(localStorage.getItem("DELETED_NOTES")!)
18+
19+
setstoredNotesCount(Notes?.length)
20+
setdeletedNotesCount(deletedNotes?.length)
21+
},[])
22+
1123
return (
1224
<div
1325
className={
@@ -20,8 +32,8 @@ function SideBar({ showSidebar }: Props) {
2032
<div className={styles.sidebar_profile}>
2133
<Image src="/Images/Icons/person.png" width={30} height={30} alt="person"/>
2234
</div>
23-
<SideBarLink path="/home" showSidebar={showSidebar} value="All notes" icon="/Images/Icons/notes.png" count="11" />
24-
<SideBarLink path="/recycle" showSidebar={showSidebar} value="Recycle bin" icon="/Images/Icons/recycle.png" count="0" />
35+
<SideBarLink path="/home" showSidebar={showSidebar} value="All notes" icon="/Images/Icons/notes.png" count={storedNotesCount} />
36+
<SideBarLink path="/recycle" showSidebar={showSidebar} value="Recycle bin" icon="/Images/Icons/recycle.png" count={deletedNotesCount} />
2537
</div>
2638
</div>
2739
);

components/SideBarLink.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function SideBarLink({ icon, value, count, showSidebar, path }: Props) {
2020
? styles.sidebar_link_container_opened
2121
: styles.sidebar_link_container_closed
2222
}
23+
style={{cursor:"pointer"}}
2324
onClick={() => {
2425
Router.push(path);
2526
}}

pages/home/Notes.tsx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ function NotesContainer({ Notes, title, setNotes }: Props) {
2323
const [selectedNotes, setselectedNotes] = useState<Note[]>([]);
2424

2525
const ShowNote = (id: string) => {
26-
Router.push(`/notes/${id}`);
26+
Router.push({
27+
pathname: `/notes/${id}`,
28+
query: {
29+
redirect: Router.asPath
30+
}
31+
});
2732
};
2833

2934
function show() {
@@ -50,7 +55,11 @@ function NotesContainer({ Notes, title, setNotes }: Props) {
5055
({ id: id1 }) => !selectedNotes.some(({ id: id2 }) => id1 == id2)
5156
);
5257
setNotes(updatedNotes);
53-
localStorage.setItem("NOTES", JSON.stringify(updatedNotes));
58+
if (title == "Recycle bin") {
59+
localStorage.setItem("DELETED_NOTES", JSON.stringify(updatedNotes))
60+
} else {
61+
localStorage.setItem("NOTES", JSON.stringify(updatedNotes));
62+
}
5463
}
5564

5665
function deleteNotes() {
@@ -72,6 +81,23 @@ function NotesContainer({ Notes, title, setNotes }: Props) {
7281
}
7382
}
7483

84+
function restoreNotes() {
85+
setonDelete(false);
86+
const StoredNotes = JSON.parse(localStorage.getItem("NOTES")!) as Note[];
87+
if (selectedNotes.length == 0) {
88+
return;
89+
}
90+
if (StoredNotes && StoredNotes.length > 0) {
91+
removeFromNotes();
92+
var StoredNotesCopy = [...StoredNotes, ...selectedNotes];
93+
localStorage.setItem("NOTES", JSON.stringify(StoredNotesCopy));
94+
} else {
95+
removeFromNotes();
96+
var restoredNotes = [...selectedNotes];
97+
localStorage.setItem("NOTES", JSON.stringify(restoredNotes));
98+
}
99+
}
100+
75101
return (
76102
<div
77103
className={
@@ -279,9 +305,9 @@ function NotesContainer({ Notes, title, setNotes }: Props) {
279305
{title == "Recycle bin" && (
280306
<button
281307
className={styles.delete_btn}
282-
style={{backgroundColor: "greenyellow"}}
308+
style={{ backgroundColor: "greenyellow" }}
283309
onClick={() => {
284-
deleteNotes();
310+
restoreNotes();
285311
}}
286312
>
287313
<MdOutlineRestoreFromTrash size={30} color="black" />

pages/newNote/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Note from "../../components/Note";
33

44
function index() {
55
return (
6-
<Note />
6+
<Note redirect="/home" />
77
);
88
}
99

pages/notes/[id].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useRouter } from "next/router";
55
function index() {
66
const router = useRouter()
77

8-
return <Note id={router.query.id as string}/>;
8+
return <Note id={router.query.id as string} redirect={router.query.redirect as string}/>;
99
}
1010

1111
export default index;

0 commit comments

Comments
 (0)