mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
* refactor: DRY up app stores with fetchPaginated helper and IndexedStore Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * refactor: Resolve related models by store responseKey instead of a key map Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
import invariant from "invariant";
|
|
import { action, makeObservable, override } from "mobx";
|
|
import type { CollectionPermission } from "@shared/types";
|
|
import Membership from "~/models/Membership";
|
|
import type { PaginationParams } from "~/types";
|
|
import { client } from "~/utils/ApiClient";
|
|
import type RootStore from "./RootStore";
|
|
import Store, { type PaginatedResponse, RPCAction } from "./base/Store";
|
|
|
|
export default class MembershipsStore extends Store<Membership> {
|
|
actions = [RPCAction.Create, RPCAction.Delete];
|
|
|
|
constructor(rootStore: RootStore) {
|
|
super(rootStore, Membership);
|
|
makeObservable(this);
|
|
}
|
|
|
|
/**
|
|
* Remove a membership, and the access that it granted.
|
|
*
|
|
* @param id the ID of the membership to remove.
|
|
*/
|
|
@override
|
|
remove(id: string, options?: { permanent?: boolean }): void {
|
|
super.remove(id, options);
|
|
this.rootStore.policies.removeForMembership(id);
|
|
}
|
|
|
|
fetchPage = async (
|
|
params: (PaginationParams & { id?: string }) | undefined
|
|
): Promise<PaginatedResponse<Membership>> =>
|
|
this.fetchPaginated("/collections.memberships", params, [
|
|
this.rootStore.users,
|
|
]);
|
|
|
|
@override
|
|
async create({
|
|
collectionId,
|
|
userId,
|
|
permission,
|
|
}: {
|
|
collectionId: string;
|
|
userId: string;
|
|
permission?: CollectionPermission;
|
|
}) {
|
|
const res = await client.post("/collections.add_user", {
|
|
id: collectionId,
|
|
userId,
|
|
permission,
|
|
});
|
|
invariant(res?.data, "Membership data should be available");
|
|
res.data.users.forEach(this.rootStore.users.add);
|
|
|
|
const memberships = res.data.memberships.map(this.add);
|
|
return memberships[0];
|
|
}
|
|
|
|
@override
|
|
async delete({
|
|
collectionId,
|
|
userId,
|
|
}: {
|
|
collectionId: string;
|
|
userId: string;
|
|
}) {
|
|
await client.post("/collections.remove_user", {
|
|
id: collectionId,
|
|
userId,
|
|
});
|
|
this.removeAll({ userId, collectionId });
|
|
}
|
|
|
|
@action
|
|
removeCollectionMemberships = (collectionId: string) => {
|
|
this.data.forEach((membership, key) => {
|
|
if (membership.collectionId === collectionId) {
|
|
this.remove(key);
|
|
}
|
|
});
|
|
};
|
|
|
|
inCollection = (collectionId: string) =>
|
|
this.orderedData.filter(
|
|
(membership) => membership.collectionId === collectionId
|
|
);
|
|
}
|