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

UISACQCOMP-231: Display system change label for version cards #836

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Move reusable version history components to the ACQ lib. Refs UISACQCOMP-230.
* Move reusable helper function to support version history functionality. Refs UISACQCOMP-232.
* Add `useDebouncedQuery` hook to fix endless request for `DynamicSelection` component. Refs UISACQCOMP-233.
* Display system change label for version cards. Refs UISACQCOMP-231.

## [6.0.1](https://github.com/folio-org/stripes-acq-components/tree/v6.0.1) (2024-11-14)
[Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v6.0.0...v6.0.1)
Expand Down
75 changes: 47 additions & 28 deletions lib/VersionHistory/VersionCard/VersionCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import css from './VersionCard.css';
const itemFormatter = (item, i) => (<li key={i} className={css.changedRecord}>{item}</li>);

const VersionCard = ({
changedFields,
changedFields = [],
id,
isCurrent,
isLatest,
isSystemChange = false,
onSelect,
source,
title,
Expand Down Expand Up @@ -62,6 +63,49 @@ const VersionCard = ({
</Tooltip>
), [id, isCurrent, onSelectVersion, tooltip]);

const message = useMemo(() => {
if (isSystemChange) {
return (
<>
<FormattedMessage
id="stripes-acq-components.versionHistory.card.changed"
tagName="b"
/>
<FormattedMessage
id="stripes-acq-components.versionHistory.card.systemChange"
tagName="div"
/>
</>
);
}

if (changedFields.length) {
return (
<>
<FormattedMessage
id="stripes-acq-components.versionHistory.card.changed"
tagName="b"
/>
<List
items={uniq(changedFields)}
itemFormatter={itemFormatter}
listClass={css.changedRecordsList}
listStyle="bullets"
/>
</>
);
}

return (
<b>
<FormattedMessage
id="stripes-acq-components.versionHistory.card.version.original"
tagName="em"
/>
</b>
);
}, [changedFields, isSystemChange]);

return (
<Card
id={`version-card-${id}`}
Expand All @@ -84,33 +128,7 @@ const VersionCard = ({
</span>
)}

<span>
{
changedFields
? (
<>
<FormattedMessage
id="stripes-acq-components.versionHistory.card.changed"
tagName="b"
/>
<List
items={uniq(changedFields)}
itemFormatter={itemFormatter}
listClass={css.changedRecordsList}
listStyle="bullets"
/>
</>
)
: (
<b>
<FormattedMessage
id="stripes-acq-components.versionHistory.card.version.original"
tagName="em"
/>
</b>
)
}
</span>
<span>{message}</span>
</Card>
);
};
Expand All @@ -120,6 +138,7 @@ VersionCard.propTypes = {
id: PropTypes.string.isRequired,
isCurrent: PropTypes.bool,
isLatest: PropTypes.bool,
isSystemChange: PropTypes.bool,
onSelect: PropTypes.func.isRequired,
source: PropTypes.node.isRequired,
title: PropTypes.node.isRequired,
Expand Down
6 changes: 6 additions & 0 deletions lib/VersionHistory/VersionCard/VersionCard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('VersionCard', () => {
expect(screen.getByText(defaultProps.title)).toBeInTheDocument();
});

it('should display version history card with system update', () => {
renderVersionCard({ isSystemChange: true });

expect(screen.getByText(/systemChange/)).toBeInTheDocument();
});

it('should call \'onSelect\' when \'View this version\' icon button was clicked', async () => {
renderVersionCard();

Expand Down
17 changes: 9 additions & 8 deletions lib/VersionHistory/VersionHistoryPane/VersionHistoryPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const VersionHistoryPane = ({
isLoading: isLoadingProp,
onClose,
onSelectVersion,
hiddenFields,
hiddenFields = [],
systemUpdatedFields = [],
labelsMap,
versions,
}) => {
Expand Down Expand Up @@ -54,15 +55,16 @@ const VersionHistoryPane = ({
const versionCards = useMemo(() => (
versionsToDisplay.map(({
id: versionId,
actionDate,
eventDate,
userId,
}, i) => {
const user = usersMap[userId];
const changedFields = getFieldLabels(
const { changedFields, isSystemChange } = getFieldLabels(
intl,
versionContext?.versionsMap?.[versionId]?.paths,
labelsMap,
hiddenFields,
systemUpdatedFields,
);

const source = (
Expand All @@ -83,9 +85,10 @@ const VersionHistoryPane = ({
isCurrent={versionId === currentVersion}
isLatest={i === 0}
onSelect={onSelectVersion}
title={<FolioFormattedTime dateString={actionDate} />}
title={<FolioFormattedTime dateString={eventDate} />}
source={source}
changedFields={changedFields}
isSystemChange={isSystemChange}
/>
);
})
Expand All @@ -96,6 +99,7 @@ const VersionHistoryPane = ({
hiddenFields,
labelsMap,
onSelectVersion,
systemUpdatedFields,
usersMap,
versionContext?.versionsMap,
versionsToDisplay,
Expand Down Expand Up @@ -140,11 +144,8 @@ VersionHistoryPane.propTypes = {
onSelectVersion: PropTypes.func.isRequired,
labelsMap: PropTypes.object.isRequired,
hiddenFields: PropTypes.arrayOf(PropTypes.string),
systemUpdatedFields: PropTypes.arrayOf(PropTypes.string),
versions: PropTypes.arrayOf(PropTypes.object).isRequired,
};

VersionHistoryPane.defaultProps = {
hiddenFields: [],
};

export default memo(VersionHistoryPane);
16 changes: 16 additions & 0 deletions lib/VersionHistory/VersionHistoryPane/VersionHistoryPane.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ describe('VersionHistoryPane', () => {
expect(screen.getByText('stripes-acq-components.fundDistribution.value')).toBeInTheDocument();
});

it('should display "System maintenance update" label if there is only system changes', () => {
const clonedAuditEvent = Object.assign(cloneDeep(orderLineAuditEvent), { id: TEST_ID });

set(clonedAuditEvent.orderLineSnapshot, 'searchLocationId[0]', 'locationId');

renderVersionHistoryPane({
versions: [
clonedAuditEvent,
orderLineAuditEvent,
],
systemUpdatedFields: ['searchLocationId'],
});

expect(screen.getByText(/systemChange/)).toBeInTheDocument();
});

it('should not display version cards without changed fields', () => {
const clonedAuditEvent = Object.assign(cloneDeep(orderLineAuditEvent), { id: TEST_ID });

Expand Down
20 changes: 14 additions & 6 deletions lib/VersionHistory/getFieldLabels.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { escapeRegExp, keyBy } from 'lodash';

export const getFieldLabels = (intl, paths, labelsMap = {}, hiddenFields = []) => {
if (!paths) return null;
export const getFieldLabels = (intl, paths, labelsMap = {}, hiddenFields = [], systemUpdatedFields = []) => {
if (!paths) return { changedFields: [], systemChanges: [] };

const labelsMapEntries = Object.entries(labelsMap);
const hiddenFieldsMap = keyBy(hiddenFields);

return paths.reduce((acc, path) => {
const { changedFields, systemChanges } = paths.reduce((acc, path) => {
const fieldLabel = labelsMapEntries.find(([fieldPath]) => {
const regex = new RegExp(`^${escapeRegExp(fieldPath).replaceAll('\\\\d', '\\d')}$`);

return regex.test(path);
})?.[1] || path;

if (!hiddenFieldsMap[fieldLabel]) {
acc.push(intl.formatMessage({ id: fieldLabel }));
if (systemUpdatedFields.includes(path.replace(/\[\d+\]/g, ''))) {
acc.systemChanges.push(fieldLabel);
} else if (!hiddenFieldsMap[fieldLabel]) {
acc.changedFields.push(intl.formatMessage({ id: fieldLabel }));
}

return acc;
}, []).sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
}, { changedFields: [], systemChanges: [] });

return {
changedFields: changedFields.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())),
systemChanges,
isSystemChange: !changedFields.length && systemChanges.length,
};
};
17 changes: 15 additions & 2 deletions lib/VersionHistory/getFieldLabels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ const paths = [
const intl = { formatMessage: ({ id }) => id };

describe('getFieldLabels', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should use keys of labelsMap as regexp to parse labels of paths', () => {
expect(getFieldLabels(intl, paths, labelsMap)).toEqual(expect.arrayContaining([
expect(getFieldLabels(intl, paths, labelsMap).changedFields).toEqual(expect.arrayContaining([
...Object.values(labelsMap),
]));
});
Expand All @@ -28,9 +32,18 @@ describe('getFieldLabels', () => {
};

const pathsWithHiddenFields = [...paths, hiddenFields.donors];
const result = getFieldLabels(intl, pathsWithHiddenFields, labelsMap, hiddenFields);

expect(getFieldLabels(intl, pathsWithHiddenFields, labelsMap, hiddenFields)).toEqual(expect.arrayContaining([
expect(result.changedFields).toEqual(expect.arrayContaining([
...Object.values(labelsMap),
]));
});

it('should isSystemChange equal `true` if there are only systemChanges', () => {
const systemUpdatedFields = ['fundDistribution'];

const result = getFieldLabels(intl, ['fundDistribution'], labelsMap, [], systemUpdatedFields);

expect(result.isSystemChange).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion lib/VersionHistory/getVersionMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import get from 'lodash/get';
export const getVersionMetadata = (version, entity) => ({
...get(entity, 'metadata', {}),
updatedByUserId: version?.userId,
updatedDate: version?.actionDate,
updatedDate: version?.eventDate,
});
2 changes: 1 addition & 1 deletion lib/VersionHistory/getVersionMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('getVersionMetadata', () => {
expect(getVersionMetadata(version, entity)).toEqual({
metadataKey: 'metadataValue',
updatedByUserId: 'userId',
updatedDate: version.actionDate,
updatedDate: version.eventDate,
...entity.metadata,
});
});
Expand Down
1 change: 1 addition & 0 deletions translations/stripes-acq-components/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
"validation.shouldBeGreaterThan": "Value must be greater than or equal to {minNumber}",

"versionHistory.card.changed": "Changed",
"versionHistory.card.systemChange": "System maintenance update",
"versionHistory.card.select.tooltip": "View this version",
"versionHistory.card.version.current": "Current version",
"versionHistory.card.version.original": "Original version",
Expand Down
Loading