From cb341f19c6ce0079a5a04f7a0bb8a14b84fb8175 Mon Sep 17 00:00:00 2001 From: Dakshesh Jain Date: Fri, 18 Aug 2023 17:52:03 +0530 Subject: [PATCH] fix: reverted to old setup --- apps/app/store/label.ts | 183 +++++++++++----------------------------- 1 file changed, 47 insertions(+), 136 deletions(-) diff --git a/apps/app/store/label.ts b/apps/app/store/label.ts index 0b531f0d6..143f43aae 100644 --- a/apps/app/store/label.ts +++ b/apps/app/store/label.ts @@ -7,27 +7,16 @@ import issueService from "services/issues.service"; // types import type { IIssueLabels, ICurrentUserResponse, LabelForm } from "types"; -type LabelDataStore = { - [projectId: string]: { - labels: IIssueLabels[]; - isLoading: boolean; - isRevalidating: boolean; - error?: any; - }; -} | null; - class LabelStore { - data: LabelDataStore = null; - // labels: IIssueLabels[] = []; - // isLabelsLoading: boolean = false; + labels: IIssueLabels[] = []; + isLabelsLoading: boolean = false; rootStore: any | null = null; constructor(_rootStore: any | null = null) { makeAutoObservable(this, { - // labels: observable.ref, - data: observable.ref, + labels: observable.ref, loadLabels: action, - // isLabelsLoading: observable, + isLabelsLoading: observable, createLabel: action, updateLabel: action, deleteLabel: action, @@ -41,89 +30,36 @@ class LabelStore { */ loadLabels = async (workspaceSlug: string, projectId: string) => { - // this.isLabelsLoading = this.labels.length === 0; - - this.data = this.data || { - [projectId]: { - // labels: [...this.labels], - labels: [...this.getLabelsByProjectId(projectId)], - isLoading: this.getLabelsByProjectId(projectId).length === 0, - isRevalidating: true, - error: null, - }, - }; - + this.isLabelsLoading = this.labels.length === 0; try { const labelsResponse: IIssueLabels[] = await issueService.getIssueLabels( workspaceSlug, projectId ); - // const _labels = [...(labelsResponse || [])].map((label) => ({ - // id: label.id, - // name: label.name, - // description: label.description, - // color: label.color, - // parent: label.parent, - // })); - - const _data = this.data?.[projectId] || { - labels: [ - ...labelsResponse.map((label) => ({ - id: label.id, - name: label.name, - description: label.description, - color: label.color, - parent: label.parent, - project: label.project, - })), - ].sort((a, b) => a.name.localeCompare(b.name)), - isLoading: false, - isRevalidating: false, - }; + const _labels = [...(labelsResponse || [])].map((label) => ({ + id: label.id, + name: label.name, + description: label.description, + color: label.color, + parent: label.parent, + })); runInAction(() => { - this.data = { - ...this.data, - [projectId]: _data, - }; - // this.labels = _labels; - // this.isLabelsLoading = false; + this.labels = _labels; + this.isLabelsLoading = false; }); } catch (error) { runInAction(() => { - this.data = { - ...this.data, - [projectId]: { - labels: [...this.getLabelsByProjectId(projectId)], - isLoading: false, - isRevalidating: false, - error, - }, - }; - // this.isLabelsLoading = false; + this.isLabelsLoading = false; }); console.error("Fetching labels error", error); } }; - // getLabelById = (labelId: string) => this.labels.find((label) => label.id === labelId); + getLabelById = (labelId: string) => this.labels.find((label) => label.id === labelId); - getLabelById = (projectId: string, labelId: string) => - this.data?.[projectId]?.labels.find((label) => label.id === labelId) || null; - - /** - * - * @param projectId - * @returns {IIssueLabels[]} array of labels of a project - */ - getLabelsByProjectId = (projectId: string): IIssueLabels[] => - this.data?.[projectId]?.labels || []; - - // getLabelChildren = (labelId: string) => this.labels.filter((label) => label.parent === labelId); - - getLabelChildren = (projectId: string, labelId: string) => - this.data?.[projectId]?.labels.filter((label) => label.parent === labelId) || []; + getLabelChildren = (labelId: string) => this.labels.filter((label) => label.parent === labelId); /** * For provided query, this function returns all labels that contain query in their name from the labels store. @@ -132,10 +68,8 @@ class LabelStore { * @example * getFilteredLabels("labe") // [{ id: "1", name: "label1", description: "", color: "", parent: null }] */ - getFilteredLabels = (projectId: string | null, query: string): IIssueLabels[] => { - if (!projectId) return []; - return this.data?.[projectId]?.labels.filter((label) => label.name.includes(query)) || []; - }; + getFilteredLabels = (query: string): IIssueLabels[] => + this.labels.filter((label) => label.name.includes(query)); createLabel = async ( workspaceSlug: string, @@ -151,27 +85,19 @@ class LabelStore { user ); - const _data = this.data?.[projectId] || { - labels: [ - ...this.getLabelsByProjectId(projectId), - { - id: labelResponse.id, - name: labelResponse.name, - description: labelResponse.description, - color: labelResponse.color, - parent: labelResponse.parent, - project: labelResponse.project, - }, - ].sort((a, b) => a.name.localeCompare(b.name)), - isLoading: false, - isRevalidating: false, - }; + const _label = [ + ...this.labels, + { + id: labelResponse.id, + name: labelResponse.name, + description: labelResponse.description, + color: labelResponse.color, + parent: labelResponse.parent, + }, + ].sort((a, b) => a.name.localeCompare(b.name)); runInAction(() => { - this.data = { - ...this.data, - [projectId]: _data, - }; + this.labels = _label; }); return labelResponse; } catch (error) { @@ -196,31 +122,23 @@ class LabelStore { user ); - const _data = this.data?.[projectId] || { - labels: [ - ...this.getLabelsByProjectId(projectId).map((label) => { - if (label.id === labelId) { - return { - id: labelResponse.id, - name: labelResponse.name, - description: labelResponse.description, - color: labelResponse.color, - parent: labelResponse.parent, - project: labelResponse.project, - }; - } - return label; - }), - ], - isLoading: false, - isRevalidating: false, - }; + const _labels = [...this.labels] + .map((label) => { + if (label.id === labelId) { + return { + id: labelResponse.id, + name: labelResponse.name, + description: labelResponse.description, + color: labelResponse.color, + parent: labelResponse.parent, + }; + } + return label; + }) + .sort((a, b) => a.name.localeCompare(b.name)); runInAction(() => { - this.data = { - ...this.data, - [projectId]: _data, - }; + this.labels = _labels; }); } catch (error) { console.error("Updating label error", error); @@ -237,17 +155,10 @@ class LabelStore { try { issueService.deleteIssueLabel(workspaceSlug, projectId, labelId, user); - const _data = this.data?.[projectId] || { - labels: [...this.getLabelsByProjectId(projectId)].filter((label) => label.id !== labelId), - isLoading: false, - isRevalidating: false, - }; + const _labels = [...this.labels].filter((label) => label.id !== labelId); runInAction(() => { - this.data = { - ...this.data, - [projectId]: _data, - }; + this.labels = _labels; }); } catch (error) { console.error("Deleting label error", error);