From 5d938656a4e7e0d113ffc439a7d42a0a0a8eea68 Mon Sep 17 00:00:00 2001 From: Elliott <85990359+Ell1ott@users.noreply.github.com> Date: Wed, 28 Feb 2024 22:07:23 +0100 Subject: [PATCH] refactor rename document to be on class --- src/lib/api/apiStore.ts | 10 ++++++++++ src/lib/api/index.ts | 4 ---- src/routes/workspace/editor/Tiptap.svelte | 8 ++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/lib/api/apiStore.ts b/src/lib/api/apiStore.ts index 6b60aec7..e5b5cd24 100644 --- a/src/lib/api/apiStore.ts +++ b/src/lib/api/apiStore.ts @@ -10,6 +10,12 @@ export class FileInfo { created_at: string; updated_at: string; + apiPath: string = '/documents/'; + + rename(newName: string, api: ApiHandler) { + return api.callApi(this.apiPath + this.id, { name: newName }, 'PUT'); + } + constructor(info) { this.id = info.id; this.name = info.name; @@ -20,6 +26,8 @@ export class FileInfo { } export class DocumentInfo extends FileInfo { + apiPath: string = '/documents/'; + constructor(info) { super(info); } @@ -36,6 +44,8 @@ export class Assignment extends FileInfo { due_date: string; progress: AssignmentProgress; + apiPath = '/assignments/'; + constructor(info) { super(info); this.due_date = info.due_date; diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index 82181ec9..d258a0a4 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -89,10 +89,6 @@ export default class ApiHandler { ); }; - renameDocument = this.debounce((documentId: string, documentName: string) => { - this.callApi('/documents/' + documentId, { name: documentName }, 'PUT'); - }, 350); - deleteDocument = (documentId: string) => { return this.callApi('/documents/' + documentId, {}, 'DELETE'); }; diff --git a/src/routes/workspace/editor/Tiptap.svelte b/src/routes/workspace/editor/Tiptap.svelte index 4f31293b..5ff995cc 100644 --- a/src/routes/workspace/editor/Tiptap.svelte +++ b/src/routes/workspace/editor/Tiptap.svelte @@ -103,13 +103,13 @@ const title = transaction.doc.content.content[0].content.content[0]?.text || 'Uden titel'; if (title && title !== currentFileName) { - api.renameDocument(initcurrentFile.id, title); + $currentFile.rename(title, api); currentFileName = title; if ($currentFile != null) { - const newState: FileInfo = { ...$currentFile }; - newState['name'] = title; - const id = $currentFile.id; + const newState: FileInfo = $currentFile; + newState.name = title; + const id = newState.id; // Update the value for the specified key documentStore.update((prev: FileInfo[]): FileInfo[] => { return prev.map((it) => {