-
Notifications
You must be signed in to change notification settings - Fork 51
Data Views #8251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grantfitzsimmons
wants to merge
32
commits into
main
Choose a base branch
from
issue-6565
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Data Views #8251
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1720e88
feat: implement basic Data Views
melton-jason c1a9e96
Merge remote-tracking branch 'origin/main' into issue-6565
melton-jason 4433f4c
Merge remote-tracking branch 'origin/main' into issue-6565
melton-jason d3d70bc
Merge branch 'main' into issue-6565
grantfitzsimmons 84e073e
Merge branch 'main' into issue-6565
grantfitzsimmons ab32217
Merge remote-tracking branch 'origin/main' into issue-6565
CarolineDenis 615557f
Merge branch 'main' into issue-6565
CarolineDenis f76ce7f
Refactor: Improve fetch index
CarolineDenis 9a00309
Chore: cleanup
CarolineDenis 1c07b95
Feat: Add a new sort picker in dataView
CarolineDenis 79328b1
Fix: Typescript types
CarolineDenis 43fe3a0
Fix: Typescript types
CarolineDenis 9462044
Fix: Typescript types
CarolineDenis f12b86c
Fix: Typescript
CarolineDenis ac1031d
Fix: Use localization
CarolineDenis f169454
Update specifyweb/frontend/js_src/lib/hooks/usePaginatedCollection.tsx
CarolineDenis d1603fb
Potential fix for pull request finding 'CodeQL / Unused variable, imp…
CarolineDenis f14ceff
Fix: Localization error
CarolineDenis 6c33abb
Refactor: reuse QueryTables comp in DataViews
CarolineDenis b91e06b
Fix: Keep canFetchMore true for sparse entries
CarolineDenis 99d8b42
Fix: Change data view dialog header
CarolineDenis f59bd59
Fix: Restrict default lis of tables
CarolineDenis e1d4313
Fix: Reuse existing localization
CarolineDenis e356403
Clean: Remove use current scope
CarolineDenis c758849
Fix: typo in timestamp field
CarolineDenis 4a38754
Fix: Reset table state when table.name changes
CarolineDenis 3c9d3b4
Feat: Add a total count amount next to each table
CarolineDenis cf38805
Merge branch 'main' into issue-6565
CarolineDenis 4cdffc4
Lint code with ESLint and Prettier
CarolineDenis 29d0b24
Merge branch 'main' into issue-6565
grantfitzsimmons 0e54224
Lint code with ESLint and Prettier
grantfitzsimmons 6084b52
Merge remote-tracking branch 'origin/main' into issue-6565
CarolineDenis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
specifyweb/frontend/js_src/lib/components/DataViews/DataViewTables.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import React from 'react'; | ||
| import { useBooleanState } from '../../hooks/useBooleanState'; | ||
| import { commonText } from '../../localization/common'; | ||
| import { dataViewsText } from '../../localization/dataViews'; | ||
| import { Http } from '../../utils/ajax/definitions'; | ||
| import { throttledPromise } from '../../utils/ajax/throttledPromise'; | ||
| import type { GetSet, IR, RA } from '../../utils/types'; | ||
| import { Button } from '../Atoms/Button'; | ||
| import { DataEntry } from '../Atoms/DataEntry'; | ||
| import { icons } from '../Atoms/Icons'; | ||
| import { serializeResource } from '../DataModel/serializers'; | ||
| import { SpecifyTable } from '../DataModel/specifyTable'; | ||
| import { getTableById, strictGetTable } from '../DataModel/tables'; | ||
| import { Tables } from '../DataModel/types'; | ||
| import { raise } from '../Errors/Crash'; | ||
| import { Dialog, dialogClassNames } from '../Molecules/Dialog'; | ||
| import { userPreferences } from '../Preferences/userPreferences'; | ||
| import { OverlayContext } from '../Router/Router'; | ||
| import { tablesFilter } from '../SchemaConfig/Tables'; | ||
| import { | ||
| queryCountPromiseGenerator, | ||
| querySpecToResource, | ||
| } from '../Statistics/hooks'; | ||
| import { TablesListEdit } from '../Toolbar/QueryTablesEdit'; | ||
| import { QueryTables } from '../Toolbar/QueryTablesWrapper'; | ||
|
|
||
| const defaultDataViewTablesConfig: RA<keyof Tables> = [ | ||
| 'Accession', | ||
| 'Agent', | ||
| 'CollectionObject', | ||
| 'CollectingEvent', | ||
| 'Gift', | ||
| 'Loan', | ||
| 'Locality', | ||
| ]; | ||
|
|
||
| export function DataViewTables(): JSX.Element { | ||
| const handleClose = React.useContext(OverlayContext); | ||
| const [tables, setTables] = useDataViewTables(); | ||
| const [isEditing, handleEditing] = useBooleanState(); | ||
| const counts = useTableRecordCounts(tables); | ||
| return isEditing ? ( | ||
| <TablesListEdit | ||
| defaultTables={defaultDataViewTablesConfig} | ||
| header={dataViewsText.configureDataViews()} | ||
| tables={tables} | ||
| onChange={setTables} | ||
| onClose={handleClose} | ||
| /> | ||
| ) : ( | ||
| <Dialog | ||
| header={dataViewsText.dataViewsTitle()} | ||
| buttons={ | ||
| <> | ||
| <span className="-ml-2 flex-1" /> | ||
| <Button.Secondary onClick={handleClose}> | ||
| {commonText.close()} | ||
| </Button.Secondary> | ||
| </> | ||
| } | ||
| className={{ | ||
| container: dialogClassNames.narrowContainer, | ||
| }} | ||
| headerButtons={<DataEntry.Edit onClick={handleEditing} />} | ||
| icon={icons.eye} | ||
| onClose={handleClose} | ||
| > | ||
| <QueryTables | ||
| counts={counts} | ||
| getHref={(name) => `/specify/dataviews/${name.toLowerCase()}`} | ||
| tables={tables} | ||
| onClick={undefined} | ||
| /> | ||
| </Dialog> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches the number of records in each table in the background, using the | ||
| * same count-only query builder logic that is used in the Statistics page. | ||
| */ | ||
| function useTableRecordCounts( | ||
| tables: RA<SpecifyTable> | ||
| ): IR<number | undefined> { | ||
| const [counts, setCounts] = React.useState<IR<number | undefined>>({}); | ||
|
|
||
| React.useEffect(() => { | ||
| let destructorCalled = false; | ||
| tables.forEach((table) => { | ||
| const query = serializeResource( | ||
| querySpecToResource(table.name, { | ||
| tableName: table.name, | ||
| fields: [], | ||
| }) | ||
| ); | ||
| throttledPromise<number | undefined>( | ||
| 'queryStats', | ||
| async () => | ||
| queryCountPromiseGenerator(query)().then((response) => | ||
| response.status === Http.OK ? response.data.count : undefined | ||
| ), | ||
| JSON.stringify(query) | ||
| ) | ||
| .then((count) => { | ||
| if (destructorCalled) return; | ||
| setCounts((previousCounts) => ({ | ||
| ...previousCounts, | ||
| [table.name]: count, | ||
| })); | ||
| }) | ||
| .catch(raise); | ||
| }); | ||
| return (): void => { | ||
| destructorCalled = true; | ||
| }; | ||
| }, [tables]); | ||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| function useDataViewTables(): GetSet<RA<SpecifyTable>> { | ||
| const [tables, setTables] = userPreferences.use( | ||
| 'dataViews', | ||
| 'general', | ||
| 'shownTables' | ||
| ); | ||
| const allowedTables = React.useMemo(() => { | ||
| const visibleTables = | ||
| tables.length === 0 | ||
| ? defaultDataViewTablesConfig.map(strictGetTable) | ||
| : tables.map(getTableById); | ||
|
|
||
| return visibleTables.filter((table) => | ||
| tablesFilter(true, false, true, table) | ||
| ); | ||
| }, [tables]); | ||
|
|
||
| const handleChange = React.useCallback( | ||
| (models: RA<SpecifyTable>) => | ||
| setTables(models.map((model) => model.tableId)), | ||
| [setTables] | ||
| ); | ||
|
|
||
| return [allowedTables, handleChange]; | ||
| } |
151 changes: 151 additions & 0 deletions
151
specifyweb/frontend/js_src/lib/components/DataViews/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import React from 'react'; | ||
| import { useParams } from 'react-router-dom'; | ||
| import { | ||
| type CollectionFetchFilters, | ||
| DEFAULT_FETCH_LIMIT, | ||
| fetchCollection, | ||
| } from '../DataModel/collection'; | ||
| import { AnySchema, SerializedResource } from '../DataModel/helperTypes'; | ||
| import { SpecifyTable } from '../DataModel/specifyTable'; | ||
| import { getTable } from '../DataModel/tables'; | ||
| import { ProtectedTable } from '../Permissions/PermissionDenied'; | ||
| import { RecordSelectorFromIds } from '../FormSliders/RecordSelectorFromIds'; | ||
| import { NotFoundView } from '../Router/NotFoundView'; | ||
| import { f } from '../../utils/functools'; | ||
| import { commonText } from '../../localization/common'; | ||
| import { dataViewsText } from '../../localization/dataViews'; | ||
| import { usePaginatedCollection } from '../../hooks/usePaginatedCollection'; | ||
| import { Tables } from '../DataModel/types'; | ||
| import { useAsyncState } from '../../hooks/useAsyncState'; | ||
| import { RA } from '../../utils/types'; | ||
| import { Label } from '../Atoms/Form'; | ||
| import { OrderPicker, type OrderPickerOrder } from '../Preferences/Renderers'; | ||
| import { attachmentsText } from '../../localization/attachments'; | ||
|
|
||
| export function TableDataView(): JSX.Element { | ||
| const { tableName = '' } = useParams(); | ||
|
|
||
| const table = getTable(tableName); | ||
|
|
||
| return table === undefined ? ( | ||
| <NotFoundView /> | ||
| ) : ( | ||
| <ProtectedTable tableName={table.name} action="read"> | ||
| <DataViewFromTableWrapped table={table} /> | ||
| </ProtectedTable> | ||
| ); | ||
| } | ||
|
|
||
| function DataViewFromTableWrapped<SCHEMA extends AnySchema>({ | ||
| table, | ||
| }: { | ||
| readonly table: SpecifyTable<SCHEMA>; | ||
| }): JSX.Element | null { | ||
| const defaultOrder = | ||
| table.getLiteralField('timestampCreated') === undefined | ||
| ? '-id' | ||
| : '-timestampCreated'; | ||
|
|
||
| const [order, setOrder] = React.useState<OrderPickerOrder<SCHEMA, 'id'>>( | ||
| defaultOrder as OrderPickerOrder<SCHEMA, 'id'> | ||
| ); | ||
|
|
||
| React.useEffect(() => { | ||
| setOrder(defaultOrder as OrderPickerOrder<SCHEMA, 'id'>); | ||
| }, [table.name, defaultOrder]); | ||
|
|
||
| const handleFetchingCollection = React.useCallback( | ||
| (offset: number = 0) => | ||
| fetchCollection(table.name, { | ||
| offset, | ||
| limit: DEFAULT_FETCH_LIMIT, | ||
| domainFilter: true, | ||
| orderBy: order, | ||
| } as CollectionFetchFilters<Tables[SCHEMA['tableName']]>), | ||
| [order, table] | ||
| ); | ||
|
|
||
| const [collection] = useAsyncState(handleFetchingCollection, true); | ||
|
|
||
| return collection === undefined ? null : ( | ||
| <DataViewFromTable | ||
| key={table.name} | ||
| table={table} | ||
| initialRecords={collection.records} | ||
| totalCount={collection.totalCount} | ||
| onFetchRecords={(index) => | ||
| handleFetchingCollection(index).then(({ records }) => records) | ||
| } | ||
| headerButtons={ | ||
| <> | ||
| <Label.Inline> | ||
| {attachmentsText.orderBy()} | ||
| <div> | ||
| <OrderPicker<SCHEMA, 'id'> | ||
| additionalFields={[{ name: 'id', label: commonText.id() }]} | ||
| includeHiddenFields | ||
| includeVirtualFields={false} | ||
| order={order} | ||
| table={table} | ||
| onChange={setOrder} | ||
| /> | ||
| </div> | ||
| </Label.Inline> | ||
| </> | ||
| } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function DataViewFromTable<SCHEMA extends AnySchema>({ | ||
| table, | ||
| totalCount: initialTotalCount, | ||
| initialRecords, | ||
| onFetchRecords: handleFetchRecords, | ||
| headerButtons, | ||
| }: { | ||
| readonly table: SpecifyTable<SCHEMA>; | ||
| readonly totalCount: number; | ||
| readonly initialRecords: RA<SerializedResource<Tables[SCHEMA['tableName']]>>; | ||
| readonly onFetchRecords: ( | ||
| index: number | ||
| ) => Promise<RA<SerializedResource<Tables[SCHEMA['tableName']]>>>; | ||
| readonly headerButtons: JSX.Element; | ||
| }): JSX.Element | null { | ||
| // FEATURE: Use useNavigator and keep current record/index in query | ||
| // parameter of page | ||
|
|
||
| const { | ||
| results: [collection], | ||
| totalCount: [totalCount], | ||
| onFetchMore: handleFetchMore, | ||
| } = usePaginatedCollection({ | ||
| fetchMore: handleFetchRecords, | ||
| initialRecords, | ||
| totalCount: initialTotalCount, | ||
| }); | ||
|
|
||
| return collection === undefined ? null : ( | ||
| <RecordSelectorFromIds | ||
| canRemove={false} | ||
| defaultIndex={0} | ||
| headerButtons={headerButtons} | ||
| dialog={false} | ||
| ids={collection.map((record) => record?.id)} | ||
| isDependent={false} | ||
| isInRecordSet={false} | ||
| newResource={undefined} | ||
| table={table} | ||
| title={dataViewsText.tableRecords({ tableLabel: table.label })} | ||
| totalCount={totalCount} | ||
| onAdd={undefined} | ||
| onClone={undefined} | ||
| onClose={() => undefined} | ||
| onDelete={undefined} | ||
| onSaved={f.void} | ||
| onSlide={(new_index) => { | ||
| handleFetchMore(new_index); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.