Skip to content

Commit

Permalink
Fix #170: show total number of records (#657)
Browse files Browse the repository at this point in the history
* Fix #170: show total number of records

* Code style fix

* @glasserc review
  • Loading branch information
leplatrem committed Oct 5, 2018
1 parent 3b10ed9 commit 34be6e2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions interfaces/external-modules/kinto-http.d.js
Expand Up @@ -77,6 +77,7 @@ declare module "kinto-http" {
setPermissions(permissions: Permissions): Promise<ObjectResponseBody<Resource>>;
removeAttachment(): Promise<ObjectResponseBody<Resource>>;
listRecords(options: Object): Promise<ListResponseBody<Resource>>;
getTotalRecords(options?: Object): Promise<number>;
addAttachment(attachment: string, record: Object, options?: Options): Promise<ObjectResponseBody<Resource>>;
createRecord(record: Object, options?: Options): Promise<ObjectResponseBody<Resource>>;
updateRecord(record: Object, options?: Options): Promise<ObjectResponseBody<Resource>>;
Expand Down
13 changes: 13 additions & 0 deletions src/actions/collection.js
Expand Up @@ -17,6 +17,7 @@ import {
COLLECTION_RECORDS_REQUEST,
COLLECTION_RECORDS_NEXT_REQUEST,
COLLECTION_RECORDS_SUCCESS,
COLLECTION_TOTAL_RECORDS,
RECORD_CREATE_REQUEST,
RECORD_UPDATE_REQUEST,
RECORD_DELETE_REQUEST,
Expand Down Expand Up @@ -75,6 +76,18 @@ export function listRecordsSuccess(
};
}

export function collectionTotalRecords(
totalRecords: number
): {
type: "COLLECTION_TOTAL_RECORDS",
totalRecords: number,
} {
return {
type: COLLECTION_TOTAL_RECORDS,
totalRecords,
};
}

export function listCollectionHistory(
bid: string,
cid: string,
Expand Down
4 changes: 3 additions & 1 deletion src/components/collection/CollectionRecords.js
Expand Up @@ -391,6 +391,7 @@ export default class CollectionRecords extends PureComponent<Props> {
records,
recordsLoaded,
hasNextRecords,
totalRecords,
} = collection;
const { schema, displayFields } = data;

Expand All @@ -417,7 +418,8 @@ export default class CollectionRecords extends PureComponent<Props> {
bid={bid}
cid={cid}
selected="records"
capabilities={capabilities}>
capabilities={capabilities}
totalRecords={totalRecords}>
{listActions}
<Table
busy={busy}
Expand Down
12 changes: 10 additions & 2 deletions src/components/collection/CollectionTabs.js
Expand Up @@ -12,11 +12,19 @@ type Props = {
selected: "records" | "attributes" | "permissions" | "history",
capabilities: Capabilities,
children?: React.Node,
totalRecords?: ?number,
};

export default class CollectionTabs extends PureComponent<Props> {
render() {
const { bid, cid, selected, capabilities, children } = this.props;
const {
bid,
cid,
selected,
capabilities,
children,
totalRecords,
} = this.props;

return (
<div className="tabs-container">
Expand All @@ -26,7 +34,7 @@ export default class CollectionTabs extends PureComponent<Props> {
className={selected === "records" ? "active" : ""}>
<AdminLink name="collection:records" params={{ bid, cid }}>
<i className="glyphicon glyphicon-align-justify" />
Records
Records {totalRecords ? `(${totalRecords})` : null}
</AdminLink>
</li>
<li
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Expand Up @@ -30,6 +30,7 @@ export const COLLECTION_RECORDS_REQUEST = "COLLECTION_RECORDS_REQUEST";
export const COLLECTION_RECORDS_NEXT_REQUEST =
"COLLECTION_RECORDS_NEXT_REQUEST";
export const COLLECTION_RECORDS_SUCCESS = "COLLECTION_RECORDS_SUCCESS";
export const COLLECTION_TOTAL_RECORDS = "COLLECTION_TOTAL_RECORDS";

export const GROUP_BUSY = "GROUP_BUSY";
export const GROUP_RESET = "GROUP_RESET";
Expand Down
9 changes: 9 additions & 0 deletions src/reducers/collection.js
Expand Up @@ -11,6 +11,7 @@ import {
COLLECTION_HISTORY_REQUEST,
COLLECTION_HISTORY_NEXT_REQUEST,
COLLECTION_HISTORY_SUCCESS,
COLLECTION_TOTAL_RECORDS,
ROUTE_LOAD_REQUEST,
ROUTE_LOAD_SUCCESS,
ROUTE_LOAD_FAILURE,
Expand All @@ -32,6 +33,7 @@ export const INITIAL_STATE: CollectionState = {
recordsLoaded: false,
hasNextRecords: false,
listNextRecords: null,
totalRecords: undefined,
history: paginator(undefined, { type: "@@INIT" }),
};

Expand Down Expand Up @@ -91,6 +93,13 @@ export function collection(
listNextRecords,
};
}
case COLLECTION_TOTAL_RECORDS: {
const { totalRecords } = action;
return {
...state,
totalRecords,
};
}
case COLLECTION_HISTORY_REQUEST:
case COLLECTION_HISTORY_NEXT_REQUEST:
case COLLECTION_HISTORY_SUCCESS: {
Expand Down
4 changes: 4 additions & 0 deletions src/sagas/collection.js
Expand Up @@ -54,7 +54,11 @@ export function* listRecords(
sort: sort || currentSort || defaultSort,
limit: maxPerPage,
});
// Show list of records.
yield put(actions.listRecordsSuccess(data, hasNextPage, next));
// Request number of total records.
const totalRecords = yield call([coll, coll.getTotalRecords]);
yield put(actions.collectionTotalRecords(totalRecords));
} catch (error) {
yield put(notifyError("Couldn't list records.", error));
}
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Expand Up @@ -99,6 +99,7 @@ export type CollectionState = {
recordsLoaded: boolean,
hasNextRecords: boolean,
listNextRecords: ?Function,
totalRecords: ?number,
history: Paginator<ResourceHistoryEntry>,
};

Expand Down
31 changes: 31 additions & 0 deletions test/components/CollectionRecords_test.js
Expand Up @@ -129,6 +129,37 @@ describe("CollectionRecords component", () => {
});
});

describe("Tabs", () => {
let node;

const collection = {
busy: false,
data: {},
permissions: {},
recordsLoaded: true,
records: [],
totalRecords: 18,
};

beforeEach(() => {
node = createComponent(CollectionRecords, {
match: { params: { bid: "bucket", cid: "collection" } },
session: {},
pluginHooks: {},
bucket,
collection,
capabilities,
listRecords: () => {},
});
});

it("should show the total number of records", () => {
expect(
node.querySelector(".tabs-container li.active").textContent
).to.eql("Records (18)");
});
});

describe("List actions", () => {
const collection = {
busy: false,
Expand Down

0 comments on commit 34be6e2

Please sign in to comment.