feat: Add reordering to starred documents (#2953)

* draft

* reordering

* JIT Index stars on first load

* test

* Remove unused code on client

* small unrefactor
This commit is contained in:
Tom Moor
2022-01-21 18:11:50 -08:00
committed by GitHub
parent 49533d7a3f
commit 79e2cad5b9
32 changed files with 931 additions and 132 deletions

44
app/stores/StarsStore.ts Normal file
View File

@@ -0,0 +1,44 @@
import invariant from "invariant";
import { action, runInAction, computed } from "mobx";
import Star from "~/models/Star";
import { PaginationParams } from "~/types";
import { client } from "~/utils/ApiClient";
import BaseStore from "./BaseStore";
import RootStore from "./RootStore";
export default class StarsStore extends BaseStore<Star> {
constructor(rootStore: RootStore) {
super(rootStore, Star);
}
@action
fetchPage = async (params?: PaginationParams | undefined): Promise<void> => {
this.isFetching = true;
try {
const res = await client.post(`/stars.list`, params);
invariant(res && res.data, "Data not available");
runInAction(`StarsStore#fetchPage`, () => {
res.data.documents.forEach(this.rootStore.documents.add);
res.data.stars.forEach(this.add);
this.addPolicies(res.policies);
this.isLoaded = true;
});
} finally {
this.isFetching = false;
}
};
@computed
get orderedData(): Star[] {
const stars = Array.from(this.data.values());
return stars.sort((a, b) => {
if (a.index === b.index) {
return a.updatedAt > b.updatedAt ? -1 : 1;
}
return a.index < b.index ? -1 : 1;
});
}
}