Notes are now displayed in order of the "lastOpened" property

The Note interface was moved to types.ts
This commit is contained in:
dhain 2022-09-25 11:16:36 +02:00
parent 2705fe27ef
commit 395a9dc2e8
3 changed files with 39 additions and 17 deletions

View file

@ -1,15 +1,12 @@
<script lang="ts">
interface Note {
id: number;
title: string;
content: string;
}
import type {Note} from "../types";
//TODO: TEMP!!!
const tempJson = "[{\"id\":0,\"title\":\"samc\",\"content\":\"SAAAAAAAAAAMC\"},{\"id\":1,\"title\":\"Push\",\"content\":\"Kollege Pusch\"},{\"id\":2,\"title\":\"Mike\",\"content\":\"C Meister\"},{\"id\":3,\"title\":\"kekw\",\"content\":\"OMEGALUL\"}]";
const tempJson = "[{\"id\":0,\"title\":\"samc\",\"content\":\"SAAAAAAAAAAMC\",\"lastOpened\":\"2022-09-25T10:45:49.903Z\"},{\"id\":1,\"title\":\"Push\",\"content\":\"Kollege Pusch\",\"lastOpened\":\"2022-09-25T10:50:49.903Z\"},{\"id\":2,\"title\":\"Mike\",\"content\":\"C Meister\",\"lastOpened\":\"2022-09-25T10:09:13.903Z\"},{\"id\":3,\"title\":\"kekw\",\"content\":\"OMEGALUL\",\"lastOpened\":\"2022-09-25T12:03:49.903Z\"}]";
//TODO: TEMP!!!
let notes: Note[] = JSON.parse(tempJson);
sortNotesByDate();
window.localStorage.setItem("notes", JSON.stringify(notes));
/**
@ -20,31 +17,51 @@
notes = notes.filter(i => i === i);
}
function sortNotesByDate(){
notes.sort(function(x, y) {
if (x.lastOpened > y.lastOpened) {
return 1;
}
if (x.lastOpened < y.lastOpened) {
return -1;
}
return 0;
});
}
/**
* Gives the user a prompt to input the new title of the note and creates it if the title is valid
*/
function addNotePrompt() {
let newTitle = prompt('Name of the new Note');
console.log(notes)
if (newTitle != null && newTitle != '') {
addNote(newTitle);
console.log(notes)
sortNotesByDate();
reloadNotesListing();
window.localStorage.setItem("notes", JSON.stringify(notes));
}
}
/**
* Adds a new note to the "notes" Array with:
* * the latest id + 1
* * the latest id + 1 (or 0 if no notes exist)
* * no content
* @param title The title of the new Note
*/
function addNote(title: string) {
let date = new Date();
let newNoteId = (notes.length == 0) ? 0 : notes[notes.length - 1].id + 1
let note: Note = {
id: notes[notes.length - 1].id + 1,
id: newNoteId,
title: title,
content: ""
content: "",
lastOpened: date.toISOString()
};
notes.push(note);
reloadNotesListing()
}
/**
@ -64,6 +81,7 @@
integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
</head>
<html lang="en">
<!-- Add Note Button -->
<!-- &#10133; is a Thiccc Plus UTF-8 Character -->
<div class="offset-8 col-1">
@ -86,6 +104,7 @@
{/each}
</ul>
</div>
</html>
<style>
html,

View file

@ -1,17 +1,12 @@
<script lang="ts">
interface Note {
id: number;
title: string;
content: string;
}
import type {Note} from "../../types";
let notes: Note[] = JSON.parse(window.localStorage.getItem("notes"));
const clickedNoteId = window.localStorage.getItem("clickedNoteId");
</script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{"Pomelonote | Edit " + notes[clickedNoteId].title}</title>
@ -19,6 +14,8 @@
integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
</head>
<html lang="en">
<div class="offset-3 col-6">
samc samg ccocooc {notes[clickedNoteId].content}
</div>
</html>

View file

@ -0,0 +1,6 @@
export interface Note {
id: number;
title: string;
content: string;
lastOpened: string;
}