From 238e3593ba64986377786556cddf287e978a213b Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Wed, 29 Mar 2023 17:40:17 +0200 Subject: [PATCH] feat(ExpandedAttributes): Change supported attributes and modify logic --- .../ListItem/ExpandedAttributes/helpers.js | 49 ++++++++++++++++--- .../ExpandedAttributes/helpers.spec.js | 40 ++++++++++++++- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.js b/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.js index b20452bacc..ddc1426be6 100644 --- a/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.js +++ b/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.js @@ -2,7 +2,11 @@ import get from 'lodash/get' import { formatDate } from '../../../Viewer/helpers' -export const normalizeExpandedAttribute = attr => attr.split('[]')[0] +export const normalizeExpandedAttribute = attr => + attr + .split('[]')[0] + .replace(':', '.') + .replace('flexsearchProps.', '') // attributes not considered as expanded attributes export const notExpandedAttributes = { @@ -26,10 +30,7 @@ export const defaultExpandedAttributes = { 'metadata.CObtentionDate', 'metadata.DObtentionDate', 'metadata.obtentionDate', - 'metadata.referencedDate', 'metadata.issueDate', - 'metadata.shootingDate', - 'metadata.date', 'metadata.datetime', 'metadata.expirationDate', 'metadata.country', @@ -79,6 +80,7 @@ export const copyToClipboard = ({ value, setAlertProps, t }) => () => { } export const isDate = value => { + if (!isNaN(value)) return false const dateTime = new Date(value).getTime() const dateParsedValue = Date.parse(value) @@ -106,6 +108,19 @@ export const formatAttrValue = ({ attribute, attrValue, f, lang }) => { } } +export const makeAttrKey = (doc, expandedAttribute) => { + switch (true) { + case expandedAttribute === 'metadata.number': + return `${expandedAttribute}.${doc.metadata.qualification.label}` + + case expandedAttribute.match(/\[.+\]/g) !== null: + return expandedAttribute.split('[')[0] + + default: + return expandedAttribute + } +} + export const makeAttrsKeyAndFormatedValue = ({ doc, expandedAttributes, @@ -125,10 +140,7 @@ export const makeAttrsKeyAndFormatedValue = ({ if (!attrFormatedValue) return undefined - const attrKey = - expandedAttribute === 'metadata.number' - ? `${expandedAttribute}.${doc.metadata.qualification.label}` - : expandedAttribute + const attrKey = makeAttrKey(doc, expandedAttribute) return { attrKey, attrFormatedValue } }) @@ -137,3 +149,24 @@ export const makeAttrsKeyAndFormatedValue = ({ return attrsKeyAndFormatedValue } + +export const hasExpandedAttributesDisplayed = ({ + doc, + expandedAttributes, + f, + lang +}) => { + const defaultExpandedAttributes = makeDefaultExpandedAttributes( + doc, + expandedAttributes + ) + + const attrsKeyAndFormatedValue = makeAttrsKeyAndFormatedValue({ + doc, + expandedAttributes: defaultExpandedAttributes, + f, + lang + }) + + return attrsKeyAndFormatedValue?.length > 0 || false +} diff --git a/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.spec.js b/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.spec.js index a0daf98dd4..c577f7e9bd 100644 --- a/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.spec.js +++ b/react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.spec.js @@ -1,7 +1,8 @@ -import { formatAttrValue } from './helpers' +import { formatAttrValue, makeAttrKey } from './helpers' const f = () => 'someMockedDate' const lang = 'en' +const doc = { metadata: { qualification: { label: 'qualifLabel' } } } describe('formatAttrValue', () => { it('should return primary formattedAddress from addresses', () => { @@ -84,6 +85,17 @@ describe('formatAttrValue', () => { expect(res).toBe(12345) }) + it('should return a number for a number value', () => { + const res = formatAttrValue({ + attribute: 'metadata.number', + attrValue: '12345', + f, + lang + }) + + expect(res).toBe('12345') + }) + it('should return a date for an ISO string formated date', () => { const res = formatAttrValue({ attribute: 'metadata.date', @@ -95,3 +107,29 @@ describe('formatAttrValue', () => { expect(res).toBe('someMockedDate') }) }) + +describe('makeAttrKey', () => { + it('should return email', () => { + const res = makeAttrKey(doc, 'email[0].address') + + expect(res).toBe('email') + }) + + it('should return phone', () => { + const res = makeAttrKey(doc, 'phone[1].number') + + expect(res).toBe('phone') + }) + + it('should return metadata.number.qualifLabel', () => { + const res = makeAttrKey(doc, 'metadata.number') + + expect(res).toBe('metadata.number.qualifLabel') + }) + + it('should return the attribute', () => { + const res = makeAttrKey(doc, 'civility') + + expect(res).toBe('civility') + }) +})