sort recent docs client-side

This commit is contained in:
2019-02-08 13:16:14 +03:00
parent 803a31ee2d
commit 3c8dbc14aa
6 changed files with 11 additions and 65 deletions

View File

@@ -12,7 +12,6 @@ import { IAppState } from "~redux/reducers";
import { DocsList } from "./DocsList"; import { DocsList } from "./DocsList";
export interface IOverviewComponentProps { export interface IOverviewComponentProps {
recent: IDocumentJSON[] | null;
all: IDocumentJSON[] | null; all: IDocumentJSON[] | null;
fetching: boolean; fetching: boolean;
spinner: boolean; spinner: boolean;
@@ -36,11 +35,14 @@ export class OverviewComponent extends React.PureComponent<
public render() { public render() {
if (this.props.all) { if (this.props.all) {
const recent = [...this.props.all];
recent.sort((a, b) => b.editedAt - a.editedAt);
const recentCut = recent.splice(0, 4);
return ( return (
<div id="overview"> <div id="overview">
<div className="section"> <div className="section">
<H3>Recent</H3> <H3>Recent</H3>
<DocsList docs={this.props.recent} /> <DocsList docs={recentCut} />
</div> </div>
<span className="separator" /> <span className="separator" />
<div className="section"> <div className="section">
@@ -57,7 +59,6 @@ export class OverviewComponent extends React.PureComponent<
function mapStateToProps(state: IAppState) { function mapStateToProps(state: IAppState) {
return { return {
recent: state.docs.recent,
all: state.docs.all, all: state.docs.all,
fetching: state.docs.fetching, fetching: state.docs.fetching,
spinner: state.docs.spinner, spinner: state.docs.spinner,

View File

@@ -26,7 +26,6 @@ export interface IDocsFetchFailAction extends Action {
export interface IDocsFetchSuccessAction extends Action { export interface IDocsFetchSuccessAction extends Action {
type: DocsTypes.DOCS_FETCH_SUCCESS; type: DocsTypes.DOCS_FETCH_SUCCESS;
payload: { payload: {
recent: IDocumentJSON[];
all: IDocumentJSON[]; all: IDocumentJSON[];
}; };
} }
@@ -44,10 +43,9 @@ export function fetchDocsFail(error: string): IDocsFetchFailAction {
} }
export function fetchDocsSuccess( export function fetchDocsSuccess(
recent: IDocumentJSON[],
all: IDocumentJSON[], all: IDocumentJSON[],
): IDocsFetchSuccessAction { ): IDocsFetchSuccessAction {
return { type: DocsTypes.DOCS_FETCH_SUCCESS, payload: { recent, all } }; return { type: DocsTypes.DOCS_FETCH_SUCCESS, payload: { all } };
} }
export type DocsAction = export type DocsAction =

View File

@@ -4,7 +4,6 @@ import { IDocumentJSON } from "~../../src/entity/Document";
import { DocsAction, DocsTypes } from "./actions"; import { DocsAction, DocsTypes } from "./actions";
export interface IDocsState { export interface IDocsState {
recent: IDocumentJSON[] | null;
all: IDocumentJSON[] | null; all: IDocumentJSON[] | null;
fetching: boolean; fetching: boolean;
error: string | null; error: string | null;
@@ -12,7 +11,6 @@ export interface IDocsState {
} }
const defaultDocsState: IDocsState = { const defaultDocsState: IDocsState = {
recent: null,
all: null, all: null,
fetching: false, fetching: false,
error: null, error: null,

View File

@@ -8,7 +8,7 @@ import {
race, race,
takeLatest, takeLatest,
} from "redux-saga/effects"; } from "redux-saga/effects";
import { fetchAllDocs, fetchRecentDocs } from "~redux/api/docs"; import { fetchAllDocs } from "~redux/api/docs";
import { import {
DocsTypes, DocsTypes,
@@ -28,7 +28,7 @@ function* docsFetchStart(action: IDocsFetchStartAction) {
const spinner = yield fork(startSpinner); const spinner = yield fork(startSpinner);
const { response, timeout } = yield race({ const { response, timeout } = yield race({
response: all([call(fetchRecentDocs), call(fetchAllDocs)]), response: call(fetchAllDocs),
timeout: call(delay, 10000), timeout: call(delay, 10000),
}); });
@@ -40,12 +40,11 @@ function* docsFetchStart(action: IDocsFetchStartAction) {
} }
if (response) { if (response) {
if (response[0].data == null || response[1].data == null) { if (response.data == null) {
yield put(fetchDocsFail(response[0].error)); yield put(fetchDocsFail(response.error));
} else { } else {
const recentDocs = response[0].data; const allDocs = response.data;
const allDocs = response[1].data; yield put(fetchDocsSuccess(allDocs));
yield put(fetchDocsSuccess(recentDocs, allDocs));
} }
} }
} catch (e) { } catch (e) {

View File

@@ -94,24 +94,6 @@ docsRouter.get("/docs/list", async ctx => {
}; };
}); });
docsRouter.get("/docs/list/recent", async ctx => {
if (!ctx.state.user) {
ctx.throw(401);
}
const { user } = ctx.state;
const documents = await Document.find({
where: { user: user.id },
order: { editedAt: "DESC" },
});
ctx.body = {
error: false,
data: documents.map(document => document.toJSON(user.id)),
};
});
docsRouter.get("/docs/byID/:id", async ctx => { docsRouter.get("/docs/byID/:id", async ctx => {
if (!ctx.state.user) { if (!ctx.state.user) {
ctx.throw(401); ctx.throw(401);

View File

@@ -93,38 +93,6 @@ describe("docs", () => {
expect(documents).to.deep.equal(userDocs); expect(documents).to.deep.equal(userDocs);
}); });
it("should list recent docs", async () => {
const doc1 = new Document(seed.user1, "doc1", "");
doc1.editedAt = new Date(doc1.editedAt.getTime() + 10000);
await doc1.save();
const doc2 = new Document(seed.user1, "doc2", "");
doc2.editedAt = new Date(doc2.editedAt.getTime() + 20000);
await doc2.save();
const doc3 = new Document(seed.user1, "doc3", "");
doc3.editedAt = new Date(doc3.editedAt.getTime() + 30000);
await doc3.save();
const response = await request(callback)
.get("/docs/list/recent")
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
})
.expect(200);
expect(response.body.error).to.be.false;
const documents = response.body.data as IDocumentJSON[];
const userDocs = [
doc3.toJSON(seed.user1.id),
doc2.toJSON(seed.user1.id),
doc1.toJSON(seed.user1.id),
seed.doc1.toJSON(seed.user1.id),
];
expect(documents).to.deep.equal(userDocs);
});
it("should get a document", async () => { it("should get a document", async () => {
const response = await request(callback) const response = await request(callback)
.get(`/docs/byID/${seed.doc1.id}`) .get(`/docs/byID/${seed.doc1.id}`)