Skip to content

Commit

Permalink
Refactored fragments.ts and ExternalResources.tsx (#422)
Browse files Browse the repository at this point in the history
* Refactored fragments.ts and ExternalResources.tsx

* update external number calls

* Refactored fragments.ts and ExternalResources.tsk

---------

Co-authored-by: fsimonjetz <[email protected]>
  • Loading branch information
kartikpaliwal and fsimonjetz authored Dec 13, 2023
1 parent c5e53a9 commit 017217f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/fragmentarium/application/FragmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const testData: TestData<FragmentService>[] = [
[fragment],
fragmentRepository.fetchCdliInfo,
resultStub,
[fragment.cdliNumber]
[fragment.getExternalNumber('cdliNumber')]
),
new TestData(
'findAnnotations',
Expand Down
6 changes: 4 additions & 2 deletions src/fragmentarium/application/FragmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ export class FragmentService {
}

fetchCdliInfo(fragment: Fragment): Bluebird<CdliInfo> {
return fragment.cdliNumber
? this.fragmentRepository.fetchCdliInfo(fragment.cdliNumber)
return fragment.getExternalNumber('cdliNumber')
? this.fragmentRepository.fetchCdliInfo(
fragment.getExternalNumber('cdliNumber')
)
: Bluebird.resolve({
photoUrl: null,
lineArtUrl: null,
Expand Down
50 changes: 5 additions & 45 deletions src/fragmentarium/domain/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,63 +234,23 @@ export class Fragment {
})
}

private getExternalNumber(
numberType: Exclude<ExternalNumber, 'oraccNumbers'>
): string {
public getExternalNumber(numberType: ExternalNumber): string {
return this.externalNumbers[numberType] || ''
}

get cdliNumber(): string {
return this.getExternalNumber('cdliNumber')
}
get bmIdNumber(): string {
return this.getExternalNumber('bmIdNumber')
}
get archibabNumber(): string {
return this.getExternalNumber('archibabNumber')
}
get bdtnsNumber(): string {
return this.getExternalNumber('bdtnsNumber')
}
get urOnlineNumber(): string {
return this.getExternalNumber('urOnlineNumber')
}
get hilprechtJenaNumber(): string {
return this.getExternalNumber('hilprechtJenaNumber')
}
get hilprechtHeidelbergNumber(): string {
return this.getExternalNumber('hilprechtHeidelbergNumber')
}
get achemenetNumber(): string {
return this.getExternalNumber('achemenetNumber')
}
get nabuccoNumber(): string {
return this.getExternalNumber('nabuccoNumber')
}
get metropolitanNumber(): string {
return this.getExternalNumber('metropolitanNumber')
}
get louvreNumber(): string {
return this.getExternalNumber('louvreNumber')
}
get philadelphiaNumber(): string {
return this.getExternalNumber('philadelphiaNumber')
}
get yalePeabodyNumber(): string {
return this.getExternalNumber('yalePeabodyNumber')
}
get oraccNumbers(): readonly string[] {
return this.externalNumbers['oraccNumbers'] || []
}
get hasExternalResources(): boolean {
return _.some([
...this.oraccNumbers,
...ExternalNumberTypes.map((number) => this.getExternalNumber(number)),
...ExternalNumberTypes.map((number) =>
this.getExternalNumber(number as ExternalNumber)
),
])
}

get atfHeading(): string {
const cdliNumber = this.cdliNumber || 'X000001'
const cdliNumber = this.getExternalNumber('cdliNumber') || 'X000001'
return `&${cdliNumber} = ${this.number}
#project: eblo
#atf: lang akk-x-stdbab
Expand Down
4 changes: 2 additions & 2 deletions src/fragmentarium/domain/museum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ export default class Museum {

class BritishMuseum extends Museum {
hasFragmentLink(fragment: Fragment) {
return fragment.bmIdNumber !== ''
return fragment.getExternalNumber('bmIdNumber') !== ''
}

createLinkFor(fragment: Fragment): FragmentLink {
if (this.hasFragmentLink(fragment)) {
const bmIdNumber = fragment.bmIdNumber
const bmIdNumber = fragment.getExternalNumber('bmIdNumber')
return {
name: this.name,
logo: this.logo,
Expand Down
17 changes: 9 additions & 8 deletions src/fragmentarium/ui/fragment/CuneiformFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ beforeEach(async () => {
await screen.findAllByText('Photo')
})

test.each(['collection', 'cdliNumber', 'accession'])(
'Renders %s',
(property) => {
expect(container).toHaveTextContent(fragment[property])
}
)
test.each(['collection', 'accession'])('Renders %s', (property) => {
expect(container).toHaveTextContent(fragment[property])
})

it('Renders CDLI number', () => {
expect(container).toHaveTextContent(fragment.getExternalNumber('cdliNumber'))
})

it('Renders museum', () => {
expect(container).toHaveTextContent(fragment.museum.name)
Expand Down Expand Up @@ -161,7 +162,7 @@ it('Updates view on Edition save', async () => {

submitFormByTestId(screen, 'transliteration-form')

await screen.findAllByText(updatedFragment.cdliNumber)
await screen.findAllByText(updatedFragment.getExternalNumber('cdliNumber'))
})

it('Updates view on References save', async () => {
Expand All @@ -172,5 +173,5 @@ it('Updates view on References save', async () => {
await screen.findAllByText('Document')
submitFormByTestId(screen, 'references-form')

await screen.findByText(updatedFragment.cdliNumber)
await screen.findByText(updatedFragment.getExternalNumber('cdliNumber'))
})
3 changes: 1 addition & 2 deletions src/fragmentarium/ui/images/Images.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ it('Displays CDLI photo if no photo and no folio specified', async () => {
test('No photo, folios, CDLI photo', async () => {
fragment = fragmentFactory.build(
{
cdliNumber: '',
hasPhoto: false,
},
{ associations: { folios: [] } }
{ associations: { folios: [], externalNumbers: { cdliNumber: '' } } }
)
fragmentService.fetchCdliInfo.mockReturnValue(
Promise.resolve({
Expand Down
53 changes: 31 additions & 22 deletions src/fragmentarium/ui/info/ExternalResources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,50 @@ import _ from 'lodash'

type ExternalLinkComponent = ({ number }: { number: string }) => JSX.Element

type ExternalLinkTypes = {
number: string
LinkComponent: ExternalLinkComponent
}

const ExternalLink = ({ number, LinkComponent }: ExternalLinkTypes) => {
return number ? <LinkComponent number={number} /> : null
}
export default function ExternalResources({
fragment,
}: {
readonly fragment: Fragment
}): JSX.Element {
const externalNumbers: ReadonlyArray<[string, ExternalLinkComponent]> = [
[fragment.bmIdNumber, BmIdLink],
[fragment.cdliNumber, CdliLink],
[fragment.bdtnsNumber, BdtnsLink],
[fragment.archibabNumber, ArchibabLink],
[fragment.urOnlineNumber, UrOnlineLink],
[fragment.hilprechtJenaNumber, HilprechtJenaLink],
[fragment.hilprechtHeidelbergNumber, HilprechtHeidelbergLink],
[fragment.achemenetNumber, AchemenetLink],
[fragment.nabuccoNumber, NabuccoLink],
[fragment.metropolitanNumber, MetropolitanLink],
[fragment.louvreNumber, LouvreLink],
[fragment.philadelphiaNumber, PhiladelphiaLink],
[fragment.yalePeabodyNumber, YalePeabodyLink],
[fragment.getExternalNumber('bmIdNumber'), BmIdLink],
[fragment.getExternalNumber('cdliNumber'), CdliLink],
[fragment.getExternalNumber('bdtnsNumber'), BdtnsLink],
[fragment.getExternalNumber('archibabNumber'), ArchibabLink],
[fragment.getExternalNumber('urOnlineNumber'), UrOnlineLink],
[fragment.getExternalNumber('hilprechtJenaNumber'), HilprechtJenaLink],
[
fragment.getExternalNumber('hilprechtHeidelbergNumber'),
HilprechtHeidelbergLink,
],
[fragment.getExternalNumber('achemenetNumber'), AchemenetLink],
[fragment.getExternalNumber('nabuccoNumber'), NabuccoLink],
[fragment.getExternalNumber('metropolitanNumber'), MetropolitanLink],
[fragment.getExternalNumber('louvreNumber'), LouvreLink],
[fragment.getExternalNumber('philadelphiaNumber'), PhiladelphiaLink],
[fragment.getExternalNumber('yalePeabodyNumber'), YalePeabodyLink],
]

return (
<ul className="ExternalResources__items">
{externalNumbers.map(
([number, LinkComponent], index) =>
number && (
<li key={index}>
<LinkComponent number={number} />
</li>
)
)}
{externalNumbers.map(([number, LinkComponent], index) => (
<li key={index}>
<ExternalLink number={number} LinkComponent={LinkComponent} />
</li>
))}
{!_.isEmpty(fragment.oraccNumbers) && (
<li>
<OraccLinks
projects={fragment.oraccNumbers}
cdliNumber={fragment.cdliNumber}
cdliNumber={fragment.getExternalNumber('cdliNumber')}
/>
</li>
)}
Expand Down

0 comments on commit 017217f

Please sign in to comment.