chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously. closes #1282
This commit is contained in:
74
app/stores/GroupsStore.ts
Normal file
74
app/stores/GroupsStore.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import invariant from "invariant";
|
||||
import { filter } from "lodash";
|
||||
import { action, runInAction, computed } from "mobx";
|
||||
import naturalSort from "@shared/utils/naturalSort";
|
||||
import Group from "~/models/Group";
|
||||
import { PaginationParams } from "~/types";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import BaseStore from "./BaseStore";
|
||||
import RootStore from "./RootStore";
|
||||
|
||||
type FetchPageParams = PaginationParams & { query?: string };
|
||||
|
||||
export default class GroupsStore extends BaseStore<Group> {
|
||||
constructor(rootStore: RootStore) {
|
||||
super(rootStore, Group);
|
||||
}
|
||||
|
||||
@computed
|
||||
get orderedData(): Group[] {
|
||||
return naturalSort(Array.from(this.data.values()), "name");
|
||||
}
|
||||
|
||||
@action
|
||||
fetchPage = async (params: FetchPageParams | undefined): Promise<any> => {
|
||||
this.isFetching = true;
|
||||
|
||||
try {
|
||||
const res = await client.post(`/groups.list`, params);
|
||||
invariant(res && res.data, "Data not available");
|
||||
runInAction(`GroupsStore#fetchPage`, () => {
|
||||
this.addPolicies(res.policies);
|
||||
res.data.groups.forEach(this.add);
|
||||
res.data.groupMemberships.forEach(this.rootStore.groupMemberships.add);
|
||||
this.isLoaded = true;
|
||||
});
|
||||
return res.data.groups;
|
||||
} finally {
|
||||
this.isFetching = false;
|
||||
}
|
||||
};
|
||||
|
||||
inCollection = (collectionId: string, query?: string) => {
|
||||
const memberships = filter(
|
||||
this.rootStore.collectionGroupMemberships.orderedData,
|
||||
(member) => member.collectionId === collectionId
|
||||
);
|
||||
const groupIds = memberships.map((member) => member.groupId);
|
||||
const groups = filter(this.orderedData, (group) =>
|
||||
groupIds.includes(group.id)
|
||||
);
|
||||
if (!query) return groups;
|
||||
return queriedGroups(groups, query);
|
||||
};
|
||||
|
||||
notInCollection = (collectionId: string, query = "") => {
|
||||
const memberships = filter(
|
||||
this.rootStore.collectionGroupMemberships.orderedData,
|
||||
(member) => member.collectionId === collectionId
|
||||
);
|
||||
const groupIds = memberships.map((member) => member.groupId);
|
||||
const groups = filter(
|
||||
this.orderedData,
|
||||
(group) => !groupIds.includes(group.id)
|
||||
);
|
||||
if (!query) return groups;
|
||||
return queriedGroups(groups, query);
|
||||
};
|
||||
}
|
||||
|
||||
function queriedGroups(groups: Group[], query: string) {
|
||||
return filter(groups, (group) =>
|
||||
group.name.toLowerCase().match(query.toLowerCase())
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user