Skip to content
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 "default" initialize property to QCBX #6037

Draft
wants to merge 12 commits into
base: production
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ const queryComboBoxSpec = (
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
defaultRecord: syncers.xmlAttribute('initialize default', 'skip'),
legacyHelpContext: syncers.xmlAttribute('initialize hc', 'skip'),
// Make query compatible with multiple ORMs
legacyAdjustQuery: pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ const fieldRenderers: {
hasEditButton,
hasSearchButton,
hasViewButton,
defaultRecord,
},
}) {
return field === undefined || !field.isRelationship ? null : (
<QueryComboBox
defaultRecord={defaultRecord}
field={field}
forceCollection={undefined}
formType={formType}
Expand Down
2 changes: 2 additions & 0 deletions specifyweb/frontend/js_src/lib/components/FormParse/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type FieldTypes = {
readonly hasViewButton: boolean;
readonly typeSearch: string | undefined;
readonly searchView: string | undefined;
readonly defaultRecord: string | undefined;
}
>;
readonly Text: State<
Expand Down Expand Up @@ -246,6 +247,7 @@ const processFieldType: {
: getProperty('viewBtn')?.toLowerCase() === 'true',
typeSearch: getProperty('name'),
searchView: getProperty('searchView'),
defaultRecord: getProperty('default'),
};
} else {
console.error('QueryComboBox can only be used to display a relationship');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function generateForm(
typeSearch: undefined,
searchView: undefined,
isReadOnly: mode === 'view',
defaultRecord: undefined,
},
isRequired: false,
viewName: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function fieldToDefinition(
hasViewButton: false,
typeSearch: undefined,
searchView: undefined,
defaultRecord: undefined,
};
else if (field.type === 'java.lang.Boolean')
return {
Expand Down
58 changes: 58 additions & 0 deletions specifyweb/frontend/js_src/lib/components/QueryComboBox/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,61 @@ export function pendingValueToResource(
typeof fieldName === 'string' ? { [fieldName]: pendingValue } : {}
);
}

const DEFAULT_RECORD_PRESETS = {
CURRENT_AGENT: () => userInformation.agent.resource_uri,
CURRENT_USER: () => userInformation.resource_uri,
BLANK: () => null,
} as const;
type DefaultRecordPreset = keyof typeof DEFAULT_RECORD_PRESETS;

export function useQueryComboBoxDefaults({
resource,
field,
defaultRecord,
}: {
readonly resource: SpecifyResource<AnySchema> | undefined;
readonly field: Relationship;
readonly defaultRecord?: string | undefined;
}): void {
if (resource === undefined || !resource.isNew()) return;

if (defaultRecord !== undefined) {
const defaultUri: string | null =
defaultRecord in DEFAULT_RECORD_PRESETS
? DEFAULT_RECORD_PRESETS[defaultRecord as DefaultRecordPreset]()
: defaultRecord;

resource.set(field.name, resource.get(field.name) ?? defaultUri, {
silent: true,
});
// The following cases need to be kept for outdated forms that do not use the defaultRecord property.
} else if (field.name === 'cataloger') {
const record = toTable(resource, 'CollectionObject');
record?.set(
'cataloger',
record?.get('cataloger') ?? userInformation.agent.resource_uri,
{
silent: true,
}
);
} else if (field.name === 'specifyUser') {
const record = toTable(resource, 'RecordSet');
record?.set(
'specifyUser',
record?.get('specifyUser') ?? userInformation.resource_uri,
{
silent: true,
}
);
} else if (field.name === 'receivedBy') {
const record = toTable(resource, 'LoanReturnPreparation');
record?.set(
'receivedBy',
record?.get('receivedBy') ?? userInformation.agent.resource_uri,
{
silent: true,
}
);
}
}
39 changes: 5 additions & 34 deletions specifyweb/frontend/js_src/lib/components/QueryComboBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { RA } from '../../utils/types';
import { filterArray, localized } from '../../utils/types';
import { DataEntry } from '../Atoms/DataEntry';
import { LoadingContext, ReadOnlyContext } from '../Core/Contexts';
import { backboneFieldSeparator, toTable } from '../DataModel/helpers';
import { backboneFieldSeparator } from '../DataModel/helpers';
import type { AnySchema } from '../DataModel/helperTypes';
import type { SpecifyResource } from '../DataModel/legacyTypes';
import {
Expand Down Expand Up @@ -47,6 +47,7 @@ import {
getRelatedCollectionId,
makeComboBoxQuery,
pendingValueToResource,
useQueryComboBoxDefaults,
} from './helpers';
import type { TypeSearch } from './spec';
import { useCollectionRelationships } from './useCollectionRelationships';
Expand All @@ -72,6 +73,7 @@ export function QueryComboBox({
typeSearch: initialTypeSearch,
forceCollection,
searchView,
defaultRecord,
relatedTable: initialRelatedTable,
}: {
readonly id: string | undefined;
Expand All @@ -87,41 +89,10 @@ export function QueryComboBox({
readonly typeSearch: TypeSearch | string | undefined;
readonly forceCollection: number | undefined;
readonly searchView?: string;
readonly defaultRecord?: string | undefined;
readonly relatedTable?: SpecifyTable | undefined;
}): JSX.Element {
React.useEffect(() => {
if (resource === undefined || !resource.isNew()) return;
if (field.name === 'cataloger') {
const record = toTable(resource, 'CollectionObject');
record?.set(
'cataloger',
record?.get('cataloger') ?? userInformation.agent.resource_uri,
{
silent: true,
}
);
}
if (field.name === 'specifyUser') {
const record = toTable(resource, 'RecordSet');
record?.set(
'specifyUser',
record?.get('specifyUser') ?? userInformation.resource_uri,
{
silent: true,
}
);
}
if (field.name === 'receivedBy') {
const record = toTable(resource, 'LoanReturnPreparation');
record?.set(
'receivedBy',
record?.get('receivedBy') ?? userInformation.agent.resource_uri,
{
silent: true,
}
);
}
}, [resource, field]);
useQueryComboBoxDefaults({ resource, field, defaultRecord });

const treeData = useTreeData(resource, field);
const collectionRelationships = useCollectionRelationships(resource);
Expand Down
Loading