-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add support for sorting columns in prep dialog #5214
Draft
grantfitzsimmons
wants to merge
11
commits into
production
Choose a base branch
from
issue-5142
base: production
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.
+115
−31
Draft
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
48ac3d5
Add initial support for sorting columns in prep dialog
grantfitzsimmons 4446a46
Preserve the selected items and quantities
grantfitzsimmons 6ae1e87
Fix bulk select inadvertently selecting all preps
grantfitzsimmons 1cad1d4
Begin fixing failing tests
grantfitzsimmons 96d1fc7
Merge branch 'production' into issue-5142
grantfitzsimmons d246bdd
Merge branch 'production' into issue-5142
grantfitzsimmons d49f7a0
Update specifyweb/frontend/js_src/lib/components/Interactions/PrepDia…
grantfitzsimmons cf6fac1
Update PrepDialog.tsx
grantfitzsimmons 2eb21b8
Merge branch 'issue-5142' of https://github.com/specify/specify7 into…
grantfitzsimmons 8a4c964
Merge branch 'production' into issue-5142
grantfitzsimmons 8f81e1b
Merge branch 'production' into issue-5142
grantfitzsimmons 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 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 |
---|---|---|
|
@@ -7,11 +7,12 @@ import { commonText } from '../../localization/common'; | |
import { interactionsText } from '../../localization/interactions'; | ||
import type { RA } from '../../utils/types'; | ||
import { defined, filterArray } from '../../utils/types'; | ||
import { group, replaceItem } from '../../utils/utils'; | ||
import { group } from '../../utils/utils'; | ||
import { Button } from '../Atoms/Button'; | ||
import { Form, Input, Label } from '../Atoms/Form'; | ||
import { Submit } from '../Atoms/Submit'; | ||
import { ReadOnlyContext } from '../Core/Contexts'; | ||
import { SortIndicator, useSortConfig } from '../Molecules/Sorting'; | ||
import { getField, toTable } from '../DataModel/helpers'; | ||
import type { AnyInteractionPreparation } from '../DataModel/helperTypes'; | ||
import type { SpecifyResource } from '../DataModel/legacyTypes'; | ||
|
@@ -63,14 +64,20 @@ export function PrepDialog({ | |
return mutatedPreparations as RA<PreparationData>; | ||
}, [rawPreparations, itemCollection]); | ||
|
||
const [selected, setSelected] = useLiveState<RA<number>>( | ||
React.useCallback( | ||
() => Array.from({ length: preparations.length }).fill(0), | ||
[preparations.length] | ||
) | ||
// Change to use an object for selected state allowing null values | ||
const [selected, setSelected] = useLiveState<{ | ||
[key: string]: number | null; | ||
}>( | ||
React.useCallback(() => { | ||
return preparations.reduce((acc, preparation) => { | ||
grantfitzsimmons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
acc[preparation.preparationId] = null; | ||
return acc; | ||
}, {} as { [key: string]: number | null }); | ||
}, [preparations]) | ||
); | ||
const canDeselect = selected.some((value) => value > 0); | ||
const canSelectAll = selected.some( | ||
|
||
const canDeselect = Object.values(selected).some((value) => value > 0); | ||
const canSelectAll = Object.values(selected).some( | ||
(value, index) => value < preparations[index].available | ||
); | ||
|
||
|
@@ -80,9 +87,37 @@ export function PrepDialog({ | |
// BUG: make this readOnly if don't have necessary permissions | ||
const isReadOnly = React.useContext(ReadOnlyContext); | ||
|
||
const [bulkValue, setBulkValue] = React.useState(0); | ||
const [bulkValue, setBulkValue] = React.useState<number | null>(null); // Allow bulkValue to be null | ||
const maxPrep = Math.max(...preparations.map(({ available }) => available)); | ||
|
||
const [sortConfig, handleSort, applySortConfig] = useSortConfig( | ||
'preparations', | ||
'catalogNumber', | ||
false | ||
); | ||
|
||
const sortedPreparations = applySortConfig( | ||
preparations, | ||
({ catalogNumber, taxon, prepType, available, unavailable }) => | ||
sortConfig.sortField === 'catalogNumber' | ||
? catalogNumber | ||
: sortConfig.sortField === 'taxon' | ||
? taxon | ||
: sortConfig.sortField === 'prepType' | ||
? prepType | ||
: sortConfig.sortField === 'available' | ||
? available | ||
: unavailable | ||
); | ||
|
||
// Handle selection change | ||
const handleSelectChange = (preparationId: string, newSelected: number | null): void => { | ||
setSelected((prev) => ({ | ||
...prev, | ||
[preparationId]: newSelected, | ||
})); | ||
}; | ||
|
||
return ( | ||
<Dialog | ||
buttons={ | ||
|
@@ -95,15 +130,27 @@ export function PrepDialog({ | |
disabled={!canSelectAll} | ||
title={interactionsText.selectAllAvailablePreparations()} | ||
onClick={(): void => | ||
setSelected(preparations.map(({ available }) => available)) | ||
setSelected((prev) => { | ||
const newSelected = { ...prev }; | ||
sortedPreparations.forEach((prep) => { | ||
newSelected[prep.preparationId] = prep.available; | ||
}); | ||
return newSelected; | ||
}) | ||
} | ||
> | ||
{interactionsText.selectAll()} | ||
</Button.Info> | ||
<Button.Info | ||
disabled={!canDeselect} | ||
title={commonText.clearAll()} | ||
onClick={(): void => setSelected(Array.from(selected).fill(0))} | ||
onClick={(): void => setSelected((prev) => { | ||
const newSelected = { ...prev }; | ||
Object.keys(newSelected).forEach((key) => { | ||
newSelected[key] = null; | ||
}); | ||
return newSelected; | ||
})} | ||
> | ||
{interactionsText.deselectAll()} | ||
</Button.Info> | ||
|
@@ -133,12 +180,28 @@ export function PrepDialog({ | |
max={maxPrep} | ||
min={0} | ||
title={interactionsText.selectedAmount()} | ||
value={bulkValue} | ||
value={bulkValue || 0} | ||
onValueChange={(newCount): void => { | ||
setBulkValue(newCount); | ||
setSelected( | ||
preparations.map(({ available }) => Math.min(available, newCount)) | ||
); | ||
if (newCount === '') { | ||
setBulkValue(0); | ||
setSelected((prev) => { | ||
const newSelected = { ...prev }; | ||
sortedPreparations.forEach((prep) => { | ||
newSelected[prep.preparationId] = null; | ||
}); | ||
return newSelected; | ||
}); | ||
} else { | ||
const count = parseInt(newCount, 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, the |
||
setBulkValue(count); | ||
setSelected((prev) => { | ||
const newSelected = { ...prev }; | ||
sortedPreparations.forEach((prep) => { | ||
newSelected[prep.preparationId] = count === 0 ? 0 : Math.min(prep.available, count); | ||
}); | ||
return newSelected; | ||
}); | ||
} | ||
}} | ||
/> | ||
</Label.Inline> | ||
|
@@ -156,14 +219,15 @@ export function PrepDialog({ | |
) as SpecifyTable<AnyInteractionPreparation>; | ||
|
||
const items = filterArray( | ||
preparations.map((preparation, index) => { | ||
if (selected[index] === 0) return undefined; | ||
preparations.map((preparation) => { | ||
const selectedQuantity = selected[preparation.preparationId]; | ||
if (selectedQuantity === null || selectedQuantity === 0) return undefined; | ||
const result = new itemTable.Resource(); | ||
result.set( | ||
'preparation', | ||
getResourceApiUrl('Preparation', preparation.preparationId) | ||
); | ||
result.set('quantity', selected[index]); | ||
result.set('quantity', selectedQuantity); | ||
const loanPreparation = toTable(result, 'LoanPreparation'); | ||
loanPreparation?.set('quantityReturned', 0); | ||
loanPreparation?.set('quantityResolved', 0); | ||
|
@@ -196,28 +260,45 @@ export function PrepDialog({ | |
<span className="sr-only">{interactionsText.selectAll()}</span> | ||
</th> | ||
<th scope="col"> | ||
{getField(tables.CollectionObject, 'catalogNumber').label} | ||
<Button.LikeLink onClick={(): void => handleSort('catalogNumber')}> | ||
{getField(tables.CollectionObject, 'catalogNumber').label} | ||
<SortIndicator fieldName="catalogNumber" sortConfig={sortConfig} /> | ||
</Button.LikeLink> | ||
</th> | ||
<th scope="col"> | ||
{getField(tables.Determination, 'taxon').label} | ||
<Button.LikeLink onClick={(): void => handleSort('taxon')}> | ||
{getField(tables.Determination, 'taxon').label} | ||
<SortIndicator fieldName="taxon" sortConfig={sortConfig} /> | ||
</Button.LikeLink> | ||
</th> | ||
<th scope="col"> | ||
{getField(tables.Preparation, 'prepType').label} | ||
<Button.LikeLink onClick={(): void => handleSort('prepType')}> | ||
{getField(tables.Preparation, 'prepType').label} | ||
<SortIndicator fieldName="prepType" sortConfig={sortConfig} /> | ||
</Button.LikeLink> | ||
</th> | ||
<th scope="col">{commonText.selected()}</th> | ||
<th scope="col">{interactionsText.available()}</th> | ||
<th scope="col">{interactionsText.unavailable()}</th> | ||
<th scope="col"> | ||
<Button.LikeLink onClick={(): void => handleSort('available')}> | ||
{interactionsText.available()} | ||
<SortIndicator fieldName="available" sortConfig={sortConfig} /> | ||
</Button.LikeLink> | ||
</th> | ||
<th scope="col"> | ||
<Button.LikeLink onClick={(): void => handleSort('unavailable')}> | ||
{interactionsText.unavailable()} | ||
<SortIndicator fieldName="unavailable" sortConfig={sortConfig} /> | ||
</Button.LikeLink> | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{preparations.map((preparation, index) => ( | ||
{sortedPreparations.map((preparation) => ( | ||
<PrepDialogRow | ||
key={index} | ||
key={preparation.preparationId} | ||
preparation={preparation} | ||
selected={selected[index]} | ||
onChange={(newSelected): void => | ||
setSelected(replaceItem(selected, index, newSelected)) | ||
} | ||
selected={selected[preparation.preparationId] === null ? 0 : selected[preparation.preparationId]} | ||
onChange={(newSelected): void => handleSelectChange(preparation.preparationId, newSelected)} | ||
/> | ||
))} | ||
</tbody> | ||
|
@@ -240,7 +321,7 @@ function setPreparationItems( | |
) | ||
); | ||
|
||
// Typecast as a single case because the relatiships do not exist in the union type. | ||
// Typecast as a single case because the relationships do not exist in the union type. | ||
(interaction as SpecifyResource<ExchangeOut>).set( | ||
preparationRelationship.name as 'exchangeOutPreps', | ||
items as RA<SpecifyResource<ExchangeOutPrep>> | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(optional) instead of null, could you refactor the code you added in this file to use
undefined
? that would be more consistent with what we use in the rest of the codebase