Skip to content

Commit

Permalink
feat: add exact number of results as row param
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanoshadjipetrou committed Oct 30, 2020
1 parent a526b74 commit 79473c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/app/components/datadisplay/DataTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-nested-ternary */
// @ts-nocheck
import React, { useState, useEffect } from 'react';
import Paper from '@material-ui/core/Paper';
Expand Down Expand Up @@ -34,7 +35,6 @@ export const DataTable = (props) => {
const [pageSize, setPageSize] = useState(10);
const [tablePage, setTablePage] = useState(0);
const [loading, setLoading] = useState(false);
const [allDataCount, setAllDataCount] = useState([]);

useEffect(() => {
if (!props.defaultCols && docsData.length > 0) {
Expand All @@ -57,7 +57,8 @@ export const DataTable = (props) => {
)
.then((response) => {
setDocsData(get(response, 'data.response.docs', []));
setAllDataCount(get(response, 'data.response.numFound', 0));
props.setAllDataCount(get(response, 'data.response.numFound', 0));
a;
setLoading(false);
})
.catch((error) => {
Expand Down Expand Up @@ -85,8 +86,8 @@ export const DataTable = (props) => {
`}
>
<h3>
Datastore retrieved {allDataCount}{' '}
{allDataCount === 1 ? 'activity' : 'activities'} for you
Datastore retrieved {props.allDataCount}{' '}
{props.allDataCount === 1 ? 'activity' : 'activities'} for you
</h3>
<Paper>
<Grid rows={docsData} columns={cols}>
Expand All @@ -96,7 +97,7 @@ export const DataTable = (props) => {
onPageSizeChange={setPageSize}
onCurrentPageChange={setTablePage}
/>
<CustomPaging totalCount={allDataCount} />
<CustomPaging totalCount={props.allDataCount} />
<Table
noDataCellComponent={() => (
<NoDataCellComponent
Expand Down
47 changes: 31 additions & 16 deletions src/app/modules/querybuilder-module/fragments/results/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const DownloadFragment = () => {
/* get query url from app store */
const store = ModuleStore.useStore();

const [rows, setRows] = useState(50);
const [allDataCount, setAllDataCount] = useState(0);
const queryURL = useStoreState((state) => state.query.url);
const [queryState, setQueryState] = useState(queryURL);
const rowFormat = store.get('rowFormat');
Expand All @@ -70,18 +72,27 @@ export const DownloadFragment = () => {
'csv&fl=*,reporting_org_narrative:[value v=""],sector:[value v=""]';
}

let csvUrl = queryState.includes('fl=')
? queryState.replace('json', 'csv')
: queryState.replace('json', `xslt&tr=${rowFormat}-csv.xsl`);
let csvUrl = queryState.replace('json', 'csv');

if (repeatRows !== '0') {
csvUrl = csvUrl.replace('csv', `xslt&tr=${rowFormat}-csv.xsl`);
csvUrl = csvUrl.replace(`/${rowFormat}`, `/${rowFormat}-${repeatRows}`);
csvUrl = csvUrl.replace(
`tr=${rowFormat}-csv.xsl`,
`tr=${rowFormat}-${repeatRows}-csv.xsl`
);
}

React.useEffect(() => {
if (allDataCount > 50 || allDataCount === 0) {
setRows(50);
setQueryState(queryState.replace(`rows=${allDataCount}`, 'rows=50'));
} else {
setRows(allDataCount);
setQueryState(queryState.replace('rows=50', `rows=${allDataCount}`));
}
}, [allDataCount]);

return (
<Grid
container
Expand All @@ -94,6 +105,8 @@ export const DownloadFragment = () => {
<Grid item lg={12}>
<DataTable
url={queryURL}
allDataCount={allDataCount}
setAllDataCount={setAllDataCount}
rowFormat={store.get('rowFormat')}
defaultCols={store.get('fields').length === 0}
/>
Expand All @@ -108,19 +121,19 @@ export const DownloadFragment = () => {
<Grid item xs={12} sm={12} md={12}>
<RadioGroupTitle title="Choose sample size" />
<RadioGroup
defaultValue="50"
value={rows.toString()}
onChange={(e) => {
switch (e.target.value) {
case '50':
document.cookie = 'rows=rows=50';
setRows(50);
setQueryState(
queryState.replace('rows=1000000', '').concat('', 'rows=50')
queryState.replace(`rows=${allDataCount}`, 'rows=50')
);
break;
case 'All':
document.cookie = 'rows=rows=1000000';
case allDataCount.toString():
setRows(allDataCount);
setQueryState(
queryState.replace('rows=50', '').concat('', 'rows=1000000')
queryState.replace('rows=50', `rows=${allDataCount}`)
);
break;
default:
Expand All @@ -129,15 +142,17 @@ export const DownloadFragment = () => {
}}
row
>
{allDataCount > 50 && (
<FormControlLabel
value="50"
control={<RadioButton />}
label="50 activities"
/>
)}
<FormControlLabel
value="50"
control={<RadioButton />}
label="50 activities"
/>
<FormControlLabel
value="All"
value={allDataCount.toString()}
control={<RadioButton />}
label="All activities"
label={`${allDataCount} activities`}
/>
</RadioGroup>
</Grid>
Expand Down

0 comments on commit 79473c0

Please sign in to comment.