import { observable, action, makeObservable, runInAction, autorun } from "mobx";
// types
import { RootStore } from "../root";
// services
import { InboxService } from "services/inbox.service";
import { IInboxIssue } from "types";
export interface IInboxIssuesStore {
// states
loader: boolean;
error: any | null;
// observables
inboxIssues: {
[inboxId: string]: IInboxIssue[];
};
// actions
fetchInboxIssues: (workspaceSlug: string, projectId: string, inboxId: string) => Promise<IInboxIssue[]>;
}
export class InboxIssuesStore implements IInboxIssuesStore {
loader: boolean = false;
error: any | null = null;
} = {};
// root store
rootStore;
inboxService;
constructor(_rootStore: RootStore) {
makeObservable(this, {
loader: observable.ref,
error: observable.ref,
inboxIssues: observable.ref,
fetchInboxIssues: action,
});
this.rootStore = _rootStore;
this.inboxService = new InboxService();
autorun(() => {
const workspaceSlug = this.rootStore.workspace.workspaceSlug;
const projectId = this.rootStore.project.projectId;
const inboxId = this.rootStore.inbox.inboxId;
if (workspaceSlug && projectId && inboxId && this.rootStore.inboxFilters.inboxFilters[inboxId])
this.fetchInboxIssues(workspaceSlug, projectId, inboxId);
fetchInboxIssues = async (workspaceSlug: string, projectId: string, inboxId: string) => {
try {
runInAction(() => {
this.loader = true;
const queryParams = this.rootStore.inboxFilters.appliedFilters ?? undefined;
const issuesResponse = await this.inboxService.getInboxIssues(workspaceSlug, projectId, inboxId, queryParams);
this.loader = false;
this.inboxIssues = {
...this.inboxIssues,
[inboxId]: issuesResponse,
return issuesResponse;
} catch (error) {
this.error = error;
throw error;