Files
CT/src/ViewModel/Pages/taskPageViewModel.ts
T

80 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-07-22 19:54:39 +02:00
import * as React from "bloatless-react";
import TaskModel, {
BoardInfoFileContent,
BoardInfoFileContentReference,
} from "../../Model/Files/taskModel";
2024-07-22 20:02:13 +02:00
2024-07-23 00:57:40 +02:00
import BoardViewModel from "./boardViewModel";
import { FileContent } from "../../Model/Files/fileModel";
2024-07-22 20:02:13 +02:00
import StorageModel from "../../Model/Global/storageModel";
import { checkMatchesObjectStructure } from "../../Model/Utility/typeSafety";
2024-07-22 20:02:13 +02:00
2024-07-22 21:07:44 +02:00
export default class TaskPageViewModel {
2024-07-22 20:02:13 +02:00
taskModel: TaskModel;
// state
newBoardNameInput: React.State<string> = new React.State("");
2024-07-23 01:40:30 +02:00
boardViewModels: React.MapState<BoardViewModel> = new React.MapState();
2024-07-22 20:02:13 +02:00
// guards
cannotCreateBoard: React.State<boolean> = React.createProxyState(
[this.newBoardNameInput],
() => this.newBoardNameInput.value == ""
);
// handlers
handleFileContent = (fileContent: FileContent<string>): void => {
if (
checkMatchesObjectStructure(fileContent, BoardInfoFileContentReference) ==
false
)
return;
this.showBoard(fileContent as BoardInfoFileContent);
};
2024-07-22 20:02:13 +02:00
// methods
createBoard = (): void => {
if (this.cannotCreateBoard.value == true) return;
2024-07-23 00:32:29 +02:00
const boardInfoFileContent: BoardInfoFileContent =
this.taskModel.createBoard(this.newBoardNameInput.value);
2024-07-22 20:02:13 +02:00
this.newBoardNameInput.value = "";
2024-07-23 00:05:48 +02:00
2024-07-23 00:57:40 +02:00
this.showBoard(boardInfoFileContent);
2024-07-23 00:05:48 +02:00
};
// view
showBoard = (boardInfo: BoardInfoFileContent): void => {
2024-07-23 00:57:40 +02:00
const boardViewModel: BoardViewModel = new BoardViewModel(this, boardInfo);
2024-07-23 01:40:30 +02:00
this.boardViewModels.set(boardInfo.fileId, boardViewModel);
};
2024-07-23 00:05:48 +02:00
// load
loadData = (): void => {
2024-07-23 00:57:40 +02:00
this.boardViewModels.clear();
2024-07-23 00:05:48 +02:00
const boardIds: string[] = this.taskModel.listBoardIds();
for (const boardId of boardIds) {
const boardInfo: BoardInfoFileContent | null =
this.taskModel.getBoardInfo(boardId);
if (boardInfo == null) continue;
this.showBoard(boardInfo);
2024-07-23 00:05:48 +02:00
}
2024-07-22 20:02:13 +02:00
};
// init
constructor(taskModel: TaskModel, storageModel: StorageModel) {
this.taskModel = taskModel;
2024-07-23 01:40:30 +02:00
// handlers
taskModel.boardHandlerManager.addHandler(
(boardInfoFileContent: BoardInfoFileContent) => {
this.showBoard(boardInfoFileContent);
}
);
2024-07-22 20:02:13 +02:00
}
}