plane/web/store/cycles.ts

137 lines
3.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 { 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: {
2023-10-02 15:03:28 +00:00
[project_id: string]: {
[filter_name: 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,
2023-10-02 15:03:28 +00:00
projectId: string,
2023-09-28 15:00:48 +00:00
params: "all" | "current" | "upcoming" | "draft" | "completed" | "incomplete"
) => Promise<void>;
2023-10-02 15:03:28 +00:00
createCycle: (workspaceSlug: string, projectId: string, data: any, filter: string) => Promise<ICycle>;
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: {
2023-10-02 15:03:28 +00:00
[project_id: string]: {
[filter_name: 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-10-02 15:03:28 +00:00
createCycle: 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,
2023-10-02 15:03:28 +00:00
projectId: string,
2023-09-28 15:00:48 +00:00
params: "all" | "current" | "upcoming" | "draft" | "completed" | "incomplete"
) => {
try {
this.loader = true;
this.error = null;
2023-10-02 15:03:28 +00:00
const cyclesResponse = await this.cycleService.getCyclesWithParams(workspaceSlug, projectId, params);
runInAction(() => {
this.cycles = {
...this.cycles,
2023-10-02 15:03:28 +00:00
[projectId]: {
[params]: 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-10-02 15:03:28 +00:00
createCycle = async (workspaceSlug: string, projectId: string, data: any, filter: string) => {
try {
const response = await this.cycleService.createCycle(
workspaceSlug,
projectId,
data,
this.rootStore.user.currentUser
);
runInAction(() => {
this.cycles = {
...this.cycles,
[projectId]: {
...this.cycles[projectId],
[filter]: [...this.cycles[projectId][filter], response],
},
};
});
return response;
} catch (error) {
console.log("Failed to create cycle from cycle store");
throw error;
}
};
2023-09-20 06:52:48 +00:00
}
export default CycleStore;