Skip to content

Commit

Permalink
feat(ExpandedAttributes): Change supported attributes and modify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Mar 29, 2023
1 parent a9800df commit 238e359
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
49 changes: 41 additions & 8 deletions react/MuiCozyTheme/ListItem/ExpandedAttributes/helpers.js
Expand Up @@ -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 = {
Expand All @@ -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',
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand All @@ -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 }
})
Expand All @@ -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
}
40 changes: 39 additions & 1 deletion 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', () => {
Expand Down Expand Up @@ -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',
Expand All @@ -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')
})
})

0 comments on commit 238e359

Please sign in to comment.