fix: reverted to old setup

This commit is contained in:
Dakshesh Jain 2023-08-18 17:52:03 +05:30
parent 0722f1f624
commit cb341f19c6

View File

@ -7,27 +7,16 @@ import issueService from "services/issues.service";
// types // types
import type { IIssueLabels, ICurrentUserResponse, LabelForm } from "types"; import type { IIssueLabels, ICurrentUserResponse, LabelForm } from "types";
type LabelDataStore = {
[projectId: string]: {
labels: IIssueLabels[];
isLoading: boolean;
isRevalidating: boolean;
error?: any;
};
} | null;
class LabelStore { class LabelStore {
data: LabelDataStore = null; labels: IIssueLabels[] = [];
// labels: IIssueLabels[] = []; isLabelsLoading: boolean = false;
// isLabelsLoading: boolean = false;
rootStore: any | null = null; rootStore: any | null = null;
constructor(_rootStore: any | null = null) { constructor(_rootStore: any | null = null) {
makeAutoObservable(this, { makeAutoObservable(this, {
// labels: observable.ref, labels: observable.ref,
data: observable.ref,
loadLabels: action, loadLabels: action,
// isLabelsLoading: observable, isLabelsLoading: observable,
createLabel: action, createLabel: action,
updateLabel: action, updateLabel: action,
deleteLabel: action, deleteLabel: action,
@ -41,89 +30,36 @@ class LabelStore {
*/ */
loadLabels = async (workspaceSlug: string, projectId: string) => { loadLabels = async (workspaceSlug: string, projectId: string) => {
// this.isLabelsLoading = this.labels.length === 0; 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,
},
};
try { try {
const labelsResponse: IIssueLabels[] = await issueService.getIssueLabels( const labelsResponse: IIssueLabels[] = await issueService.getIssueLabels(
workspaceSlug, workspaceSlug,
projectId projectId
); );
// const _labels = [...(labelsResponse || [])].map((label) => ({ 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, id: label.id,
name: label.name, name: label.name,
description: label.description, description: label.description,
color: label.color, color: label.color,
parent: label.parent, parent: label.parent,
project: label.project, }));
})),
].sort((a, b) => a.name.localeCompare(b.name)),
isLoading: false,
isRevalidating: false,
};
runInAction(() => { runInAction(() => {
this.data = { this.labels = _labels;
...this.data, this.isLabelsLoading = false;
[projectId]: _data,
};
// this.labels = _labels;
// this.isLabelsLoading = false;
}); });
} catch (error) { } catch (error) {
runInAction(() => { runInAction(() => {
this.data = { this.isLabelsLoading = false;
...this.data,
[projectId]: {
labels: [...this.getLabelsByProjectId(projectId)],
isLoading: false,
isRevalidating: false,
error,
},
};
// this.isLabelsLoading = false;
}); });
console.error("Fetching labels error", error); 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) => getLabelChildren = (labelId: string) => this.labels.filter((label) => label.parent === labelId);
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) || [];
/** /**
* For provided query, this function returns all labels that contain query in their name from the labels store. * 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 * @example
* getFilteredLabels("labe") // [{ id: "1", name: "label1", description: "", color: "", parent: null }] * getFilteredLabels("labe") // [{ id: "1", name: "label1", description: "", color: "", parent: null }]
*/ */
getFilteredLabels = (projectId: string | null, query: string): IIssueLabels[] => { getFilteredLabels = (query: string): IIssueLabels[] =>
if (!projectId) return []; this.labels.filter((label) => label.name.includes(query));
return this.data?.[projectId]?.labels.filter((label) => label.name.includes(query)) || [];
};
createLabel = async ( createLabel = async (
workspaceSlug: string, workspaceSlug: string,
@ -151,27 +85,19 @@ class LabelStore {
user user
); );
const _data = this.data?.[projectId] || { const _label = [
labels: [ ...this.labels,
...this.getLabelsByProjectId(projectId),
{ {
id: labelResponse.id, id: labelResponse.id,
name: labelResponse.name, name: labelResponse.name,
description: labelResponse.description, description: labelResponse.description,
color: labelResponse.color, color: labelResponse.color,
parent: labelResponse.parent, parent: labelResponse.parent,
project: labelResponse.project,
}, },
].sort((a, b) => a.name.localeCompare(b.name)), ].sort((a, b) => a.name.localeCompare(b.name));
isLoading: false,
isRevalidating: false,
};
runInAction(() => { runInAction(() => {
this.data = { this.labels = _label;
...this.data,
[projectId]: _data,
};
}); });
return labelResponse; return labelResponse;
} catch (error) { } catch (error) {
@ -196,9 +122,8 @@ class LabelStore {
user user
); );
const _data = this.data?.[projectId] || { const _labels = [...this.labels]
labels: [ .map((label) => {
...this.getLabelsByProjectId(projectId).map((label) => {
if (label.id === labelId) { if (label.id === labelId) {
return { return {
id: labelResponse.id, id: labelResponse.id,
@ -206,21 +131,14 @@ class LabelStore {
description: labelResponse.description, description: labelResponse.description,
color: labelResponse.color, color: labelResponse.color,
parent: labelResponse.parent, parent: labelResponse.parent,
project: labelResponse.project,
}; };
} }
return label; return label;
}), })
], .sort((a, b) => a.name.localeCompare(b.name));
isLoading: false,
isRevalidating: false,
};
runInAction(() => { runInAction(() => {
this.data = { this.labels = _labels;
...this.data,
[projectId]: _data,
};
}); });
} catch (error) { } catch (error) {
console.error("Updating label error", error); console.error("Updating label error", error);
@ -237,17 +155,10 @@ class LabelStore {
try { try {
issueService.deleteIssueLabel(workspaceSlug, projectId, labelId, user); issueService.deleteIssueLabel(workspaceSlug, projectId, labelId, user);
const _data = this.data?.[projectId] || { const _labels = [...this.labels].filter((label) => label.id !== labelId);
labels: [...this.getLabelsByProjectId(projectId)].filter((label) => label.id !== labelId),
isLoading: false,
isRevalidating: false,
};
runInAction(() => { runInAction(() => {
this.data = { this.labels = _labels;
...this.data,
[projectId]: _data,
};
}); });
} catch (error) { } catch (error) {
console.error("Deleting label error", error); console.error("Deleting label error", error);