-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathlabel.store.ts
More file actions
71 lines (62 loc) · 1.9 KB
/
label.store.ts
File metadata and controls
71 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Copyright (c) 2023-present Plane Software, Inc. and contributors
* SPDX-License-Identifier: AGPL-3.0-only
* See the LICENSE file for details.
*/
import { set } from "lodash-es";
import { action, computed, makeObservable, observable, runInAction } from "mobx";
// plane imports
import { SitesLabelService } from "@plane/services";
import type { IIssueLabel } from "@plane/types";
// store
import type { RootStore } from "./root.store";
export interface IIssueLabelStore {
// observables
labels: IIssueLabel[] | undefined;
// computed actions
getLabelById: (labelId: string | undefined) => IIssueLabel | undefined;
getLabelsByIds: (labelIds: string[]) => IIssueLabel[];
// fetch actions
fetchLabels: (anchor: string) => Promise<IIssueLabel[]>;
}
export class LabelStore implements IIssueLabelStore {
labelMap: Record<string, IIssueLabel> = {};
labelService: SitesLabelService;
rootStore: RootStore;
constructor(_rootStore: RootStore) {
makeObservable(this, {
// observables
labelMap: observable,
// computed
labels: computed,
// fetch action
fetchLabels: action,
});
this.labelService = new SitesLabelService();
this.rootStore = _rootStore;
}
get labels() {
return Object.values(this.labelMap);
}
getLabelById = (labelId: string | undefined) => (labelId ? this.labelMap[labelId] : undefined);
getLabelsByIds = (labelIds: string[]) => {
const currLabels = [];
for (const labelId of labelIds) {
const label = this.getLabelById(labelId);
if (label) {
currLabels.push(label);
}
}
return currLabels;
};
fetchLabels = async (anchor: string) => {
const labelsResponse = await this.labelService.list(anchor);
runInAction(() => {
this.labelMap = {};
for (const label of labelsResponse) {
set(this.labelMap, [label.id], label);
}
});
return labelsResponse;
};
}