Duplicative method cleanup (#6431)

This commit is contained in:
Tom Moor
2024-01-25 20:02:17 -08:00
committed by GitHub
parent cab9a1ec96
commit e62c734c41
13 changed files with 87 additions and 122 deletions

View File

@@ -71,32 +71,9 @@ export default class CollectionGroupMembershipsStore extends Store<CollectionGro
id: collectionId,
groupId,
});
const membership = Array.from(this.data.values()).find(
(m) => m.groupId === groupId && m.collectionId === collectionId
);
if (membership) {
this.remove(membership.id);
}
}
@action
removeCollectionMemberships = (collectionId: string) => {
this.data.forEach((membership, key) => {
if (membership.collectionId === collectionId) {
this.remove(key);
}
this.removeAll({
collectionId,
groupId,
});
};
/**
* Find a collection group membership by collectionId and groupId
*
* @param collectionId The collection ID
* @param groupId The group ID
* @returns The collection group membership or undefined if not found.
*/
find = (collectionId: string, groupId: string) =>
Array.from(this.data.values()).find(
(m) => m.groupId === groupId && m.collectionId === collectionId
);
}
}

View File

@@ -1,4 +1,3 @@
import filter from "lodash/filter";
import orderBy from "lodash/orderBy";
import { action, computed } from "mobx";
import Comment from "~/models/Comment";
@@ -18,8 +17,9 @@ export default class CommentsStore extends Store<Comment> {
* @returns Array of comments
*/
threadsInDocument(documentId: string): Comment[] {
return this.inDocument(documentId).filter(
(comment) => !comment.parentCommentId
return this.filter(
(comment: Comment) =>
comment.documentId === documentId && !comment.parentCommentId
);
}
@@ -30,26 +30,12 @@ export default class CommentsStore extends Store<Comment> {
* @returns Array of comments
*/
inThread(threadId: string): Comment[] {
return filter(
this.orderedData,
(comment) =>
return this.filter(
(comment: Comment) =>
comment.parentCommentId === threadId || comment.id === threadId
);
}
/**
* Returns a list of comments in a document.
*
* @param documentId ID of the document to get comments for
* @returns Array of comments
*/
inDocument(documentId: string): Comment[] {
return filter(
this.orderedData,
(comment) => comment.documentId === documentId
);
}
@action
setTyping({
commentId,

View File

@@ -1,5 +1,4 @@
import filter from "lodash/filter";
import sortBy from "lodash/sortBy";
import orderBy from "lodash/orderBy";
import { computed } from "mobx";
import Event from "~/models/Event";
import RootStore from "./RootStore";
@@ -14,10 +13,6 @@ export default class EventsStore extends Store<Event> {
@computed
get orderedData(): Event[] {
return sortBy(Array.from(this.data.values()), "createdAt").reverse();
}
inDocument(documentId: string): Event[] {
return filter(this.orderedData, (event) => event.documentId === documentId);
return orderBy(Array.from(this.data.values()), "createdAt", "asc");
}
}

View File

@@ -1,6 +1,4 @@
import filter from "lodash/filter";
import { computed } from "mobx";
import { IntegrationService } from "@shared/types";
import naturalSort from "@shared/utils/naturalSort";
import RootStore from "~/stores/RootStore";
import Store from "~/stores/base/Store";
@@ -15,13 +13,6 @@ class IntegrationsStore extends Store<Integration> {
get orderedData(): Integration[] {
return naturalSort(Array.from(this.data.values()), "name");
}
@computed
get slackIntegrations(): Integration[] {
return filter(this.orderedData, {
service: IntegrationService.Slack,
});
}
}
export default IntegrationsStore;

View File

@@ -71,7 +71,7 @@ export default class MembershipsStore extends Store<Membership> {
id: collectionId,
userId,
});
this.revoke({ userId, collectionId });
this.removeAll({ userId, collectionId });
}
@action
@@ -82,30 +82,4 @@ export default class MembershipsStore extends Store<Membership> {
}
});
};
@action
revoke = ({
userId,
collectionId,
}: {
collectionId: string;
userId: string;
}) => {
const membership = this.find(collectionId, userId);
if (membership) {
this.remove(membership.id);
}
};
/**
* Find a collection user membership by collectionId and userId
*
* @param collectionId The collection ID
* @param userId The user ID
* @returns The collection user membership or undefined if not found.
*/
find = (collectionId: string, userId: string) =>
Array.from(this.data.values()).find(
(m) => m.userId === userId && m.collectionId === collectionId
);
}

View File

@@ -2,7 +2,7 @@ import invariant from "invariant";
import filter from "lodash/filter";
import find from "lodash/find";
import isUndefined from "lodash/isUndefined";
import sortBy from "lodash/sortBy";
import orderBy from "lodash/orderBy";
import { action, computed } from "mobx";
import type { Required } from "utility-types";
import type { JSONObject } from "@shared/types";
@@ -26,7 +26,7 @@ export default class SharesStore extends Store<Share> {
@computed
get orderedData(): Share[] {
return sortBy(Array.from(this.data.values()), "createdAt").reverse();
return orderBy(Array.from(this.data.values()), "createdAt", "asc");
}
@computed

View File

@@ -8,6 +8,15 @@ import { client } from "~/utils/ApiClient";
import RootStore from "./RootStore";
import Store, { RPCAction } from "./base/Store";
type UserCounts = {
active: number;
admins: number;
all: number;
invited: number;
suspended: number;
viewers: number;
};
export default class UsersStore extends Store<User> {
actions = [
RPCAction.Info,
@@ -19,14 +28,7 @@ export default class UsersStore extends Store<User> {
];
@observable
counts: {
active: number;
admins: number;
all: number;
invited: number;
suspended: number;
viewers: number;
} = {
counts: UserCounts = {
active: 0,
admins: 0,
all: 0,
@@ -159,8 +161,12 @@ export default class UsersStore extends Store<User> {
});
@action
fetchCounts = async (teamId: string): Promise<any> => {
const res = await client.post(`/users.count`, {
fetchCounts = async (
teamId: string
): Promise<{
counts: UserCounts;
}> => {
const res = await client.post(`/≈`, {
teamId,
});
invariant(res?.data, "Data should be available");

View File

@@ -1,4 +1,7 @@
import invariant from "invariant";
import type { ObjectIterateeCustom } from "lodash";
import filter from "lodash/filter";
import find from "lodash/find";
import flatten from "lodash/flatten";
import lowerFirst from "lodash/lowerFirst";
import orderBy from "lodash/orderBy";
@@ -72,9 +75,7 @@ export default abstract class Store<T extends Model> {
}
addPolicies = (policies: Policy[]) => {
if (policies) {
policies.forEach((policy) => this.rootStore.policies.add(policy));
}
policies?.forEach((policy) => this.rootStore.policies.add(policy));
};
@action
@@ -130,6 +131,15 @@ export default abstract class Store<T extends Model> {
this.data.delete(id);
}
/**
* Remove all items in the store that match the predicate.
*
* @param predicate A function that returns true if the item matches, or an object with the properties to match.
*/
removeAll = (predicate: Parameters<typeof this.filter>[0]) => {
this.filter(predicate).forEach((item) => this.remove(item.id));
};
save(params: Properties<T>, options: JSONObject = {}): Promise<T> {
const { isNew, ...rest } = options;
if (isNew || !("id" in params) || !params.id) {
@@ -138,6 +148,11 @@ export default abstract class Store<T extends Model> {
return this.update(params, rest);
}
/**
* Get a single item from the store that matches the ID.
*
* @param id The ID of the item to get.
*/
get(id: string): T | undefined {
return this.data.get(id);
}
@@ -291,4 +306,22 @@ export default abstract class Store<T extends Model> {
get orderedData(): T[] {
return orderBy(Array.from(this.data.values()), "createdAt", "desc");
}
/**
* Find an item in the store matching the given predicate.
*
* @param predicate A function that returns true if the item matches, or an object with the properties to match.
*/
find = (predicate: ObjectIterateeCustom<T, boolean>): T | undefined =>
// @ts-expect-error not sure why T is incompatible
find(this.orderedData, predicate);
/**
* Filter items in the store matching the given predicate.
*
* @param predicate A function that returns true if the item matches, or an object with the properties to match.
*/
filter = (predicate: ObjectIterateeCustom<T, boolean>): T[] =>
// @ts-expect-error not sure why T is incompatible
filter(this.orderedData, predicate);
}