dev: label state management setup

This commit is contained in:
Dakshesh Jain 2023-08-10 17:51:47 +05:30
parent f562fcd466
commit 380ad340a6
3 changed files with 114 additions and 0 deletions

102
apps/app/store/labels.ts Normal file
View File

@ -0,0 +1,102 @@
// mobx
import { action, observable, runInAction, makeObservable } from "mobx";
// services
import issueService from "services/issues.service";
// types
import type { IIssueLabels, LabelForm, ICurrentUserResponse } from "types";
class LabelStore {
labels: IIssueLabels[] = [];
rootStore: any | null = null;
constructor(_rootStore: any | null = null) {
makeObservable(this, {
labels: observable.ref,
loadLabels: action,
createLabel: action,
updateLabel: action,
deleteLabel: action,
});
this.rootStore = _rootStore;
}
loadLabels = async (workspaceSlug: string, projectId: string) => {
try {
const labelsResponse: IIssueLabels[] = await issueService.getIssueLabels(
workspaceSlug,
projectId
);
runInAction(() => {
this.labels = labelsResponse;
});
} catch (error) {
console.error("Fetching labels error", error);
}
};
createLabel = async (
workspaceSlug: string,
projectId: string,
labelForm: LabelForm,
user: ICurrentUserResponse
) => {
try {
const labelResponse: IIssueLabels = await issueService.createIssueLabel(
workspaceSlug,
projectId,
labelForm,
user
);
runInAction(() => {
this.labels.push(labelResponse);
});
} catch (error) {
console.error("Creating label error", error);
}
};
updateLabel = async (
workspaceSlug: string,
projectId: string,
labelId: string,
labelForm: LabelForm,
user: ICurrentUserResponse
) => {
try {
const labelResponse: IIssueLabels = await issueService.patchIssueLabel(
workspaceSlug,
projectId,
labelId,
labelForm,
user
);
runInAction(() => {
const labelIndex = this.labels.findIndex((label) => label.id === labelId);
this.labels[labelIndex] = labelResponse;
});
} catch (error) {
console.error("Updating label error", error);
}
};
deleteLabel = async (
workspaceSlug: string,
projectId: string,
labelId: string,
user: ICurrentUserResponse
) => {
try {
await issueService.deleteIssueLabel(workspaceSlug, projectId, labelId, user);
runInAction(() => {
this.labels = this.labels.filter((label) => label.id !== labelId);
});
} catch (error) {
console.error("Deleting label error", error);
}
};
}
export default LabelStore;

View File

@ -3,15 +3,18 @@ import { enableStaticRendering } from "mobx-react-lite";
// store imports // store imports
import UserStore from "./user"; import UserStore from "./user";
import ThemeStore from "./theme"; import ThemeStore from "./theme";
import LabelStore from "./labels";
enableStaticRendering(typeof window === "undefined"); enableStaticRendering(typeof window === "undefined");
export class RootStore { export class RootStore {
user; user;
theme; theme;
labels;
constructor() { constructor() {
this.user = new UserStore(this); this.user = new UserStore(this);
this.theme = new ThemeStore(this); this.theme = new ThemeStore(this);
this.labels = new LabelStore(this);
} }
} }

View File

@ -164,6 +164,15 @@ export interface IIssueLabels {
parent: string | null; parent: string | null;
} }
export interface LabelForm {
name: string;
description: string;
color: string;
project: string;
workspace: string;
parent: string | null;
}
export interface IIssueActivity { export interface IIssueActivity {
actor: string; actor: string;
actor_detail: IUserLite; actor_detail: IUserLite;