Handle users.demote event (#6315)
* fix: Handle users.demote event * fix: fetchAll * fix: fetch based on total
This commit is contained in:
@@ -18,6 +18,7 @@ import Pin from "~/models/Pin";
|
||||
import Star from "~/models/Star";
|
||||
import Subscription from "~/models/Subscription";
|
||||
import Team from "~/models/Team";
|
||||
import User from "~/models/User";
|
||||
import withStores from "~/components/withStores";
|
||||
import {
|
||||
PartialWithId,
|
||||
@@ -460,6 +461,13 @@ class WebsocketProvider extends React.Component<Props> {
|
||||
}
|
||||
);
|
||||
|
||||
this.socket.on("users.demote", async (event: PartialWithId<User>) => {
|
||||
if (auth.user && event.id === auth.user.id) {
|
||||
documents.all.forEach((document) => policies.remove(document.id));
|
||||
await collections.fetchAll();
|
||||
}
|
||||
});
|
||||
|
||||
// received a message from the API server that we should request
|
||||
// to join a specific room. Forward that to the ws server.
|
||||
this.socket.on("join", (event) => {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import invariant from "invariant";
|
||||
import flatten from "lodash/flatten";
|
||||
import lowerFirst from "lodash/lowerFirst";
|
||||
import orderBy from "lodash/orderBy";
|
||||
import { observable, action, computed, runInAction } from "mobx";
|
||||
import pluralize from "pluralize";
|
||||
import { Pagination } from "@shared/constants";
|
||||
import RootStore from "~/stores/RootStore";
|
||||
import Policy from "~/models/Policy";
|
||||
import Model from "~/models/base/Model";
|
||||
@@ -268,6 +270,20 @@ export default abstract class Store<T extends Model> {
|
||||
}
|
||||
};
|
||||
|
||||
@action
|
||||
fetchAll = async (): Promise<T[]> => {
|
||||
const limit = Pagination.defaultLimit;
|
||||
const response = await this.fetchPage({ limit });
|
||||
const pages = Math.ceil(response[PAGINATION_SYMBOL].total / limit);
|
||||
const fetchPages = [];
|
||||
for (let page = 1; page < pages; page++) {
|
||||
fetchPages.push(this.fetchPage({ offset: page * limit, limit }));
|
||||
}
|
||||
|
||||
const results = await Promise.all(fetchPages);
|
||||
return flatten(results);
|
||||
};
|
||||
|
||||
@computed
|
||||
get orderedData(): T[] {
|
||||
return orderBy(Array.from(this.data.values()), "createdAt", "desc");
|
||||
|
||||
Reference in New Issue
Block a user