Skip to content

Commit

Permalink
Create CSV export with only selected query rows
Browse files Browse the repository at this point in the history
Fixes #2348
  • Loading branch information
CarolineDenis committed May 10, 2023
1 parent bb54576 commit 6242d46
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function QueryExportButtons({
queryResource,
getQueryFieldRecords,
recordSetId,
selectedRows,
}: {
readonly baseTableName: keyof Tables;
readonly fields: RA<QueryField>;
Expand All @@ -31,6 +32,7 @@ export function QueryExportButtons({
| (() => RA<SerializedResource<SpQueryField>>)
| undefined;
readonly recordSetId: number | undefined;
readonly selectedRows: ReadonlySet<number>;
}): JSX.Element {
const showConfirmation = (): boolean =>
fields.some(({ mappingPath }) => !mappingPathIsComplete(mappingPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Props = {
readonly createRecordSet: JSX.Element | undefined;
readonly extraButtons: JSX.Element | undefined;
readonly tableClassName?: string;
readonly selectedRows: GetSet<ReadonlySet<number>>;
};

export function QueryResults(props: Props): JSX.Element {
Expand All @@ -86,6 +87,7 @@ export function QueryResults(props: Props): JSX.Element {
createRecordSet,
extraButtons,
tableClassName = '',
selectedRows: [selectedRows, setSelectedRows],
} = props;
const visibleFieldSpecs = fieldSpecs.filter(({ isPhantom }) => !isPhantom);

Expand Down Expand Up @@ -121,9 +123,9 @@ export function QueryResults(props: Props): JSX.Element {
const [treeRanksLoaded = false] = useAsyncState(fetchTreeRanks, false);

// Ids of selected records
const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(
new Set()
);
// const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(
// new Set()
// );
const lastSelectedRow = React.useRef<number | undefined>(undefined);
// Unselect all rows when query is reRun
React.useEffect(() => setSelectedRows(new Set()), [fieldSpecs]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import { ajax } from '../../utils/ajax';
import type { IR, RA } from '../../utils/types';
import type { GetSet, IR, RA } from '../../utils/types';
import { keysToLowerCase, replaceItem } from '../../utils/utils';
import type { SpecifyResource } from '../DataModel/legacyTypes';
import type { SpecifyModel } from '../DataModel/specifyModel';
Expand All @@ -27,12 +27,14 @@ export function QueryResultsWrapper({
extraButtons,
model,
onSelected: handleSelected,
// selectedRows: [selectedRows, setSelectedRows],
...props
}: ResultsProps & {
readonly model: SpecifyModel;
readonly createRecordSet: JSX.Element | undefined;
readonly extraButtons: JSX.Element | undefined;
readonly onSelected?: (selected: RA<number>) => void;
readonly selectedRows: GetSet<ReadonlySet<number>>;
}): JSX.Element | null {
const newProps = useQueryResultsWrapper(props);

Expand All @@ -49,6 +51,7 @@ export function QueryResultsWrapper({
extraButtons={extraButtons}
model={model}
onSelected={handleSelected}
// selectedRows={[selectedRows, setSelectedRows]}
/>
</ErrorBoundary>
</div>
Expand All @@ -69,6 +72,7 @@ type ResultsProps = {
*/
newFields: RA<QueryField>
) => void;
readonly selectedRows: GetSet<ReadonlySet<number>>;
};

type PartialProps = Omit<
Expand Down Expand Up @@ -99,13 +103,14 @@ export function useQueryResultsWrapper({
recordSetId,
forceCollection,
onSortChange: handleSortChange,
selectedRows: [selectedRows, setSelectedRows],
}: ResultsProps): PartialProps | undefined {
/*
* Need to store all props in a state so that query field edits do not affect
* the query results until query is reRun
*/
const [props, setProps] = React.useState<
Omit<PartialProps, 'totalCount'> | undefined
Omit<PartialProps, 'totalCount' | 'selectedRows'> | undefined
>(undefined);

const [totalCount, setTotalCount] = React.useState<number | undefined>(
Expand Down Expand Up @@ -217,5 +222,6 @@ export function useQueryResultsWrapper({
: {
...props,
totalCount,
selectedRows: [selectedRows, setSelectedRows],
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export function QueryBuilder({
const [query, setQuery] = useResource(queryResource);
useErrorContext('query', query);

const [selectedRows, setSelectedRows] = React.useState<ReadonlySet<number>>(
new Set()
);

const model = getModelById(query.contextTableId);
const buildInitialState = React.useCallback(
() =>
Expand Down Expand Up @@ -536,6 +540,7 @@ export function QueryBuilder({
getQueryFieldRecords={getQueryFieldRecords}
queryResource={queryResource}
recordSetId={recordSet?.id}
selectedRows={selectedRows}
/>
)
}
Expand All @@ -553,6 +558,7 @@ export function QueryBuilder({
});
runQuery('regular', fields);
}}
selectedRows={[selectedRows, setSelectedRows]}
/>
)}
</Form>
Expand Down

0 comments on commit 6242d46

Please sign in to comment.