From 29c3e4ad439a12b55b1be54dd048bddea1f5bde7 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Mon, 22 Jul 2024 11:49:05 +0200 Subject: [PATCH] add handlers in TaskModel --- dist/index.js | 81 ++++++++++++++++++++++++++++++------ src/Model/Files/fileModel.ts | 56 ++++++++++++++++--------- src/Model/Files/taskModel.ts | 60 +++++++++++++++++++++++--- src/index.tsx | 2 +- 4 files changed, 160 insertions(+), 39 deletions(-) diff --git a/dist/index.js b/dist/index.js index 633af5f9..f9fd28f1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -289,11 +289,14 @@ const requiredType = typeof reference[key]; const actualType = typeof objectToCheck[key]; if (requiredType != actualType) return false; - if (Array.isArray(reference[key])) { + if (Array.isArray(reference[key]) || reference[key] instanceof Set) { if (objectToCheck[key].length == 0) continue; + if (objectToCheck[key].size == 0) continue; + const [firstOfObjectToCheck] = objectToCheck[key]; + const [fisrtOfReference] = reference[key]; const doesFirstItemMatch = checkMatchesObjectStructure( - objectToCheck[key][0], - reference[key][0] + firstOfObjectToCheck, + fisrtOfReference ); if (doesFirstItemMatch == false) return false; } else if (requiredType == "object") { @@ -478,14 +481,50 @@ }; // src/Model/Files/taskModel.ts - var TaskModel = class { + var TaskModel = class _TaskModel { chatModel; fileModel; + // handler + handleTaskFileContent = (file, fileContent) => { + if (checkMatchesObjectStructure(fileContent, TaskFileContentReference) == false) + return; + }; + // methods + createTask = (board, name) => { + const file = FileModel.createFile(); + const taskFileContent = _TaskModel.createTaskFileContent( + board, + name + ); + file.contentVersions.add(taskFileContent); + this.fileModel.addOrUpdateFile(file); + }; + saveTask = (file, newTaskFileContent) => { + file.contentVersions.add(newTaskFileContent); + this.fileModel.addOrUpdateFile(file); + }; // init constructor(chatModel, fileModel) { this.chatModel = chatModel; this.fileModel = fileModel; } + // utility + static createTaskFileContent = (name, board) => { + const fileContent = FileModel.createFileContent("task"); + return { + ...fileContent, + name, + board + }; + }; + }; + var TaskFileContentReference = { + dataVersion: DATA_VERSION, + id: "", + creationDate: "", + type: "task", + name: "", + board: "" }; // src/Model/Files/fileModel.ts @@ -511,18 +550,20 @@ handleStringifiedFile = (stringifiedFile) => { const file = parseValidObject(stringifiedFile, FileReference); if (file == null) return; - this.storeFile(file); + this.handleFile(file); + }; + handleFile = (file) => { + for (const fileContent of file.contentVersions) { + this.storeFileContent(file, fileContent); + this.taskModel.handleTaskFileContent(file, fileContent); + } }; // methods - addFile = (file) => { + addOrUpdateFile = (file) => { + this.handleFile(file); this.chatModel.sendMessage("", file); }; // storage - storeFile = (file) => { - for (const fileContent of file.contentVersions) { - this.storeFileContent(file, fileContent); - } - }; storeFileContent = (file, fileContent) => { const fileContentName = _FileModel.getFileContentName(fileContent); const fileContentPath = this.getFileContentPath( @@ -571,7 +612,15 @@ return { dataVersion: DATA_VERSION, id: v4_default(), - contentVersions: [] + contentVersions: /* @__PURE__ */ new Set() + }; + }; + static createFileContent = (type) => { + return { + dataVersion: DATA_VERSION, + id: v4_default(), + creationDate: createTimestamp(), + type }; }; }; @@ -584,7 +633,7 @@ var FileReference = { dataVersion: DATA_VERSION, id: "", - contentVersions: [FileContentReference] + contentVersions: /* @__PURE__ */ new Set([FileContentReference]) }; // src/Model/Utility/crypto.ts @@ -2518,4 +2567,10 @@ ConnectionModal(connectionViewModel), StorageModal(storageViewModel) ); + console.log( + checkMatchesObjectStructure( + { a: 1, c: /* @__PURE__ */ new Set() }, + { a: 2, c: /* @__PURE__ */ new Set(["bla"]) } + ) + ); })(); diff --git a/src/Model/Files/fileModel.ts b/src/Model/Files/fileModel.ts index 2022ef11..71bdcf6c 100644 --- a/src/Model/Files/fileModel.ts +++ b/src/Model/Files/fileModel.ts @@ -2,7 +2,11 @@ import { DATA_VERSION, ValidObject } from "../Utility/typeSafety"; import StorageModel, { filePaths } from "../Global/storageModel"; -import { parseValidObject, stringify } from "../Utility/utility"; +import { + createTimestamp, + parseValidObject, + stringify, +} from "../Utility/utility"; import ChatModel from "../Chat/chatModel"; import TaskModel from "./taskModel"; @@ -36,22 +40,26 @@ export default class FileModel { const file: File | null = parseValidObject(stringifiedFile, FileReference); if (file == null) return; - this.storeFile(file); + this.handleFileAndClearContents(file); }; - // methods - addFile = (file: File): void => { - this.chatModel.sendMessage("", file); - }; - - // storage - storeFile = (file: File): void => { + handleFileAndClearContents = (file: File): void => { for (const fileContent of file.contentVersions) { this.storeFileContent(file, fileContent); + this.taskModel.handleTaskFileContent(file, fileContent); + + file.contentVersions.delete(fileContent); } }; - storeFileContent = (file: File, fileContent: FileContent): void => { + // methods + addOrUpdateFile = (file: File): void => { + this.chatModel.sendMessage("", file); + this.handleFileAndClearContents(file); + }; + + // storage + storeFileContent = (file: File, fileContent: FileContent): void => { const fileContentName: string = FileModel.getFileContentName(fileContent); const fileContentPath: string[] = this.getFileContentPath( file, @@ -92,9 +100,9 @@ export default class FileModel { getFileContent = ( file: File, fileContentName: string - ): FileContent | null => { + ): FileContent | null => { const filePath = this.getFileContentPath(file, fileContentName); - const fileContentOrNull: FileContent | null = + const fileContentOrNull: FileContent | null = this.storageModel.readStringifiable(filePath, FileContentReference); return fileContentOrNull; }; @@ -108,7 +116,7 @@ export default class FileModel { } // utility - static getFileContentName = (fileContent: FileContent): string => { + static getFileContentName = (fileContent: FileContent): string => { return fileContent.creationDate + fileContent.id; }; @@ -117,7 +125,17 @@ export default class FileModel { dataVersion: DATA_VERSION, id: v4(), - contentVersions: [], + contentVersions: new Set(), + }; + }; + + static createFileContent = (type: T): FileContent => { + return { + dataVersion: DATA_VERSION, + + id: v4(), + creationDate: createTimestamp(), + type, }; }; } @@ -125,18 +143,18 @@ export default class FileModel { // types export interface File extends ValidObject { id: string; - contentVersions: FileContent[]; + contentVersions: Set>; } -export interface FileContent extends ValidObject { +export interface FileContent extends ValidObject { id: string; creationDate: string; - type: string; + type: T; } // references -export const FileContentReference: FileContent = { +export const FileContentReference: FileContent = { dataVersion: DATA_VERSION, id: "", @@ -148,5 +166,5 @@ export const FileReference: File = { dataVersion: DATA_VERSION, id: "", - contentVersions: [FileContentReference], + contentVersions: new Set([FileContentReference]), }; diff --git a/src/Model/Files/taskModel.ts b/src/Model/Files/taskModel.ts index f2e5e1a2..426ee79a 100644 --- a/src/Model/Files/taskModel.ts +++ b/src/Model/Files/taskModel.ts @@ -1,26 +1,73 @@ // thile file is responsible for managing task files within chats. -import FileModel, { FileContent } from "./fileModel"; +import { + DATA_VERSION, + checkMatchesObjectStructure, +} from "../Utility/typeSafety"; +import FileModel, { File, FileContent } from "./fileModel"; import ChatModel from "../Chat/chatModel"; -import { DATA_VERSION } from "../Utility/typeSafety"; export default class TaskModel { chatModel: ChatModel; fileModel: FileModel; + // handler + handleTaskFileContent = ( + file: File, + fileContent: FileContent + ): void => { + if ( + checkMatchesObjectStructure(fileContent, TaskFileContentReference) == + false + ) + return; + + // TODO handle + }; + + // methods + createTask = (board: string, name: string): void => { + const file: File = FileModel.createFile(); + const taskFileContent: TaskFileContent = TaskModel.createTaskFileContent( + board, + name + ); + this.saveTask(file, taskFileContent); + }; + + saveTask = (file: File, newTaskFileContent: TaskFileContent): void => { + file.contentVersions.add(newTaskFileContent); + this.fileModel.addOrUpdateFile(file); + }; + // init constructor(chatModel: ChatModel, fileModel: FileModel) { this.chatModel = chatModel; this.fileModel = fileModel; } + + // utility + static createTaskFileContent = ( + name: string, + board: string + ): TaskFileContent => { + const fileContent: FileContent<"task"> = + FileModel.createFileContent("task"); + return { + ...fileContent, + + name, + board, + }; + }; } // types -export interface TaskFileContent extends FileContent { - type: "task"; - +export interface TaskFileContent extends FileContent<"task"> { + name: string; board: string; + category?: string; status?: string; @@ -37,6 +84,7 @@ export const TaskFileContentReference: TaskFileContent = { id: "", creationDate: "", type: "task", - + + name: "", board: "", }; diff --git a/src/index.tsx b/src/index.tsx index aafba104..ba7ff69d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -63,4 +63,4 @@ document ChatPageWrapper(chatListViewModel), ConnectionModal(connectionViewModel), StorageModal(storageViewModel) - ); + ); \ No newline at end of file