diff --git a/react/Viewer/Panel/Qualification.jsx b/react/Viewer/Panel/Qualification.jsx index 70736dd1f..bc4941336 100644 --- a/react/Viewer/Panel/Qualification.jsx +++ b/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' @@ -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 } diff --git a/react/Viewer/helpers.js b/react/Viewer/helpers.js index 59157db2e..db2ac9c30 100644 --- a/react/Viewer/helpers.js +++ b/react/Viewer/helpers.js @@ -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' } @@ -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') diff --git a/react/Viewer/helpers.spec.js b/react/Viewer/helpers.spec.js index aeeb7af54..410ddcf16 100644 --- a/react/Viewer/helpers.spec.js +++ b/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({}) @@ -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', () => {