Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import type { State } from 'typesafe-reducer';

import { useAsyncState } from '../../hooks/useAsyncState';
import { useValidation } from '../../hooks/useValidation';
import { commonText } from '../../localization/common';
import { interactionsText } from '../../localization/interactions';
Expand Down Expand Up @@ -32,6 +33,7 @@ import type {
} from '../DataModel/helperTypes';
import type { SpecifyResource } from '../DataModel/legacyTypes';
import { getResourceViewUrl } from '../DataModel/resource';
import { fetchContext as fetchDomain } from '../DataModel/schema';
import type { LiteralField } from '../DataModel/specifyField';
import type { Collection, SpecifyTable } from '../DataModel/specifyTable';
import { tables } from '../DataModel/tables';
Expand Down Expand Up @@ -202,14 +204,25 @@ export function InteractionDialog({
})),
});

const [collectionHasSeveralTypes] = useAsyncState(
React.useCallback(
async () =>
fetchDomain.then(async (schema) => Object.keys(schema.collectionObjectTypeCatalogNumberFormats).length > 1),
[]
),
false
);

function handleParse(): RA<string> | undefined {
const parseResults = split(catalogNumbers).map((value) =>
parseValue(parser, inputRef.current ?? undefined, value)
);

const errorMessages = parseResults
.filter((result): result is InvalidParseResult => !result.isValid)
.map(({ reason, value }) => `${reason} (${value})`);
if (errorMessages.length > 0) {
.filter((result): result is InvalidParseResult => !result.isValid)
.map(({ reason, value }) => `${reason} (${value})`);

if (errorMessages.length > 0 && collectionHasSeveralTypes === false) {
setValidation(errorMessages);
setState({
type: 'InvalidState',
Expand All @@ -218,12 +231,22 @@ export function InteractionDialog({
return undefined;
}

if (collectionHasSeveralTypes === true) {
const parsedCatNumber = split(catalogNumbers);

setCatalogNumbers(parsedCatNumber.join('\n'));
setState({ type: 'MainState' });

return parsedCatNumber.map(String);
}

const parsed = f.unique(
(parseResults as RA<ValidParseResult>)
.filter(({ parsed }) => parsed !== null)
.map(({ parsed }) => (parsed as number | string).toString())
.sort(sortFunction(f.id))
);

setCatalogNumbers(parsed.join('\n'));

setState({ type: 'MainState' });
Expand Down