Skip to content

Commit

Permalink
Add AfO service, tests & bibliography routes and tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Oct 30, 2023
1 parent 6b090e8 commit bc7e1fe
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 33 deletions.
48 changes: 48 additions & 0 deletions src/afo-register/application/AfoRegisterService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { testDelegation, TestData } from 'test-support/utils'
import AfoRegisterService from 'afo-register/application/AfoRegisterService'
import AfoRegisterRepository from 'afo-register/infrastructure/AfoRegisterRepository'
import { stringify } from 'query-string'

jest.mock('afo-register/infrastructure/AfoRegisterRepository', () => {
return function () {
return {
search: jest.fn(),
}
}
})

const resultStub = {
afoNumber: 'AfO 1',
page: '2',
text: 'some text',
textNumber: '5',
}
const afoRegisterRepository = new (AfoRegisterRepository as jest.Mock)()
const afoRegisterService = new AfoRegisterService(afoRegisterRepository)

const testData: TestData<AfoRegisterService>[] = [
new TestData(
'search',
[
stringify({
afoNumber: 'AfO 1',
page: '2',
}),
],
afoRegisterRepository.search,
[resultStub]
),
new TestData(
'search',
[
stringify({
text: 'some text',
textNumber: '5',
}),
],
afoRegisterRepository.search,
[resultStub]
),
]
describe('afoRegisterService', () =>
testDelegation(afoRegisterService, testData))
19 changes: 19 additions & 0 deletions src/afo-register/application/AfoRegisterService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Promise from 'bluebird'
import AfoRegisterRecord from 'afo-register/domain/Record'
import AfoRegisterRepository from 'afo-register/infrastructure/AfoRegisterRepository'

export interface afoRegisterSearch {
search(query: string): Promise<readonly AfoRegisterRecord[]>
}

export default class AfoRegisterService implements afoRegisterSearch {
private readonly afoRegisterRepository: AfoRegisterRepository

constructor(afoRegisterRepository: AfoRegisterRepository) {
this.afoRegisterRepository = afoRegisterRepository
}

search(query: string): Promise<readonly AfoRegisterRecord[]> {
return this.afoRegisterRepository.search(query)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const testData: TestData<AfoRegisterRepository>[] = [
apiClient.fetchJson,
[entry],
[`/afo-register?${encodeURIComponent(query)}`, false],
Promise.resolve(entry)
Promise.resolve([entry])
),
]

Expand Down
9 changes: 5 additions & 4 deletions src/afo-register/infrastructure/AfoRegisterRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export default class BibliographyRepository {
this.apiClient = apiClient
}

search(query: string): Promise<AfoRegisterRecord> {
return this.apiClient
.fetchJson(`/afo-register?${encodeURIComponent(query)}`, false)
.then((result) => new AfoRegisterRecord(result))
search(query: string): Promise<AfoRegisterRecord[]> {
return this.apiClient.fetchJson(
`/afo-register?${encodeURIComponent(query)}`,
false
)
}
}
85 changes: 69 additions & 16 deletions src/bibliography/ui/Bibliography.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { parse } from 'query-string'
import { LinkContainer } from 'react-router-bootstrap'
import { Button } from 'react-bootstrap'
import { Button, Tab, Tabs } from 'react-bootstrap'
import _ from 'lodash'

import AppContent from 'common/AppContent'
Expand All @@ -10,9 +10,10 @@ import BibliographySearch from './BibliographySearch'
import SessionContext from 'auth/SessionContext'

import './Bibliography.css'
import { SectionCrumb } from 'common/Breadcrumbs'
import { TextCrumb } from 'common/Breadcrumbs'
import { Session } from 'auth/Session'
import { RouteComponentProps } from 'react-router-dom'
import { RouteComponentProps, useHistory } from 'react-router-dom'
import BibliographyService from 'bibliography/application/BibliographyService'

function CreateButton({ session }: { session: Session }): JSX.Element {
return (
Expand All @@ -27,31 +28,83 @@ function CreateButton({ session }: { session: Session }): JSX.Element {
)
}

export default function Bibliography({
function BibliographyReferences({
bibliographyService,
location,
}: {
bibliographyService
bibliographyService: BibliographyService
} & RouteComponentProps): JSX.Element {
const rawQuery = parse(location.search).query || ''
const query = _.isArray(rawQuery) ? rawQuery.join('') : rawQuery
return (
<>
<div className="Bibliography__search">
<BibliographySearchForm query={query} />
</div>
<BibliographySearch
query={query}
bibliographyService={bibliographyService}
/>
</>
)
}

export default function Bibliography({
bibliographyService,
location,
activeTab,
...props
}: {
bibliographyService: BibliographyService
activeTab: 'references' | 'afo-register'
} & RouteComponentProps): JSX.Element {
const history = useHistory()
return (
<SessionContext.Consumer>
{(session: Session): JSX.Element => (
<AppContent
crumbs={[new SectionCrumb('Bibliography')]}
actions={<CreateButton session={session} />}
crumbs={[
new TextCrumb('Bibliography'),
new TextCrumb(
{ 'afo-register': 'AfO Register', references: 'References' }[
activeTab
]
),
]}
actions={
activeTab === 'references' && <CreateButton session={session} />
}
>
{session.isAllowedToReadBibliography() ? (
<>
<div className="Bibliography__search">
<BibliographySearchForm query={query} />
</div>
<BibliographySearch
query={query}
bibliographyService={bibliographyService}
/>
</>
<Tabs
activeKey={activeTab}
onSelect={(eventKey) => {
if (eventKey !== activeTab) {
history.push(`${eventKey}`)
}
}}
id={_.uniqueId('Bibliography-')}
>
<Tab
eventKey={'references'}
title={'References'}
key={'references'}
style={{ paddingTop: '20px' }}
>
<BibliographyReferences
bibliographyService={bibliographyService}
location={location}
{...props}
/>
</Tab>
<Tab
eventKey={'afo-register'}
title={'AfO Register'}
key={'afo-register'}
>
<div>AfO Register Here</div>
</Tab>
</Tabs>
) : (
<p>Please log in to browse the Bibliography.</p>
)}
Expand Down
6 changes: 3 additions & 3 deletions src/bibliography/ui/BibliographySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react'
import { Link } from 'react-router-dom'
import _ from 'lodash'
import { Parser } from 'html-to-react'
import Promise from 'bluebird'

import withData from 'http/withData'

import './BibliographySearch.css'
import BibliographyEntry from 'bibliography/domain/BibliographyEntry'
import BibliographyService from 'bibliography/application/BibliographyService'

function BibliographySearch({ data }: { data: readonly BibliographyEntry[] }) {
const parser = new Parser()
Expand All @@ -16,7 +16,7 @@ function BibliographySearch({ data }: { data: readonly BibliographyEntry[] }) {
{data.map((entry) => (
<li key={entry.id} className="BibliographySearch__entry">
<Link
to={`/bibliography/${encodeURIComponent(entry.id)}`}
to={`/bibliography/references/${encodeURIComponent(entry.id)}`}
className="BibliographySearch__edit"
>
<i className="fas fa-edit" />
Expand All @@ -31,7 +31,7 @@ function BibliographySearch({ data }: { data: readonly BibliographyEntry[] }) {
export default withData<
unknown,
{
bibliographyService: { search(query: string): Promise<BibliographyEntry[]> }
bibliographyService: BibliographyService
query: string
},
readonly BibliographyEntry[]
Expand Down
3 changes: 3 additions & 0 deletions src/fragmentarium/domain/FragmentDtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ interface UncuratedReferenceDto {
pages: readonly number[]
}

type TraditionalReference = string

type RecordEntryDto = Pick<RecordEntry, 'user' | 'date' | 'type'>

type FolioDto = Pick<Folio, 'name' | 'number'>
Expand Down Expand Up @@ -85,6 +87,7 @@ export default interface FragmentDto {
notes: Notes
references: readonly ReferenceDto[]
uncuratedReferences: readonly UncuratedReferenceDto[] | null
traditionalReferences: readonly TraditionalReference[]
atf: string
hasPhoto: boolean
genres: readonly GenreDto[]
Expand Down
1 change: 1 addition & 0 deletions src/fragmentarium/infrastructure/FragmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function createFragment(dto: FragmentDto): Fragment {
text: createTransliteration(dto.text),
references: dto.references.map(createReference),
uncuratedReferences: dto.uncuratedReferences,
traditionalReferences: dto.traditionalReferences,
genres: Genres.fromJson(dto.genres),
script: createScript(dto.script),
projects: dto.projects.map(createResearchProject),
Expand Down
41 changes: 34 additions & 7 deletions src/router/bibliographyRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactNode } from 'react'
import { Route } from 'react-router-dom'
import { Redirect, Route } from 'react-router-dom'
import Bibliography from 'bibliography/ui/Bibliography'
import BibliographyEditor from 'bibliography/ui/BibliographyEditor'
import BibliographyService from 'bibliography/application/BibliographyService'
Expand All @@ -18,7 +18,7 @@ export default function BibliographyRoutes({
return [
<Route
key="BibliographyViewerAndEditor"
path="/bibliography/:id"
path="/bibliography/references/:id"
render={(props): ReactNode => (
<HeadTagsService
title="Bibliography entry: eBL"
Expand Down Expand Up @@ -47,17 +47,44 @@ export default function BibliographyRoutes({
)}
/>,
<Route
key="Bibliography"
path="/bibliography"
key="Bibliography references search"
path="/bibliography/references"
render={(props): ReactNode => (
<HeadTagsService
title="Bibliography: eBL"
description="Bibliography search in the electronic Babylonian Library (eBL)."
title="Bibliography References: eBL"
description="Bibliography references search in the electronic Babylonian Library (eBL)."
>
<Bibliography bibliographyService={bibliographyService} {...props} />
<Bibliography
bibliographyService={bibliographyService}
{...props}
activeTab={'references'}
/>
</HeadTagsService>
)}
{...(sitemap && sitemapDefaults)}
/>,
<Route
key="Bibliography AfO Register search"
path="/bibliography/afo-register"
render={(props): ReactNode => (
<HeadTagsService
title="Bibliography AfO Register: eBL"
description="AfO Register search in the electronic Babylonian Library (eBL)."
>
<Bibliography
bibliographyService={bibliographyService}
{...props}
activeTab={'afo-register'}
/>
</HeadTagsService>
)}
{...(sitemap && sitemapDefaults)}
/>,
<Redirect
from="/bibliography"
to="/bibliography/references"
key="bibliography-root-redirect"
strict={true}
/>,
]
}
1 change: 1 addition & 0 deletions src/test-support/fragment-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export const fragmentFactory = Factory.define<Fragment>(
associations.references ??
referenceFactory.buildList(2, {}, { transient: { chance } }),
associations.uncuratedReferences ?? null,
[],
'',
chance.bool(),
associations.genres ??
Expand Down
2 changes: 2 additions & 0 deletions src/test-support/test-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export const fragmentDto: FragmentDto = {
},
],
uncuratedReferences: null,
traditionalReferences: [],
atf:
'10. sal/: š[im {gu}[...].GA\n10. ::/sal ši]m\n10. šim | šim\n10. ...+ku {KA.G[A} ... ....ku x',
hasPhoto: true,
Expand Down Expand Up @@ -481,6 +482,7 @@ export const fragment = new Fragment(
),
],
null,
[],
'10. sal/: š[im {gu}[...].GA\n10. ::/sal ši]m\n10. šim | šim\n10. ...+ku {KA.G[A} ... ....ku x',
true,
Genres.fromJson([
Expand Down
2 changes: 0 additions & 2 deletions src/test-support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ export function testDelegation<S>(
it('Returns', async () => {
if (result instanceof Bluebird || result instanceof Promise) {
const resolvedResult = await result
console.log('!!!', resolvedResult.afoNumber)
console.log('!11', expectedResult.afoNumber)
await expect(resolvedResult).toEqual(expectedResult)
} else {
expect(result).toEqual(expectedResult)
Expand Down

0 comments on commit bc7e1fe

Please sign in to comment.