import { action, makeObservable, runInAction } from "mobx"; // base class import { WorkspaceService } from "@/services/workspace.service"; import { IssuePaginationOptions, TBulkOperationsPayload, TIssue, TIssuesResponse, TLoader, ViewFlags } from "@plane/types"; // services // types import { IIssueRootStore } from "../root.store"; import { BaseIssuesStore, IBaseIssuesStore } from "../helpers/base-issues.store"; import { IWorkspaceIssuesFilter } from "./filter.store"; export interface IWorkspaceIssues extends IBaseIssuesStore { // observable viewFlags: ViewFlags; // actions fetchIssues: ( workspaceSlug: string, viewId: string, loadType: TLoader, options: IssuePaginationOptions ) => Promise; fetchIssuesWithExistingPagination: ( workspaceSlug: string, viewId: string, loadType: TLoader ) => Promise; fetchNextIssues: ( workspaceSlug: string, viewId: string, groupId?: string, subGroupId?: string ) => Promise; createIssue: (workspaceSlug: string, projectId: string, data: Partial) => Promise; updateIssue: (workspaceSlug: string, projectId: string, issueId: string, data: Partial) => Promise; archiveIssue: (workspaceSlug: string, projectId: string, issueId: string) => Promise; removeBulkIssues: (workspaceSlug: string, projectId: string, issueIds: string[]) => Promise; archiveBulkIssues: (workspaceSlug: string, projectId: string, issueIds: string[]) => Promise; bulkUpdateProperties: (workspaceSlug: string, projectId: string, data: TBulkOperationsPayload) => Promise; quickAddIssue: undefined; clear(): void; } export class WorkspaceIssues extends BaseIssuesStore implements IWorkspaceIssues { viewFlags = { enableQuickAdd: true, enableIssueCreation: true, enableInlineEditing: true, }; // service workspaceService; // filterStore issueFilterStore; constructor(_rootStore: IIssueRootStore, issueFilterStore: IWorkspaceIssuesFilter) { super(_rootStore, issueFilterStore); makeObservable(this, { // action fetchIssues: action, fetchNextIssues: action, fetchIssuesWithExistingPagination: action, }); // services this.workspaceService = new WorkspaceService(); // filter store this.issueFilterStore = issueFilterStore; } fetchParentStats = () => {}; /** * This method is called to fetch the first issues of pagination * @param workspaceSlug * @param viewId * @param loadType * @param options * @returns */ fetchIssues = async (workspaceSlug: string, viewId: string, loadType: TLoader, options: IssuePaginationOptions) => { try { // set loader and clear store runInAction(() => { this.setLoader(loadType); }); this.clear(); // get params from pagination options const params = this.issueFilterStore?.getFilterParams(viewId, options, undefined, undefined, undefined); // call the fetch issues API with the params const response = await this.workspaceService.getViewIssues(workspaceSlug, params); // after fetching issues, call the base method to process the response further this.onfetchIssues(response, options, workspaceSlug); return response; } catch (error) { // set loader to undefined if errored out this.setLoader(undefined); throw error; } }; /** * This method is called subsequent pages of pagination * if groupId/subgroupId is provided, only that specific group's next page is fetched * else all the groups' next page is fetched * @param workspaceSlug * @param viewId * @param groupId * @param subGroupId * @returns */ fetchNextIssues = async (workspaceSlug: string, viewId: string, groupId?: string, subGroupId?: string) => { const cursorObject = this.getPaginationData(groupId, subGroupId); // if there are no pagination options and the next page results do not exist the return if (!this.paginationOptions || (cursorObject && !cursorObject?.nextPageResults)) return; try { // set Loader this.setLoader("pagination", groupId, subGroupId); // get params from stored pagination options const params = this.issueFilterStore?.getFilterParams( viewId, this.paginationOptions, cursorObject?.nextCursor, groupId, subGroupId ); // call the fetch issues API with the params for next page in issues const response = await this.workspaceService.getViewIssues(workspaceSlug, params); // after the next page of issues are fetched, call the base method to process the response this.onfetchNexIssues(response, groupId, subGroupId); return response; } catch (error) { // set Loader as undefined if errored out this.setLoader(undefined, groupId, subGroupId); throw error; } }; /** * This Method exists to fetch the first page of the issues with the existing stored pagination * This is useful for refetching when filters, groupBy, orderBy etc changes * @param workspaceSlug * @param viewId * @param loadType * @returns */ fetchIssuesWithExistingPagination = async (workspaceSlug: string, viewId: string, loadType: TLoader) => { if (!this.paginationOptions) return; return await this.fetchIssues(workspaceSlug, viewId, loadType, this.paginationOptions); }; archiveBulkIssues = this.bulkArchiveIssues; quickAddIssue = undefined; }