Skip to content

Commit

Permalink
Add colophon factories & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Mar 8, 2024
1 parent d46fb73 commit d1611c7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
59 changes: 26 additions & 33 deletions src/fragmentarium/ui/fragment/ColophonEditorInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Individual,
IndividualType,
} from './ColophonEditor'
import _ from 'lodash'

export const ColophonStatusInput = ({
colophonStatus,
Expand Down Expand Up @@ -118,7 +119,13 @@ export const ColophonIndividualsInput = ({
key: string,
value: any
): void => {
console.log(index, field, key, value)
const updatedIndividuals = [...individuals]
const updatedField = { ...updatedIndividuals[index], [key]: value }
updatedIndividuals[index] = {
...updatedIndividuals[index],
[field]: updatedField,
}
onChange(updatedIndividuals)
}

return (
Expand Down Expand Up @@ -149,40 +156,26 @@ const IndividualInput = ({
onUpdate: (field: keyof Individual, key: string, value: any) => void
onRemove: () => void
}): JSX.Element => {
const inputFields = [
'type',
'name',
'sonOf',
'grandsonOf',
'family',
].map((key) => (
<Form.Control
type="text"
key={key}
placeholder={_.startCase(key)}
value={key === 'type' ? individual.type : individual[key]?.value}
onChange={(event) =>
onUpdate(key as keyof Individual, 'value', event.target.value)
}
/>
))
return (
<Row>
<Col>
<Form.Control
type="text"
placeholder="Type"
value={individual.type}
onChange={(e) => onUpdate('family', 'value', e.target.value)}
/>
<Form.Control
type="text"
placeholder="Name"
value={individual.name?.value ?? ''}
onChange={(e) => onUpdate('name', 'value', e.target.value)}
/>
<Form.Control
type="text"
placeholder="Son of"
value={individual.sonOf?.value}
onChange={(e) => onUpdate('sonOf', 'value', e.target.value)}
/>
<Form.Control
type="text"
placeholder="Grandson of"
value={individual.grandsonOf?.value}
onChange={(e) => onUpdate('grandsonOf', 'value', e.target.value)}
/>
<Form.Control
type="text"
placeholder="Family"
value={individual.family?.value}
onChange={(e) => onUpdate('family', 'value', e.target.value)}
/>
</Col>
<Col>{inputFields}</Col>
<Col xs="auto">
<Button variant="danger" onClick={onRemove}>
Remove
Expand Down
39 changes: 39 additions & 0 deletions src/test-support/colophon-fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Factory } from 'fishery'
import { Chance } from 'chance'
import {
IndividualType,
ColophonStatus,
ColophonOwnership,
ColophonType,
Name,
Individual,
Colophon,
} from 'fragmentarium/ui/fragment/ColophonEditor'
import { Provenances } from 'corpus/domain/provenance'

const chance = new Chance()

const nameFactory = Factory.define<Name>(() => ({
value: chance.name(),
isBroken: chance.bool(),
isUncertain: chance.bool(),
}))

const individualFactory = Factory.define<Individual>(() => ({
name: nameFactory.build(),
sonOf: chance.bool() ? nameFactory.build() : undefined,
grandsonOf: chance.bool() ? nameFactory.build() : undefined,
family: chance.bool() ? nameFactory.build() : undefined,
nativeOf: chance.pickone(Object.values(Provenances)),
type: chance.pickone(Object.values(IndividualType)),
}))

export const colophonFactory = Factory.define<Colophon>(() => ({
colophonStatus: chance.pickone(Object.values(ColophonStatus)),
colophonOwnership: chance.pickone(Object.values(ColophonOwnership)),
colophonType: chance.pickone(Object.values(ColophonType)),
originalFrom: chance.pickone(Object.values(Provenances)),
writtenIn: chance.pickone(Object.values(Provenances)),
notesToScribalProcess: chance.sentence(),
individuals: individualFactory.buildList(chance.integer({ min: 0, max: 5 })),
}))

0 comments on commit d1611c7

Please sign in to comment.