Skip to content

Commit

Permalink
Merge pull request #6145 from specify/issue-6141
Browse files Browse the repository at this point in the history
Remove changes in production from #6061
  • Loading branch information
CarolineDenis authored Jan 23, 2025
2 parents 185b8c8 + 09a76e8 commit 028677a
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 620 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,13 @@ function PreparationReturn({
/>
</Label.Inline>
</div>
<table className="grid-table grid-cols-[repeat(9,auto)] gap-2">
<table className="grid-table grid-cols-[repeat(8,auto)] gap-2">
<thead>
<tr>
<td />
<th className="text-center" scope="col">
{getField(tables.CollectionObject, 'catalogNumber').label}
</th>
<th className="text-center" scope="col">
{tables.CollectionObjectGroup.label}
</th>
<th className="text-center" scope="col">
{getField(tables.Determination, 'taxon').label}
</th>
Expand Down
110 changes: 63 additions & 47 deletions specifyweb/frontend/js_src/lib/components/Interactions/PrepDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useId } from '../../hooks/useId';
import { useLiveState } from '../../hooks/useLiveState';
import { commonText } from '../../localization/common';
import { interactionsText } from '../../localization/interactions';
import { ajax } from '../../utils/ajax';
import type { RA } from '../../utils/types';
import { defined, filterArray } from '../../utils/types';
import { group, replaceItem } from '../../utils/utils';
Expand All @@ -15,11 +16,16 @@ import { ReadOnlyContext } from '../Core/Contexts';
import { getField, toTable } from '../DataModel/helpers';
import type { AnyInteractionPreparation } from '../DataModel/helperTypes';
import type { SpecifyResource } from '../DataModel/legacyTypes';
import { getResourceApiUrl, getResourceViewUrl } from '../DataModel/resource';
import {
getResourceApiUrl,
getResourceViewUrl,
idFromUrl,
} from '../DataModel/resource';
import { serializeResource } from '../DataModel/serializers';
import type { Collection, SpecifyTable } from '../DataModel/specifyTable';
import { tables } from '../DataModel/tables';
import type { ExchangeOut, ExchangeOutPrep } from '../DataModel/types';
import { softFail } from '../Errors/Crash';
import { Dialog } from '../Molecules/Dialog';
import type { InteractionWithPreps, PreparationData } from './helpers';
import { interactionPrepTables } from './helpers';
Expand All @@ -44,10 +50,7 @@ export function PrepDialog({
const indexedPreparations = Object.fromEntries(
group(
mutatedPreparations.map((preparation) => [
getResourceApiUrl(
'Preparation',
preparation.preparationId as PreparationData['preparationId']
),
getResourceApiUrl('Preparation', preparation.preparationId),
preparation,
])
)
Expand Down Expand Up @@ -86,13 +89,18 @@ export function PrepDialog({
const [bulkValue, setBulkValue] = React.useState(0);
const maxPrep = Math.max(...preparations.map(({ available }) => available));

const consolidatedPreps = React.useMemo(
() =>
preparations
.map((prepData, index) => [index, prepData] as const)
.filter(([_, prepData]) => prepData.isConsolidated),
[preparations]
);
const fetchSiblings = async (
preparationIds: RA<number>
): Promise<RA<number>> =>
ajax<RA<number>>(`/interactions/sibling_preps/`, {
method: 'POST',
headers: {
Accept: 'application/json',
},
body: {
ids: preparationIds,
},
}).then(({ data }) => data);

return (
<Dialog
Expand Down Expand Up @@ -182,25 +190,49 @@ export function PrepDialog({
})
);

if (typeof itemCollection === 'object') {
itemCollection.add(items);
handleClose();
} else {
const interaction = new table.Resource();
setPreparationItems(interaction, items);
const preparationIds: RA<number> = items
.map((item) => idFromUrl(item.get('preparation') ?? ''))
.filter((id): id is number => id !== undefined);

void fetchSiblings(preparationIds)
.then((siblings) => {
const siblingsPreps = siblings.map((preparation) => {
const result = new itemTable.Resource();
result.set(
'preparation',
getResourceApiUrl('Preparation', preparation)
);
// Need to find a way to set the maximum
result.set('quantity', 1);
const loanPreparation = toTable(result, 'LoanPreparation');
loanPreparation?.set('quantityReturned', 0);
loanPreparation?.set('quantityResolved', 0);
return result;
});

const mergedPreparations = [...items, ...siblingsPreps];

const loan = toTable(interaction, 'Loan');
loan?.set('isClosed', false);
navigate(getResourceViewUrl(table.name, undefined), {
state: {
type: 'RecordSet',
resource: serializeResource(interaction),
},
});
}
if (typeof itemCollection === 'object') {
itemCollection.add(mergedPreparations);
handleClose();
} else {
const interaction = new table.Resource();
setPreparationItems(interaction, mergedPreparations);

const loan = toTable(interaction, 'Loan');
loan?.set('isClosed', false);
navigate(getResourceViewUrl(table.name, undefined), {
state: {
type: 'RecordSet',
resource: serializeResource(interaction),
},
});
}
})
.catch((error) => softFail(error));
}}
>
<table className="grid-table grid-cols-[min-content_repeat(7,auto)] gap-2">
<table className="grid-table grid-cols-[min-content_repeat(6,auto)] gap-2">
<thead>
<tr>
<th scope="col">
Expand All @@ -212,7 +244,6 @@ export function PrepDialog({
<th scope="col">
{getField(tables.Determination, 'taxon').label}
</th>
<th scope="col">{tables.CollectionObjectGroup.label}</th>
<th scope="col">
{getField(tables.Preparation, 'prepType').label}
</th>
Expand All @@ -227,24 +258,9 @@ export function PrepDialog({
key={index}
preparation={preparation}
selected={selected[index]}
onChange={(newSelected): void => {
if (preparation.isConsolidated)
consolidatedPreps.forEach(([prepIndex, prep]) => {
if (prepIndex !== index)
setSelected((selected) =>
replaceItem(
selected,
prepIndex,
newSelected > prep.available
? prep.available
: newSelected
)
);
});
setSelected((selected) =>
replaceItem(selected, index, newSelected)
);
}}
onChange={(newSelected): void =>
setSelected(replaceItem(selected, index, newSelected))
}
/>
))}
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,9 @@ export function PrepDialogRow({
</Link.NewTab>
</td>
<td>
{preparation.taxon !== undefined &&
preparation.taxonId !== undefined ? (
<Link.NewTab
href={getResourceViewUrl('Taxon', preparation.taxonId)}
>
{localized(preparation.taxon)}
</Link.NewTab>
) : undefined}
</td>
<td>
{preparation.cogName !== undefined &&
preparation.cogId !== undefined ? (
<Link.NewTab
href={getResourceViewUrl(
'CollectionObjectGroup',
preparation.cogId
)}
>
{localized(preparation.cogName)}
</Link.NewTab>
) : undefined}
<Link.NewTab href={getResourceViewUrl('Taxon', preparation.taxonId)}>
{localized(preparation.taxon)}
</Link.NewTab>
</td>
<td>{preparation.prepType}</td>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { commonText } from '../../localization/common';
import { interactionsText } from '../../localization/interactions';
import { Button } from '../Atoms/Button';
import { Input } from '../Atoms/Form';
import { cogTypes, getField } from '../DataModel/helpers';
import { getField } from '../DataModel/helpers';
import type { SpecifyResource } from '../DataModel/legacyTypes';
import { tables } from '../DataModel/tables';
import type { LoanPreparation } from '../DataModel/types';
Expand All @@ -29,8 +29,6 @@ export function PrepReturnRow({
readonly catalogNumber: string;
readonly taxon: string;
readonly prepType: string;
readonly cogName: string | undefined;
readonly isConsolidated: boolean;
}>(
React.useCallback(
async () =>
Expand All @@ -42,15 +40,11 @@ export function PrepReturnRow({
preparation.get('descriptionOfMaterial')?.slice(0, 50) ??
interactionsText.unCataloged(),
prepType: '',
cogName: undefined,
isConsolidated: false,
}
: {
...(await loanPreparation.rgetPromise('collectionObject').then<{
readonly catalogNumber: string;
readonly taxon: string;
readonly cogName: string | undefined;
readonly isConsolidated: boolean;
}>(async (collectionObject) => ({
catalogNumber: await fieldFormat(
getField(tables.CollectionObject, 'catalogNumber'),
Expand All @@ -64,27 +58,6 @@ export function PrepReturnRow({
?.rgetPromise('preferredTaxon')
)
.then((taxon) => taxon?.get('fullName') ?? ''),
...((await collectionObject
.getDependentResource('cojo')
?.rgetPromise('parentCog')
.then<{
readonly cogName: string;
readonly isConsolidated: boolean;
}>(async (cog) => ({
cogName: await fieldFormat(
getField(tables.CollectionObjectGroup, 'name'),
cog.get('name')
),
isConsolidated: await cog
.rgetPromise('cogType')
.then(
(cogType) =>
cogType.get('type') === cogTypes.CONSOLIDATED
),
}))) ?? {
cogName: undefined,
isConsolidated: false,
}),
}))),
prepType: await loanPreparation
.rgetPromise('prepType')
Expand Down Expand Up @@ -118,7 +91,6 @@ export function PrepReturnRow({
/>
</td>
<td>{data?.catalogNumber ?? commonText.loading()}</td>
<td>{data?.cogName ?? commonText.loading()}</td>
<td>{data?.taxon ?? commonText.loading()}</td>
<td className="text-center">
{data?.prepType ?? commonText.loading()}
Expand Down
55 changes: 22 additions & 33 deletions specifyweb/frontend/js_src/lib/components/Interactions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { ajax } from '../../utils/ajax';
import { formData } from '../../utils/ajax/helpers';
import type { RA, RestrictedTuple } from '../../utils/types';
import type { AnyInteractionPreparation } from '../DataModel/helperTypes';
import type {
CollectionObject,
CollectionObjectGroup,
Tables,
} from '../DataModel/types';
import type { Tables } from '../DataModel/types';

export const interactionPrepTables: RestrictedTuple<
AnyInteractionPreparation['tableName']
Expand All @@ -32,35 +28,29 @@ export const interactionsWithPrepTables: RestrictedTuple<
export type PreparationData = {
readonly catalogNumber: string;
readonly collectionObjectId: number;
readonly taxon: string | undefined;
readonly taxonId: number | undefined;
readonly taxon: string;
readonly taxonId: number;
readonly preparationId: number;
readonly prepType: string;
readonly countAmount: number;
readonly loaned: number;
readonly gifted: number;
readonly exchanged: number;
readonly available: number;
readonly cogId: number | undefined;
readonly cogName: string | undefined;
readonly isConsolidated: boolean;
};

export type PreparationRow = readonly [
catalogNumber: string, // 0
collectionObjectId: number, // 1
taxonFullName: string | null, // 2
taxonId: number | null, // 3
preparationId: number, // 4
prepType: string, // 5
preparationCountAmt: number, // 6
amountLoaned: string | null, // 7
amountedGifted: string | null, // 8
amountExchanged: string | null, // 9
amountAvailable: string, // 10
cogId: number | null, // 11
cogName: string | null, // 12
isConsolidated: 0 | 1, // 13
catalogNumber: string,
collectionObjectId: number,
taxonFullName: string,
taxonId: number,
preparationId: number,
prepType: string,
preparationCountAmt: number,
amountLoaned: string | null,
amountedGifted: string | null,
amountExchanged: string | null,
amountAvailable: string,
];

export const getPrepsAvailableForLoanRs = async (
Expand All @@ -76,20 +66,19 @@ export const getPrepsAvailableForLoanRs = async (
}
).then(({ data }) => data);

export const getPrepsForCoOrCog = async (
model: CollectionObject['tableName'] | CollectionObjectGroup['tableName'],
fieldName: string,
records: RA<string>,
export const getPrepsAvailableForLoanCoIds = async (
idField: string,
collectionObjectIds: RA<string>,
isLoan: boolean
) =>
ajax<RA<PreparationRow>>(`/interactions/associated_preps/${model}/`, {
ajax<RA<PreparationRow>>('/interactions/preparations_available_ids/', {
method: 'POST',
headers: { Accept: 'application/json' },
body: {
fieldName,
records,
body: formData({
id_fld: idField,
co_ids: collectionObjectIds,
isLoan,
},
}),
}).then(({ data }) => data);

export const getInteractionsForPrepId = async (prepId: number) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function ReportRecordSets({
return state.type === 'Main' ? (
<ReadOnlyContext.Provider value>
<RecordSetsDialog
tables={table}
table={table}
onClose={handleClose}
onConfigure={(recordSet): void =>
setState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function testFilter<SCHEMA extends AnySchema>(
? // Cast numbers to strings
values.some((value) => {
const fieldValue = resource.get(field);
// eslint-disable-next-line eqeqeq

return isRelationship
? value == strictIdFromUrl(fieldValue!).toString()
: value == fieldValue;
Expand Down
Loading

0 comments on commit 028677a

Please sign in to comment.