plane/web/store/cycles.ts

194 lines
5.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 17:50:14 +00:00
[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,
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 17:50:14 +00:00
createCycle: (workspaceSlug: string, projectId: string, data: any) => Promise<ICycle>;
2023-10-02 15:03:28 +00:00
2023-10-02 17:50:14 +00:00
addCycleToFavorites: (workspaceSlug: string, projectId: string, cycleId: string) => Promise<any>;
removeCycleFromFavorites: (workspaceSlug: string, projectId: string, cycleId: string) => 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: {
2023-10-02 17:50:14 +00:00
[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-10-02 15:03:28 +00:00
createCycle: action,
2023-10-02 17:50:14 +00:00
addCycleToFavorites: action,
removeCycleFromFavorites: 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 17:50:14 +00:00
[projectId]: 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
2023-10-02 17:50:14 +00:00
createCycle = async (workspaceSlug: string, projectId: string, data: any) => {
2023-10-02 15:03:28 +00:00
try {
const response = await this.cycleService.createCycle(
workspaceSlug,
projectId,
data,
this.rootStore.user.currentUser
);
runInAction(() => {
this.cycles = {
...this.cycles,
2023-10-02 17:50:14 +00:00
[projectId]: [...this.cycles[projectId], response],
2023-10-02 15:03:28 +00:00
};
});
return response;
} catch (error) {
console.log("Failed to create cycle from cycle store");
throw error;
}
};
2023-10-02 17:50:14 +00:00
addCycleToFavorites = async (workspaceSlug: string, projectId: string, cycleId: string) => {
try {
runInAction(() => {
this.cycles = {
...this.cycles,
[projectId]: this.cycles[projectId].map((cycle) => {
if (cycle.id === cycleId) return { ...cycle, is_favorite: true };
return cycle;
}),
};
});
// updating through api.
const response = await this.cycleService.addCycleToFavorites(workspaceSlug, projectId, { cycle: cycleId });
return response;
} catch (error) {
console.log("Failed to add cycle to favorites in the cycles store", error);
// resetting the local state
runInAction(() => {
this.cycles = {
...this.cycles,
[projectId]: this.cycles[projectId].map((cycle) => {
if (cycle.id === cycleId) return { ...cycle, is_favorite: false };
return cycle;
}),
};
});
throw error;
}
};
removeCycleFromFavorites = async (workspaceSlug: string, projectId: string, cycleId: string) => {
try {
runInAction(() => {
this.cycles = {
...this.cycles,
[projectId]: this.cycles[projectId].map((cycle) => {
if (cycle.id === cycleId) return { ...cycle, is_favorite: false };
return cycle;
}),
};
});
// updating through api
const response = await this.cycleService.removeCycleFromFavorites(workspaceSlug, projectId, cycleId);
return response;
} catch (error) {
console.log("Failed to remove cycle from favorites - Cycle Store", error);
// resetting the local state
runInAction(() => {
this.cycles = {
...this.cycles,
[projectId]: this.cycles[projectId].map((cycle) => {
if (cycle.id === cycleId) return { ...cycle, is_favorite: true };
return cycle;
}),
};
});
throw error;
}
};
2023-09-20 06:52:48 +00:00
}
export default CycleStore;