mirror of
https://github.com/usatiuk/writer.git
synced 2025-10-28 16:07:49 +01:00
sort recent docs client-side
This commit is contained in:
@@ -12,7 +12,6 @@ import { IAppState } from "~redux/reducers";
|
||||
import { DocsList } from "./DocsList";
|
||||
|
||||
export interface IOverviewComponentProps {
|
||||
recent: IDocumentJSON[] | null;
|
||||
all: IDocumentJSON[] | null;
|
||||
fetching: boolean;
|
||||
spinner: boolean;
|
||||
@@ -36,11 +35,14 @@ export class OverviewComponent extends React.PureComponent<
|
||||
|
||||
public render() {
|
||||
if (this.props.all) {
|
||||
const recent = [...this.props.all];
|
||||
recent.sort((a, b) => b.editedAt - a.editedAt);
|
||||
const recentCut = recent.splice(0, 4);
|
||||
return (
|
||||
<div id="overview">
|
||||
<div className="section">
|
||||
<H3>Recent</H3>
|
||||
<DocsList docs={this.props.recent} />
|
||||
<DocsList docs={recentCut} />
|
||||
</div>
|
||||
<span className="separator" />
|
||||
<div className="section">
|
||||
@@ -57,7 +59,6 @@ export class OverviewComponent extends React.PureComponent<
|
||||
|
||||
function mapStateToProps(state: IAppState) {
|
||||
return {
|
||||
recent: state.docs.recent,
|
||||
all: state.docs.all,
|
||||
fetching: state.docs.fetching,
|
||||
spinner: state.docs.spinner,
|
||||
|
||||
@@ -26,7 +26,6 @@ export interface IDocsFetchFailAction extends Action {
|
||||
export interface IDocsFetchSuccessAction extends Action {
|
||||
type: DocsTypes.DOCS_FETCH_SUCCESS;
|
||||
payload: {
|
||||
recent: IDocumentJSON[];
|
||||
all: IDocumentJSON[];
|
||||
};
|
||||
}
|
||||
@@ -44,10 +43,9 @@ export function fetchDocsFail(error: string): IDocsFetchFailAction {
|
||||
}
|
||||
|
||||
export function fetchDocsSuccess(
|
||||
recent: IDocumentJSON[],
|
||||
all: IDocumentJSON[],
|
||||
): IDocsFetchSuccessAction {
|
||||
return { type: DocsTypes.DOCS_FETCH_SUCCESS, payload: { recent, all } };
|
||||
return { type: DocsTypes.DOCS_FETCH_SUCCESS, payload: { all } };
|
||||
}
|
||||
|
||||
export type DocsAction =
|
||||
|
||||
@@ -4,7 +4,6 @@ import { IDocumentJSON } from "~../../src/entity/Document";
|
||||
import { DocsAction, DocsTypes } from "./actions";
|
||||
|
||||
export interface IDocsState {
|
||||
recent: IDocumentJSON[] | null;
|
||||
all: IDocumentJSON[] | null;
|
||||
fetching: boolean;
|
||||
error: string | null;
|
||||
@@ -12,7 +11,6 @@ export interface IDocsState {
|
||||
}
|
||||
|
||||
const defaultDocsState: IDocsState = {
|
||||
recent: null,
|
||||
all: null,
|
||||
fetching: false,
|
||||
error: null,
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
race,
|
||||
takeLatest,
|
||||
} from "redux-saga/effects";
|
||||
import { fetchAllDocs, fetchRecentDocs } from "~redux/api/docs";
|
||||
import { fetchAllDocs } from "~redux/api/docs";
|
||||
|
||||
import {
|
||||
DocsTypes,
|
||||
@@ -28,7 +28,7 @@ function* docsFetchStart(action: IDocsFetchStartAction) {
|
||||
const spinner = yield fork(startSpinner);
|
||||
|
||||
const { response, timeout } = yield race({
|
||||
response: all([call(fetchRecentDocs), call(fetchAllDocs)]),
|
||||
response: call(fetchAllDocs),
|
||||
timeout: call(delay, 10000),
|
||||
});
|
||||
|
||||
@@ -40,12 +40,11 @@ function* docsFetchStart(action: IDocsFetchStartAction) {
|
||||
}
|
||||
|
||||
if (response) {
|
||||
if (response[0].data == null || response[1].data == null) {
|
||||
yield put(fetchDocsFail(response[0].error));
|
||||
if (response.data == null) {
|
||||
yield put(fetchDocsFail(response.error));
|
||||
} else {
|
||||
const recentDocs = response[0].data;
|
||||
const allDocs = response[1].data;
|
||||
yield put(fetchDocsSuccess(recentDocs, allDocs));
|
||||
const allDocs = response.data;
|
||||
yield put(fetchDocsSuccess(allDocs));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -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 => {
|
||||
if (!ctx.state.user) {
|
||||
ctx.throw(401);
|
||||
|
||||
@@ -93,38 +93,6 @@ describe("docs", () => {
|
||||
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 () => {
|
||||
const response = await request(callback)
|
||||
.get(`/docs/byID/${seed.doc1.id}`)
|
||||
|
||||
Reference in New Issue
Block a user