add handlers in TaskModel

This commit is contained in:
marlon-erler
2024-07-22 11:49:05 +02:00
parent 6d126a2fa8
commit 29c3e4ad43
4 changed files with 160 additions and 39 deletions
+37 -19
View File
@@ -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<string>): 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<string> | null => {
const filePath = this.getFileContentPath(file, fileContentName);
const fileContentOrNull: FileContent | null =
const fileContentOrNull: FileContent<string> | 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>): 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 = <T extends string>(type: T): FileContent<T> => {
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<FileContent<string>>;
}
export interface FileContent extends ValidObject {
export interface FileContent<T extends string> extends ValidObject {
id: string;
creationDate: string;
type: string;
type: T;
}
// references
export const FileContentReference: FileContent = {
export const FileContentReference: FileContent<string> = {
dataVersion: DATA_VERSION,
id: "",
@@ -148,5 +166,5 @@ export const FileReference: File = {
dataVersion: DATA_VERSION,
id: "",
contentVersions: [FileContentReference],
contentVersions: new Set([FileContentReference]),
};
+54 -6
View File
@@ -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<string>
): 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: "",
};
+1 -1
View File
@@ -63,4 +63,4 @@ document
ChatPageWrapper(chatListViewModel),
ConnectionModal(connectionViewModel),
StorageModal(storageViewModel)
);
);