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

109 lines
3.0 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";
2024-07-23 02:50:22 +02:00
import ChatViewModel from "../Chat/chatViewModel";
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-23 02:50:22 +02:00
storageModel: StorageModel;
2024-07-22 20:02:13 +02:00
taskModel: TaskModel;
2024-07-23 02:50:22 +02:00
chatViewModel: ChatViewModel;
2024-07-22 20:02:13 +02:00
// 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
2024-07-23 02:40:11 +02:00
selectedBoard: React.State<BoardViewModel | undefined> = new React.State<
BoardViewModel | undefined
>(undefined);
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;
2024-07-23 02:40:11 +02:00
this.showBoardInList(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 02:40:11 +02:00
this.showBoardInList(boardInfoFileContent);
2024-07-23 00:05:48 +02:00
};
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
this.taskModel.updateBoard(boardInfoFileContent);
};
// view
2024-07-23 02:40:11 +02:00
showBoardInList = (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 02:40:11 +02:00
selectBoard = (boardViewModel: BoardViewModel): void => {
this.selectedBoard.value = boardViewModel;
2024-07-23 02:50:22 +02:00
this.chatViewModel.displayedColor.value = boardViewModel.color.value;
2024-07-23 02:40:11 +02:00
};
closeBoard = (): void => {
this.selectedBoard.value = undefined;
2024-07-23 02:50:22 +02:00
this.chatViewModel.resetColor();
2024-07-23 02:40:11 +02:00
};
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;
2024-07-23 02:40:11 +02:00
this.showBoardInList(boardInfo);
2024-07-23 00:05:48 +02:00
}
2024-07-22 20:02:13 +02:00
};
// init
2024-07-23 02:50:22 +02:00
constructor(
taskModel: TaskModel,
storageModel: StorageModel,
chatViewModel: ChatViewModel
) {
2024-07-22 20:02:13 +02:00
this.taskModel = taskModel;
2024-07-23 02:50:22 +02:00
this.storageModel = storageModel;
this.chatViewModel = chatViewModel;
2024-07-23 01:40:30 +02:00
// handlers
taskModel.boardHandlerManager.addHandler(
(boardInfoFileContent: BoardInfoFileContent) => {
2024-07-23 02:40:11 +02:00
this.showBoardInList(boardInfoFileContent);
}
);
2024-07-22 20:02:13 +02:00
}
}