From 154d496208b3670a9421c6542899bd8d4fc6ca57 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Sun, 16 Oct 2022 23:22:19 +0200 Subject: [PATCH] stores.ts added, StrapiNoteRepository using it, updated editor --- .../svelte/src/models/StrapiNoteRepository.ts | 16 ++++-- .../svelte/src/routes/editor/+page.svelte | 55 ++++++++++++++++--- frontend/svelte/src/stores.ts | 7 +++ 3 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 frontend/svelte/src/stores.ts diff --git a/frontend/svelte/src/models/StrapiNoteRepository.ts b/frontend/svelte/src/models/StrapiNoteRepository.ts index ab4a68f..fc06660 100644 --- a/frontend/svelte/src/models/StrapiNoteRepository.ts +++ b/frontend/svelte/src/models/StrapiNoteRepository.ts @@ -1,6 +1,8 @@ import type {Note} from "./types"; import {parseCookies} from "nookies"; import type {NoteRepository} from "./NoteRepository"; +import {currentNoteId} from "../stores"; + type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' @@ -13,13 +15,19 @@ export class StrapiNoteRepository implements NoteRepository { return this.instance; } - private constructor() {} + private constructor() { + currentNoteId.subscribe((value) => (this._currentNoteId = value)); + } - private _currentNoteId: number | undefined; + private _currentNoteId: unknown; private static apiNoteEndpoint: string = "http://localhost:1337/api/notes" public set currentNoteId(value: number | undefined) { - this._currentNoteId = value; + currentNoteId.set(value || -1); + } + + public get currentNoteId(): number { + return this._currentNoteId; } public async getNotes(): Promise{ @@ -36,7 +44,7 @@ export class StrapiNoteRepository implements NoteRepository { if (this._currentNoteId === null || this._currentNoteId === undefined) { return; } - return await this.getNote(this._currentNoteId); + return await this.getNote(this.currentNoteId); } public async updateNote(id: number, note: Partial): Promise { diff --git a/frontend/svelte/src/routes/editor/+page.svelte b/frontend/svelte/src/routes/editor/+page.svelte index b8f0b1a..13fcc92 100644 --- a/frontend/svelte/src/routes/editor/+page.svelte +++ b/frontend/svelte/src/routes/editor/+page.svelte @@ -1,24 +1,65 @@ - {"Pomelonote | Edit " + currNote.title} + Editor +
- {currNote.content} +
+ + +
+ + diff --git a/frontend/svelte/src/stores.ts b/frontend/svelte/src/stores.ts new file mode 100644 index 0000000..b763f5f --- /dev/null +++ b/frontend/svelte/src/stores.ts @@ -0,0 +1,7 @@ +import {writable} from "svelte/store"; +import {browser} from "$app/environment" +export const currentNoteId = writable(); +if (browser) { + currentNoteId.set(Number(localStorage.getItem("currentNoteId") || "")) + currentNoteId.subscribe(val => localStorage.setItem("currentNoteId", String(val))); +} \ No newline at end of file