plane/web/store/issue_detail.ts

176 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-09-15 10:07:54 +00:00
import { observable, action, makeObservable, runInAction } from "mobx";
// types
2023-09-20 06:52:48 +00:00
import { RootStore } from "./root";
2023-09-15 10:07:54 +00:00
// services
2023-09-21 09:30:19 +00:00
import { IssueService } from "services/issue.service";
2023-09-21 11:24:11 +00:00
import { IIssue } from "types";
2023-09-15 10:07:54 +00:00
export type IPeekMode = "side" | "modal" | "full";
2023-09-21 11:24:11 +00:00
export interface IIssueDetailStore {
2023-09-15 10:07:54 +00:00
loader: boolean;
error: any | null;
peekId: string | null;
peekMode: IPeekMode | null;
2023-09-21 11:24:11 +00:00
issues: {
[key: string]: IIssue;
2023-09-15 10:07:54 +00:00
};
setPeekId: (issueId: string | null) => void;
setPeekMode: (issueId: IPeekMode | null) => void;
// fetch issue details
2023-09-21 11:24:11 +00:00
fetchIssueDetails: (workspaceId: string, projectId: string, issueId: string) => void;
2023-09-15 10:07:54 +00:00
// creating issue
2023-09-21 11:24:11 +00:00
createIssue: (workspaceId: string, projectId: string, issueId: string, data: any) => void;
2023-09-15 10:07:54 +00:00
// updating issue
2023-09-21 11:24:11 +00:00
updateIssue: (workspaceId: string, projectId: string, issueId: string, data: any) => void;
2023-09-15 10:07:54 +00:00
// deleting issue
2023-09-21 11:24:11 +00:00
deleteIssue: (workspaceId: string, projectId: string, issueId: string) => void;
2023-09-15 10:07:54 +00:00
}
2023-09-21 11:24:11 +00:00
class IssueDetailStore implements IIssueDetailStore {
2023-09-15 10:07:54 +00:00
loader: boolean = false;
error: any | null = null;
peekId: string | null = null;
peekMode: IPeekMode | null = null;
2023-09-21 11:24:11 +00:00
issues: {
[key: string]: IIssue;
} = {};
2023-09-15 10:07:54 +00:00
// root store
rootStore;
// service
issueService;
constructor(_rootStore: RootStore) {
makeObservable(this, {
// observable
2023-09-21 11:24:11 +00:00
loader: observable.ref,
error: observable.ref,
peekId: observable.ref,
peekMode: observable.ref,
2023-09-15 10:07:54 +00:00
2023-09-21 11:24:11 +00:00
issues: observable.ref,
2023-09-15 10:07:54 +00:00
setPeekId: action,
setPeekMode: action,
2023-09-21 11:24:11 +00:00
fetchIssueDetails: action,
createIssue: action,
updateIssue: action,
deleteIssue: action,
2023-09-15 10:07:54 +00:00
});
this.rootStore = _rootStore;
2023-09-21 09:30:19 +00:00
this.issueService = new IssueService();
2023-09-15 10:07:54 +00:00
}
setPeekId = (issueId: string | null) => (this.peekId = issueId);
2023-09-21 11:24:11 +00:00
2023-09-15 10:07:54 +00:00
setPeekMode = (mode: IPeekMode | null) => (this.peekMode = mode);
2023-09-21 11:24:11 +00:00
fetchIssueDetails = async (workspaceId: string, projectId: string, issueId: string) => {
2023-09-15 10:07:54 +00:00
try {
this.loader = true;
this.error = null;
2023-09-21 11:24:11 +00:00
const issueDetailsResponse = await this.issueService.retrieve(workspaceId, projectId, issueId);
2023-09-15 10:07:54 +00:00
runInAction(() => {
this.loader = false;
this.error = null;
2023-09-21 11:24:11 +00:00
this.issues = {
[issueId]: issueDetailsResponse,
};
2023-09-15 10:07:54 +00:00
});
} catch (error) {
console.log("error in fetching issue details", error);
this.loader = false;
this.error = error;
return error;
}
};
2023-09-21 11:24:11 +00:00
createIssue = async (workspaceId: string, projectId: string, issueId: string, data: any) => {
2023-09-15 10:07:54 +00:00
try {
this.loader = true;
this.error = null;
console.log("workspaceId", workspaceId);
console.log("projectId", projectId);
console.log("issueId", issueId);
console.log("data", data);
runInAction(() => {
this.loader = false;
this.error = null;
});
} catch (error) {
console.log("error in fetching issue details", error);
this.loader = false;
this.error = error;
return error;
}
};
2023-09-21 11:24:11 +00:00
updateIssue = async (workspaceId: string, projectId: string, issueId: string, data: any) => {
2023-09-15 10:07:54 +00:00
try {
this.loader = true;
this.error = null;
2023-09-21 11:24:11 +00:00
const filteredParams = this.rootStore.issueFilter.getComputedFilters(
2023-09-15 11:25:38 +00:00
workspaceId,
projectId,
null,
null,
null,
this.rootStore.issueFilters.issueView || "issues"
);
2023-09-15 10:07:54 +00:00
const issueResponse = await this.issueService.patchIssue(workspaceId, projectId, issueId, data, undefined);
2023-09-19 07:20:27 +00:00
const issueList = (await this.issueService.getIssuesWithParams(workspaceId, projectId, filteredParams)) as any;
2023-09-15 11:25:38 +00:00
console.log("issueList", issueList);
2023-09-15 10:07:54 +00:00
if (issueResponse) {
runInAction(() => {
this.loader = false;
this.error = null;
2023-09-15 11:25:38 +00:00
this.rootStore.issueView.issues[workspaceId].project_issues[projectId].issues.list = issueList;
2023-09-15 10:07:54 +00:00
});
}
} catch (error) {
console.log("error in fetching issue details", error);
this.loader = false;
this.error = error;
return error;
}
};
2023-09-21 11:24:11 +00:00
deleteIssue = async (workspaceId: string, projectId: string, issueId: string) => {
2023-09-15 10:07:54 +00:00
try {
this.loader = true;
this.error = null;
console.log("workspaceId", workspaceId);
console.log("projectId", projectId);
console.log("issueId", issueId);
runInAction(() => {
this.loader = false;
this.error = null;
});
} catch (error) {
console.log("error in fetching issue details", error);
this.loader = false;
this.error = error;
return error;
}
};
}
2023-09-21 11:24:11 +00:00
export default IssueDetailStore;