plane/web/store/cycles.ts

102 lines
2.3 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 { IssueService } from "services/issue.service";
2023-09-25 07:05:42 +00:00
import { ICycle } from "types";
import { CycleService } from "services/cycles.service";
2023-09-20 06:52:48 +00:00
export interface ICycleStore {
loader: boolean;
error: any | null;
2023-09-25 07:05:42 +00:00
cycles: {
[project_id: string]: ICycle[];
};
cycle_details: {
2023-09-25 07:05:42 +00:00
[cycle_id: string]: ICycle;
};
2023-09-28 15:00:48 +00:00
fetchCycles: (
workspaceSlug: string,
projectSlug: string,
params: "all" | "current" | "upcoming" | "draft" | "completed" | "incomplete"
) => Promise<void>;
2023-09-20 06:52:48 +00:00
}
class CycleStore implements ICycleStore {
loader: boolean = false;
error: any | null = null;
2023-09-25 07:05:42 +00:00
cycles: {
[project_id: string]: ICycle[];
} = {};
cycle_details: {
2023-09-25 07:05:42 +00:00
[cycle_id: string]: ICycle;
} = {};
2023-09-20 06:52:48 +00:00
// root store
rootStore;
// services
projectService;
issueService;
cycleService;
2023-09-20 06:52:48 +00:00
constructor(_rootStore: RootStore) {
makeObservable(this, {
loader: observable,
error: observable.ref,
2023-09-25 07:05:42 +00:00
cycles: observable.ref,
2023-09-20 06:52:48 +00:00
// computed
2023-09-28 15:00:48 +00:00
projectCycles: computed,
2023-09-20 06:52:48 +00:00
// actions
2023-09-28 15:00:48 +00:00
fetchCycles: action,
2023-09-20 06:52:48 +00:00
});
this.rootStore = _rootStore;
2023-09-21 09:30:19 +00:00
this.projectService = new ProjectService();
this.issueService = new IssueService();
this.cycleService = new CycleService();
2023-09-20 06:52:48 +00:00
}
// computed
get projectCycles() {
if (!this.rootStore.project.projectId) return null;
return this.cycles[this.rootStore.project.projectId] || null;
}
2023-09-20 06:52:48 +00:00
// actions
2023-09-28 15:00:48 +00:00
fetchCycles = async (
workspaceSlug: string,
projectSlug: string,
params: "all" | "current" | "upcoming" | "draft" | "completed" | "incomplete"
) => {
try {
this.loader = true;
this.error = null;
2023-09-28 15:00:48 +00:00
const cyclesResponse = await this.cycleService.getCyclesWithParams(workspaceSlug, projectSlug, params);
runInAction(() => {
this.cycles = {
...this.cycles,
[projectSlug]: cyclesResponse,
};
this.loader = false;
this.error = null;
});
} catch (error) {
console.error("Failed to fetch project cycles in project store", error);
this.loader = false;
this.error = error;
}
};
2023-09-20 06:52:48 +00:00
}
export default CycleStore;