plane/web/store/modules.ts

99 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-09-20 06:52:48 +00:00
import { action, computed, observable, makeObservable, runInAction } from "mobx";
// types
import { RootStore } from "./root";
// services
2023-09-21 09:30:19 +00:00
import { ProjectService } from "services/project.service";
import { ModuleService } from "services/modules.service";
import { IModule } from "@/types";
2023-09-20 06:52:48 +00:00
export interface IModuleStore {
loader: boolean;
error: any | null;
moduleId: string | null;
modules: {
[project_id: string]: IModule[];
};
module_details: {
[module_id: string]: IModule;
};
2023-09-20 06:52:48 +00:00
setModuleId: (moduleSlug: string) => void;
fetchModules: (workspaceSlug: string, projectSlug: string) => void;
2023-09-20 06:52:48 +00:00
}
class ModuleStore implements IModuleStore {
loader: boolean = false;
error: any | null = null;
moduleId: string | null = null;
modules: {
[project_id: string]: IModule[];
} = {};
module_details: {
[module_id: string]: IModule;
} = {};
2023-09-20 06:52:48 +00:00
// root store
rootStore;
// services
projectService;
moduleService;
2023-09-20 06:52:48 +00:00
constructor(_rootStore: RootStore) {
makeObservable(this, {
loader: observable,
error: observable.ref,
moduleId: observable.ref,
// computed
// actions
setModuleId: action,
});
this.rootStore = _rootStore;
2023-09-21 09:30:19 +00:00
this.projectService = new ProjectService();
this.moduleService = new ModuleService();
2023-09-20 06:52:48 +00:00
}
// computed
get projectModules() {
if (!this.rootStore.project.projectId) return null;
return this.modules[this.rootStore.project.projectId] || null;
}
2023-09-20 06:52:48 +00:00
// actions
setModuleId = (moduleSlug: string) => {
this.moduleId = moduleSlug ?? null;
};
fetchModules = async (workspaceSlug: string, projectSlug: string) => {
try {
this.loader = true;
this.error = null;
const modulesResponse = await this.moduleService.getModules(workspaceSlug, projectSlug);
runInAction(() => {
this.modules = {
...this.modules,
[projectSlug]: modulesResponse,
};
this.loader = false;
this.error = null;
});
} catch (error) {
console.error("Failed to fetch modules list in project store", error);
this.loader = false;
this.error = error;
}
};
2023-09-20 06:52:48 +00:00
}
export default ModuleStore;