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

UILD-410: fixes for Sonar maintainability issues #41

Merged
merged 7 commits into from
Nov 25, 2024
Merged
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
7 changes: 7 additions & 0 deletions src/common/constants/uiControls.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ export enum SchemaControlType {
export const UI_CONTROLS_LIST = [AdvancedFieldType.literal, AdvancedFieldType.simple, AdvancedFieldType.complex];

export const UI_DROPDOWNS_LIST = [AdvancedFieldType.dropdown, AdvancedFieldType.dropdownOption];

export const NOT_PREVIEWABLE_TYPES = [
AdvancedFieldType.profile,
AdvancedFieldType.hidden,
AdvancedFieldType.dropdownOption,
AdvancedFieldType.complex,
];
71 changes: 71 additions & 0 deletions src/common/helpers/record.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { AUTOCLEAR_TIMEOUT } from '@common/constants/storage.constants';
import { localStorageService } from '@common/services/storage';
import { generateRecordBackupKey } from './progressBackup.helper';
import {
GROUP_BY_LEVEL,
GROUP_CONTENTS_LEVEL,
IDENTIFIER_AS_VALUE,
PROFILE_BFIDS,
TITLE_CONTAINER_URIS,
Expand All @@ -12,6 +14,8 @@ import { BFLITE_URI_TO_BLOCK, BFLITE_URIS, BLOCKS_BFLITE } from '@common/constan
import { ResourceType } from '@common/constants/record.constants';
import { QueryParams } from '@common/constants/routes.constants';
import { cloneDeep } from 'lodash';
import { NOT_PREVIEWABLE_TYPES, AdvancedFieldType } from '@common/constants/uiControls.constants';
import { PREVIEW_ALT_DISPLAY_LABELS } from '@common/constants/uiElements.constants';

type IGetAdjustedRecordContents = {
record: RecordEntry;
Expand Down Expand Up @@ -203,3 +207,70 @@ export const checkIfRecordHasDependencies = (record: RecordEntry) => {
export const getRecordPropertyData = (property: string[] | string) => {
return Array.isArray(property) ? property[0] : property;
};

export const getPreviewFieldsConditions = ({
entry,
level,
userValues,
uuid,
schema,
isOnBranchWithUserValue,
altDisplayNames,
hideActions,
isEntity,
forceRenderAllTopLevelEntities,
}: {
entry: SchemaEntry;
level: number;
userValues: UserValues;
uuid: string;
schema: Schema;
isOnBranchWithUserValue: boolean;
altDisplayNames?: Record<string, string>;
hideActions?: boolean;
isEntity: boolean;
forceRenderAllTopLevelEntities?: boolean;
}) => {
const { displayName = '', children, type, bfid = '', linkedEntry } = entry;

const isPreviewable = !NOT_PREVIEWABLE_TYPES.includes(type as AdvancedFieldType);
const isGroupable = level <= GROUP_BY_LEVEL;
const hasChildren = children?.length;
const selectedUserValues = userValues[uuid];
const isBranchEnd = !hasChildren;
const isBranchEndWithoutValues = !selectedUserValues && isBranchEnd;
const isBranchEndWithValues = !!selectedUserValues;
const shouldRenderLabelOrPlaceholders =
(isPreviewable && isGroupable) ||
type === AdvancedFieldType.dropdown ||
(isBranchEndWithValues && type !== AdvancedFieldType.complex) ||
isBranchEndWithoutValues;
const hasOnlyDropdownChildren =
hasChildren &&
!children.filter(childUuid => schema.get(childUuid)?.type !== AdvancedFieldType.dropdownOption).length;
const shouldRenderValuesOrPlaceholders = !hasChildren || hasOnlyDropdownChildren;
const shouldRenderPlaceholders =
(isPreviewable && isGroupable && !isOnBranchWithUserValue) || !isOnBranchWithUserValue;
const isDependentDropdown = type === AdvancedFieldType.dropdown && !!linkedEntry?.controlledBy;
const displayNameWithAltValue =
altDisplayNames?.[displayName] ?? PREVIEW_ALT_DISPLAY_LABELS[displayName] ?? displayName;
const isBlock = level === GROUP_BY_LEVEL && shouldRenderLabelOrPlaceholders;
const isBlockContents = level === GROUP_CONTENTS_LEVEL;
const isInstance = bfid === PROFILE_BFIDS.INSTANCE;
const showEntityActions = !hideActions && isEntity;
const wrapEntities = forceRenderAllTopLevelEntities && isEntity;

return {
isGroupable,
shouldRenderLabelOrPlaceholders,
shouldRenderValuesOrPlaceholders,
shouldRenderPlaceholders,
isDependentDropdown,
displayNameWithAltValue,
isBlock,
isBlockContents,
isInstance,
showEntityActions,
wrapEntities,
};
};
152 changes: 152 additions & 0 deletions src/components/EditSection/DrawComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { FC } from 'react';
import { FormattedMessage } from 'react-intl';
import { GROUP_COMPLEX_CUTOFF_LEVEL } from '@common/constants/bibframe.constants';
import { AdvancedFieldType } from '@common/constants/uiControls.constants';
import { EDIT_ALT_DISPLAY_LABELS } from '@common/constants/uiElements.constants';
import { findParentEntryByProperty } from '@common/helpers/schema.helper';
import { Button, ButtonType } from '@components/Button';
import { ComplexLookupField } from '@components/ComplexLookupField';
import { DropdownField } from '@components/DropdownField';
import { FieldWithMetadataAndControls } from '@components/FieldWithMetadataAndControls';
import { LiteralField } from '@components/LiteralField';
import { SimpleLookupField } from '@components/SimpleLookupField';
import { EditSectionDataProps } from './renderDrawComponent';

export type IDrawComponent = {
schema: Map<string, SchemaEntry>;
entry: SchemaEntry;
disabledFields?: Schema;
level?: number;
isCompact?: boolean;
};

export const DrawComponent: FC<IDrawComponent & EditSectionDataProps> = ({
schema,
entry,
disabledFields,
level = 0,
isCompact = false,
selectedEntriesService,
selectedEntries,
setSelectedEntries,
userValues,
collapsedEntries,
collapsibleEntries,
onChange,
handleGroupsCollapseExpand,
}) => {
const { uuid, displayName = '', type, children, constraints } = entry;
const isDisabled = !!disabledFields?.get(uuid);
const displayNameWithAltValue = EDIT_ALT_DISPLAY_LABELS[displayName] || displayName;
const selectedUserValue = userValues[uuid];

if (type === AdvancedFieldType.block) {
return (
<FieldWithMetadataAndControls
entry={entry}
level={level}
isCompact={isCompact}
showLabel={false}
className="entity-heading"
>
<strong className="heading">{displayNameWithAltValue}</strong>
{!!collapsibleEntries.size && (
<Button className="toggle-expansion-button" type={ButtonType.Link} onClick={handleGroupsCollapseExpand}>
<FormattedMessage id={collapsedEntries.size ? 'ld.expandAll' : 'ld.collapseAll'} />
</Button>
)}
</FieldWithMetadataAndControls>
);
}

if (
(type === AdvancedFieldType.group || type === AdvancedFieldType.groupComplex) &&
level < GROUP_COMPLEX_CUTOFF_LEVEL
) {
const isComplexGroup = type === AdvancedFieldType.groupComplex;

return (
<FieldWithMetadataAndControls entry={entry} level={level} isCompact={isCompact} showLabel={isComplexGroup}>
{!isComplexGroup && <span className="group-label">{displayNameWithAltValue}</span>}
</FieldWithMetadataAndControls>
);
}

if (type === AdvancedFieldType.literal) {
return (
<FieldWithMetadataAndControls entry={entry} level={level} isCompact={isCompact}>
<LiteralField
uuid={uuid}
value={selectedUserValue?.contents[0].label}
onChange={onChange}
isDisabled={isDisabled}
/>
</FieldWithMetadataAndControls>
);
}

if (type === AdvancedFieldType.dropdown && children) {
const options = children
.map(id => schema.get(id))
.map(entry => ({
label: entry?.displayName ?? '',
value: entry?.uri ?? '', // TBD
uri: entry?.uri ?? '',
id: entry?.uuid,
}));

const selectedOption = options?.find(({ id }) => id && selectedEntries.includes(id));

const handleChange = (option: any) => {
selectedEntriesService.addNew(selectedOption?.id, option.id);

setSelectedEntries(selectedEntriesService.get());
};

return (
<FieldWithMetadataAndControls entry={entry} level={level} isCompact={isCompact}>
<DropdownField
options={options}
uuid={uuid}
onChange={handleChange}
value={selectedOption}
isDisabled={isDisabled || entry?.layout?.readOnly}
/>
</FieldWithMetadataAndControls>
);
}

if (type === AdvancedFieldType.simple) {
const blockEntry = findParentEntryByProperty({
schema,
path: entry.path,
key: 'type',
value: AdvancedFieldType.block,
});

return (
<FieldWithMetadataAndControls entry={entry} level={level} isCompact={isCompact}>
<SimpleLookupField
uri={constraints?.useValuesFrom[0] ?? ''}
uuid={uuid}
onChange={onChange}
parentUri={constraints?.valueDataType?.dataTypeURI}
value={selectedUserValue?.contents}
isDisabled={isDisabled}
propertyUri={entry.uri}
parentBlockUri={blockEntry?.uriBFLite}
/>
</FieldWithMetadataAndControls>
);
}

if (type === AdvancedFieldType.complex) {
return (
<FieldWithMetadataAndControls entry={entry} level={level} isCompact={isCompact}>
<ComplexLookupField entry={entry} onChange={onChange} value={selectedUserValue?.contents} />
</FieldWithMetadataAndControls>
);
}

return null;
};
Loading
Loading