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 14 commits into
base: production
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ const isDarkMode = ({
}: PreferencesVisibilityContext): boolean => isDarkMode || isRedirecting;

// Navigator may not be defined in some environments, like non-browser environments
const altKeyName = typeof navigator !== 'undefined' && navigator?.userAgent?.includes('Mac')
? 'Option'
: 'Alt';
const altKeyName =
typeof navigator !== 'undefined' && navigator?.userAgent?.includes('Mac')
? 'Option'
: 'Alt';

/**
* Have to be careful as preferences may be used before schema is loaded
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,
}
);
}
}
86 changes: 30 additions & 56 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 Expand Up @@ -264,29 +235,32 @@ export function QueryComboBox({
(typeof typeSearch === 'object' ? typeSearch?.table : undefined) ??
field.relatedTable;

const [fetchedTreeDefinition] = useAsyncState(
React.useCallback(async () => {
if (resource?.specifyTable === tables.Determination) {
return resource.collection?.related?.specifyTable === tables.CollectionObject
? (resource.collection?.related as SpecifyResource<CollectionObject>)
.rgetPromise('collectionObjectType')
.then(
(
collectionObjectType:
| SpecifyResource<CollectionObjectType>
| undefined
) => collectionObjectType?.get('taxonTreeDef')
)
: undefined;
} else if (resource?.specifyTable === tables.Taxon) {
const definition = resource.get('definition')
const parentDefinition = (resource?.independentResources?.parent as SpecifyResource<AnySchema>)?.get?.('definition');
return definition || parentDefinition;
const [fetchedTreeDefinition] = useAsyncState(
React.useCallback(async () => {
if (resource?.specifyTable === tables.Determination) {
return resource.collection?.related?.specifyTable ===
tables.CollectionObject
? (resource.collection?.related as SpecifyResource<CollectionObject>)
.rgetPromise('collectionObjectType')
.then(
(
collectionObjectType:
| SpecifyResource<CollectionObjectType>
| undefined
) => collectionObjectType?.get('taxonTreeDef')
)
: undefined;
} else if (resource?.specifyTable === tables.Taxon) {
const definition = resource.get('definition');
const parentDefinition = (
resource?.independentResources?.parent as SpecifyResource<AnySchema>
)?.get?.('definition');
return definition || parentDefinition;
}
return undefined;
}, [resource, resource?.collection?.related?.get('collectionObjectType')]),
false
);
return undefined;
}, [resource, resource?.collection?.related?.get('collectionObjectType')]),
false
);

// Tree Definition passed by a parent QCBX in the component tree
const parentTreeDefinition = React.useContext(TreeDefinitionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export function SwitchCollectionCommand(): null {
body: collectionId!.toString(),
errorMode: 'dismissible',
})
.then(clearAllCache)
.then(() => globalThis.location.replace(nextUrl)),
.then(clearAllCache)
.then(() => globalThis.location.replace(nextUrl)),
[collectionId, nextUrl]
),
true
Expand Down
Loading