Skip to content

Commit

Permalink
feat(Viewer): Use formatMetadataQualification and METADATA_NAMES from
Browse files Browse the repository at this point in the history
cozy-client

This method and these constants are used in cozy-ui Viewer to display
metadata qualification for a paper. It has been migrated to
cozy-client because it will be used in cozy-keys-browser.

feat: Use formatMetadataQualification
  • Loading branch information
zatteo committed Mar 12, 2024
1 parent c5f2649 commit 450fb74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 141 deletions.
20 changes: 10 additions & 10 deletions react/Viewer/Panel/Qualification.jsx
@@ -1,16 +1,16 @@
import React, { useRef, useState, createRef, useMemo, useEffect } from 'react'
import PropTypes from 'prop-types'

import { isExpiringSoon } from 'cozy-client/dist/models/paper'
import {
isExpiringSoon,
formatMetadataQualification,
KNOWN_DATE_METADATA_NAMES,
KNOWN_INFORMATION_METADATA_NAMES,
KNOWN_OTHER_METADATA_NAMES
} from 'cozy-client/dist/models/paper'

import List from '../../List'
import { withViewerLocales } from '../hoc/withViewerLocales'
import {
formatMetadataQualification,
knownDateMetadataNames,
knownInformationMetadataNames,
knownOtherMetadataNames
} from '../helpers'
import ExpirationAlert from '../components/ExpirationAlert'
import QualificationListItemContact from './QualificationListItemContact'
import ActionMenuWrapper from './ActionMenuWrapper'
Expand All @@ -19,15 +19,15 @@ import QualificationListItemInformation from './QualificationListItemInformation
import QualificationListItemOther from './QualificationListItemOther'

const makeQualificationListItemComp = metadataName => {
if (knownDateMetadataNames.includes(metadataName)) {
if (KNOWN_DATE_METADATA_NAMES.includes(metadataName)) {
return QualificationListItemDate
}

if (knownInformationMetadataNames.includes(metadataName)) {
if (KNOWN_INFORMATION_METADATA_NAMES.includes(metadataName)) {
return QualificationListItemInformation
}

if (knownOtherMetadataNames.includes(metadataName)) {
if (KNOWN_OTHER_METADATA_NAMES.includes(metadataName)) {
if (metadataName === 'contact') {
return QualificationListItemContact
}
Expand Down
89 changes: 6 additions & 83 deletions react/Viewer/helpers.js
Expand Up @@ -8,35 +8,15 @@ const {
normalize
} = models.file

export const knownDateMetadataNames = [
'AObtentionDate',
'BObtentionDate',
'CObtentionDate',
'DObtentionDate',
'obtentionDate',
'expirationDate',
'referencedDate',
'issueDate',
'shootingDate',
'date',
'datetime'
]
export const knownInformationMetadataNames = [
'number',
'bicNumber',
'country',
'refTaxIncome',
'contractType',
'netSocialAmount',
'employerName',
'noticePeriod'
]
export const knownOtherMetadataNames = ['contact', 'page', 'qualification']
const {
KNOWN_DATE_METADATA_NAMES,
KNOWN_INFORMATION_METADATA_NAMES
} = models.paper

export const getCurrentModel = metadataName => {
if (
knownDateMetadataNames.includes(metadataName) ||
knownInformationMetadataNames.includes(metadataName)
KNOWN_DATE_METADATA_NAMES.includes(metadataName) ||
KNOWN_INFORMATION_METADATA_NAMES.includes(metadataName)
) {
return 'information'
}
Expand Down Expand Up @@ -75,63 +55,6 @@ export const downloadFile = async ({ client, file, url }) => {

export const isFileEncrypted = file => isEncrypted(file)

const makeMetadataQualification = ({ metadata, knownMetadataName, value }) => {
const shouldReturnThisMetadata = Object.keys(metadata).includes(
knownMetadataName
)

if (shouldReturnThisMetadata || knownMetadataName === 'contact') {
return { name: knownMetadataName, value: value || null }
}
}

/**
* @param {Object} metadata
* @returns {{ name: string, value: string }[]} Array of formated metadata
*/
export const formatMetadataQualification = metadata => {
const dates = knownDateMetadataNames
.map(dateName =>
makeMetadataQualification({
metadata,
knownMetadataName: dateName,
value: metadata[dateName]
})
)
.filter(Boolean)
.filter((data, _, arr) => {
if (arr.length > 1) return data.name !== 'datetime'
return data
})

const informations = knownInformationMetadataNames
.map(numberName =>
makeMetadataQualification({
metadata,
knownMetadataName: numberName,
value: metadata[numberName]
})
)
.filter(Boolean)

const others = knownOtherMetadataNames
.map(otherName => {
const value =
otherName === 'qualification'
? metadata[otherName]?.label
: metadata[otherName]

return makeMetadataQualification({
metadata,
knownMetadataName: otherName,
value
})
})
.filter(Boolean)

return [...dates, ...informations, ...others]
}

export const formatDate = ({ f, lang, date }) => {
if (lang === 'en') {
return f(date, 'MM/DD/YYYY')
Expand Down
58 changes: 10 additions & 48 deletions react/Viewer/helpers.spec.js
@@ -1,49 +1,17 @@
import { createMockClient } from 'cozy-client'
import {
KNOWN_DATE_METADATA_NAMES,
KNOWN_INFORMATION_METADATA_NAMES
} from 'cozy-client/dist/models/paper'

import {
downloadFile,
formatMetadataQualification,
getCurrentModel,
buildEditAttributePath,
knownDateMetadataNames,
knownInformationMetadataNames,
isEditableAttribute,
removeFilenameFromPath
} from './helpers'

const fakeMetadata = {
number: '111111',
AObtentionDate: '2029-12-01T23:00:00.000Z',
BObtentionDate: '2029-12-02T23:00:00.000Z',
CObtentionDate: '2029-12-03T23:00:00.000Z',
DObtentionDate: '2029-12-04T23:00:00.000Z',
expirationDate: '2029-12-05T23:00:00.000Z',
referencedDate: '2029-12-06T23:00:00.000Z',
issueDate: '2029-12-07T23:00:00.000Z',
shootingDate: '2029-12-08T23:00:00.000Z',
date: '2029-12-09T23:00:00.000Z',
datetime: '2029-12-10T23:00:00.000Z',
qualification: { label: 'fake_label' },
page: 'front',
contact: 'Alice Durand'
}

const computedMetadata = [
{ name: 'AObtentionDate', value: '2029-12-01T23:00:00.000Z' },
{ name: 'BObtentionDate', value: '2029-12-02T23:00:00.000Z' },
{ name: 'CObtentionDate', value: '2029-12-03T23:00:00.000Z' },
{ name: 'DObtentionDate', value: '2029-12-04T23:00:00.000Z' },
{ name: 'expirationDate', value: '2029-12-05T23:00:00.000Z' },
{ name: 'referencedDate', value: '2029-12-06T23:00:00.000Z' },
{ name: 'issueDate', value: '2029-12-07T23:00:00.000Z' },
{ name: 'shootingDate', value: '2029-12-08T23:00:00.000Z' },
{ name: 'date', value: '2029-12-09T23:00:00.000Z' },
{ name: 'number', value: '111111' },
{ name: 'contact', value: 'Alice Durand' },
{ name: 'page', value: 'front' },
{ name: 'qualification', value: 'fake_label' }
]

describe('helpers', () => {
describe('download', () => {
const client = new createMockClient({})
Expand All @@ -68,20 +36,14 @@ describe('helpers', () => {
expect(mockForceFileDownload).toHaveBeenCalledWith(url, file.name)
})
})
describe('computeMetadataQualification', () => {
it('should return correctly formatted metadata', () => {
const res = formatMetadataQualification(fakeMetadata)
expect(res).toEqual(computedMetadata)
})
})
describe('getCurrentModel', () => {
const expected = 'information'
it.each([...knownDateMetadataNames, ...knownInformationMetadataNames])(
`getCurrentModel(%s) should return ${expected}`,
input => {
expect(getCurrentModel(input)).toBe(expected)
}
)
it.each([
...KNOWN_DATE_METADATA_NAMES,
...KNOWN_INFORMATION_METADATA_NAMES
])(`getCurrentModel(%s) should return ${expected}`, input => {
expect(getCurrentModel(input)).toBe(expected)
})
})
describe('buildEditAttributePath', () => {
it('should build correct path', () => {
Expand Down

0 comments on commit 450fb74

Please sign in to comment.