diff --git a/src/chronology/ui/DateEditor/DateSelectionInput.test.tsx b/src/chronology/ui/DateEditor/DateSelectionInput.test.tsx index 03ce73cc7..bcb340d28 100644 --- a/src/chronology/ui/DateEditor/DateSelectionInput.test.tsx +++ b/src/chronology/ui/DateEditor/DateSelectionInput.test.tsx @@ -141,15 +141,15 @@ describe('Date Input Groups', () => { }) ) const yearInput = screen.getByLabelText('Year') - const yearBrokenSwitch = screen.getByTestId('year-broken-switch') - const yearUncertainSwitch = screen.getByTestId('year-uncertain-switch') + const yearBrokenSwitch = screen.getByTestId('0-year-broken-switch') + const yearUncertainSwitch = screen.getByTestId('0-year-uncertain-switch') const monthInput = screen.getByLabelText('Month') const monthIntercalaryCheckbox = screen.getByLabelText('Intercalary') - const monthBrokenSwitch = screen.getByTestId('month-broken-switch') - const monthUncertainSwitch = screen.getByTestId('month-uncertain-switch') + const monthBrokenSwitch = screen.getByTestId('0-month-broken-switch') + const monthUncertainSwitch = screen.getByTestId('0-month-uncertain-switch') const dayInput = screen.getByLabelText('Day') - const dayBrokenSwitch = screen.getByTestId('day-broken-switch') - const dayUncertainSwitch = screen.getByTestId('day-uncertain-switch') + const dayBrokenSwitch = screen.getByTestId('0-day-broken-switch') + const dayUncertainSwitch = screen.getByTestId('0-day-uncertain-switch') expect(yearInput).toBeInTheDocument() expect(yearBrokenSwitch).toBeInTheDocument() diff --git a/src/common/BrokenAndUncertain.tsx b/src/common/BrokenAndUncertain.tsx index 87d404356..b6f8b86fd 100644 --- a/src/common/BrokenAndUncertain.tsx +++ b/src/common/BrokenAndUncertain.tsx @@ -1,8 +1,10 @@ +import _ from 'lodash' import React from 'react' import { Form } from 'react-bootstrap' export interface BrokenUncertainProps { name: string + id?: string | number isBroken?: boolean isUncertain?: boolean setBroken: @@ -13,33 +15,34 @@ export interface BrokenUncertainProps { | ((isUncertain: boolean) => void) } -export function BrokenAndUncertainSwitches({ - name, - isBroken = false, - isUncertain = false, - setBroken, - setUncertain, -}: BrokenUncertainProps): JSX.Element { +export function BrokenAndUncertainSwitches( + props: BrokenUncertainProps +): JSX.Element { return ( <> - setBroken(event.target.checked)} - checked={isBroken} - /> - setUncertain(event.target.checked)} - checked={isUncertain} - /> + {getSwitch('broken', props)} + {getSwitch('uncertain', props)} ) } + +function getSwitch( + type: 'broken' | 'uncertain', + props: BrokenUncertainProps +): JSX.Element { + const { name, id } = props + const onChange = props[`set${_.capitalize(type)}`] + const checked = props[`is${_.capitalize(type)}`] + const _id = id ?? 0 + return ( + onChange(event.target.checked)} + checked={checked} + /> + ) +} diff --git a/src/fragmentarium/domain/Colophon.ts b/src/fragmentarium/domain/Colophon.ts index a15a2651e..750ed3ac5 100644 --- a/src/fragmentarium/domain/Colophon.ts +++ b/src/fragmentarium/domain/Colophon.ts @@ -1,4 +1,5 @@ import produce, { Draft, castDraft, immerable } from 'immer' +import _ from 'lodash' export enum ColophonStatus { Yes = 'Yes', @@ -145,22 +146,53 @@ export class IndividualAttestation { return this?.type?.value ? `${this.type.value}: ` : '' } private get nameString(): string { - return this?.name?.value ?? '' + return this.formatItemString(this.name, '') } private get sonOfString(): string { - return this?.sonOf?.value ? `s. ${this.sonOf.value}` : '' + return this.formatItemString(this.sonOf, 's.') } private get grandsonOfString(): string { - return this?.grandsonOf?.value ? `gs. ${this.grandsonOf.value}` : '' + return this.formatItemString(this.grandsonOf, 'gs.') } private get familyString(): string { - return this?.family?.value ? `f. ${this.family.value}` : '' + return this.formatItemString(this.family, 'f.') } private get nativeOfString(): string { - return this?.nativeOf?.value ? `n. ${this.nativeOf.value}` : '' + return this.formatItemString(this.nativeOf, 'n.') + } + + private formatItemString( + item?: NameAttestation | ProvenanceAttestation, + prefix?: string + ): string { + if (this.isItemEmpty(item)) { + return '' + } + + const prefixString = prefix ? `${prefix} ` : '' + const valueString = this.formatValueString( + item as NameAttestation | ProvenanceAttestation + ) + + return `${prefixString}${valueString}` + } + + private isItemEmpty(item?: NameAttestation | ProvenanceAttestation): boolean { + return !item || _.isEmpty(item) || _.every(item, (val) => val === false) + } + + private formatValueString( + item: NameAttestation | ProvenanceAttestation + ): string { + const value = item.value ?? '…' + const brokenSymbol = item.isBroken ? '[' : '' + const uncertainSymbol = item.isUncertain ? '?' : '' + const closingBrokenSymbol = item.isBroken ? ']' : '' + + return `${brokenSymbol}${value}${uncertainSymbol}${closingBrokenSymbol}` } } diff --git a/src/fragmentarium/ui/fragment/ColophonEditor.test.tsx b/src/fragmentarium/ui/fragment/ColophonEditor.test.tsx index 9d7ab9574..ac756943f 100644 --- a/src/fragmentarium/ui/fragment/ColophonEditor.test.tsx +++ b/src/fragmentarium/ui/fragment/ColophonEditor.test.tsx @@ -125,7 +125,7 @@ describe('ColophonEditor', () => { await userEvent.click(screen.getByText('Add Individual')) await userEvent.click(screen.getByText('Individual 1.')) }) - userEvent.click(screen.getByLabelText('name-broken-switch')) + userEvent.click(screen.getByLabelText('0-name-broken-switch')) const nameInput = screen.getByLabelText('select-colophon-individual-name') userEvent.type(nameInput, 'ba') await selectOption(nameInput, 'Humbaba') diff --git a/src/fragmentarium/ui/fragment/ColophonEditorIndividualForm.tsx b/src/fragmentarium/ui/fragment/ColophonEditorIndividualForm.tsx index 142a64aab..2357a0e2f 100644 --- a/src/fragmentarium/ui/fragment/ColophonEditorIndividualForm.tsx +++ b/src/fragmentarium/ui/fragment/ColophonEditorIndividualForm.tsx @@ -133,6 +133,7 @@ const getIndividualField = ({ const getLocationItems = (colophon: Colophon) => ['originalFrom', 'writtenIn'] .map((key) => [key, colophon[key]?.value]) - .filter(([key, value]) => !!value) + .filter(([, value]) => !!value) .map(([key, value]) => `${_.startCase(key)}: ${value}`) const getTypesItem = (colophon: Colophon) => - colophon?.colophonTypes + colophon?.colophonTypes && colophon?.colophonTypes.length > 0 ? [`Types: ${colophon?.colophonTypes.join(', ')}`] : [] @@ -31,6 +31,15 @@ const ColophonInfo = ({ fragment }: { fragment: Fragment }): JSX.Element => { if (!colophon) { return <> } + const mapToList = (text, index) =>
  • {text}
  • + const individuals = colophon?.individuals && colophon?.individuals.length > 0 && ( +
  • + Individuals:{' '} +
      + {getIndividualsItems(colophon).map(mapToList)} +
    +
  • + ) return (
      @@ -38,10 +47,8 @@ const ColophonInfo = ({ fragment }: { fragment: Fragment }): JSX.Element => { ...getGeneralInfoItems(colophon), ...getTypesItem(colophon), ...getLocationItems(colophon), - ...getIndividualsItems(colophon), - ].map((text, index) => ( -
    • {text}
    • - ))} + ].map(mapToList)} + {individuals}
    ) }