Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/276 avoid multiple api calls #362

Merged
merged 11 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/dataset/domain/repositories/DatasetRepository.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Dataset } from '../models/Dataset'
import { TotalDatasetsCount } from '../models/TotalDatasetsCount'
import { DatasetPaginationInfo } from '../models/DatasetPaginationInfo'
import { DatasetPreview } from '../models/DatasetPreview'
import { DatasetDTO } from '../useCases/DTOs/DatasetDTO'
import { DatasetsWithCount } from '../models/DatasetsWithCount'

export interface DatasetRepository {
getByPersistentId: (persistentId: string, version?: string) => Promise<Dataset | undefined>
getByPrivateUrlToken: (privateUrlToken: string) => Promise<Dataset | undefined>
getAll: (collectionId: string, paginationInfo: DatasetPaginationInfo) => Promise<DatasetPreview[]>
getTotalDatasetsCount: (collectionId: string) => Promise<TotalDatasetsCount>
create: (dataset: DatasetDTO) => Promise<{ persistentId: string }>
getAllWithCount: (
collectionId: string,
Expand Down
13 changes: 0 additions & 13 deletions src/dataset/domain/useCases/getDatasets.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/dataset/domain/useCases/getTotalDatasetsCount.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import {
WriteError
} from '@iqss/dataverse-client-javascript'
import { JSDatasetMapper } from '../mappers/JSDatasetMapper'
import { TotalDatasetsCount } from '../../domain/models/TotalDatasetsCount'
import { DatasetPaginationInfo } from '../../domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../domain/models/DatasetPreview'
import { JSDatasetPreviewMapper } from '../mappers/JSDatasetPreviewMapper'
import { DatasetDTO } from '../../domain/useCases/DTOs/DatasetDTO'
import { DatasetDTOMapper } from '../mappers/DatasetDTOMapper'
Expand All @@ -33,25 +31,6 @@ import { DatasetsWithCount } from '../../domain/models/DatasetsWithCount'
const includeDeaccessioned = true

export class DatasetJSDataverseRepository implements DatasetRepository {
getAll(collectionId: string, paginationInfo: DatasetPaginationInfo): Promise<DatasetPreview[]> {
return getAllDatasetPreviews
.execute(paginationInfo.pageSize, paginationInfo.offset, collectionId)
.then((subset: DatasetPreviewSubset) => {
return subset.datasetPreviews.map((datasetPreview: JSDatasetPreview) =>
JSDatasetPreviewMapper.toDatasetPreview(datasetPreview)
)
})
}

getTotalDatasetsCount(collectionId: string): Promise<TotalDatasetsCount> {
// TODO: refactor this so we don't make the same call twice?
return getAllDatasetPreviews
.execute(10, 0, collectionId)
.then((subset: DatasetPreviewSubset) => {
return subset.totalDatasetCount
})
}

getAllWithCount(
collectionId: string,
paginationInfo: DatasetPaginationInfo
Expand Down
62 changes: 23 additions & 39 deletions src/sections/collection/datasets-list/useDatasets.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useEffect, useState } from 'react'
import { DatasetRepository } from '../../../dataset/domain/repositories/DatasetRepository'
import { getDatasets } from '../../../dataset/domain/useCases/getDatasets'
import { getTotalDatasetsCount } from '../../../dataset/domain/useCases/getTotalDatasetsCount'
import { getDatasetsWithCount } from '../../../dataset/domain/useCases/getDatasetsWithCount'
import { TotalDatasetsCount } from '../../../dataset/domain/models/TotalDatasetsCount'
import { DatasetPaginationInfo } from '../../../dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../../dataset/domain/models/DatasetPreview'
import { DatasetsWithCount } from '../../../dataset/domain/models/DatasetsWithCount'

export function useDatasets(
datasetRepository: DatasetRepository,
Expand All @@ -17,52 +17,36 @@ export function useDatasets(
const [isLoading, setIsLoading] = useState<boolean>(true)
const [totalDatasetsCount, setTotalDatasetsCount] = useState<TotalDatasetsCount>()

const fetchTotalDatasetsCount: () => Promise<TotalDatasetsCount> = () => {
return getTotalDatasetsCount(datasetRepository, collectionId)
.then((totalDatasetsCount: TotalDatasetsCount) => {
const fetchDatasetsWithCount = () => {
return getDatasetsWithCount(datasetRepository, collectionId, paginationInfo).then(
(datasetsWithCount: DatasetsWithCount) => {
setTotalDatasetsCount(totalDatasetsCount)
if (datasetsWithCount.totalCount === 0) {
setIsLoading(false)
return Promise.resolve()
}
if (totalDatasetsCount !== paginationInfo.totalItems) {
paginationInfo = paginationInfo.withTotal(totalDatasetsCount)
paginationInfo = paginationInfo.withTotal(datasetsWithCount.totalCount)
onPaginationInfoChange(paginationInfo)

if (paginationInfo.page > paginationInfo.totalPages) {
setPageNumberNotFound(true)
setIsLoading(false)
return Promise.resolve()
}
setDatasets(datasetsWithCount.datasetPreviews)
setIsLoading(false)
}
return totalDatasetsCount
})
.catch(() => {
throw new Error('There was an error getting the datasets count info')
})
}
const fetchDatasets = (totalDatasetsCount: TotalDatasetsCount) => {
if (typeof totalDatasetsCount !== 'undefined') {
if (totalDatasetsCount === 0) {
setIsLoading(false)
return
}
if (paginationInfo.page > paginationInfo.totalPages) {
setPageNumberNotFound(true)
setIsLoading(false)
return
}
return getDatasets(datasetRepository, collectionId, paginationInfo)
.then((datasets: DatasetPreview[]) => {
setDatasets(datasets)
setIsLoading(false)
return datasets
})
.catch(() => {
throw new Error('There was an error getting the datasets')
})
}
)
}

useEffect(() => {
setIsLoading(true)

fetchTotalDatasetsCount()
.then((totalDatasetsCount) => fetchDatasets(totalDatasetsCount))
.catch(() => {
console.error('There was an error getting the datasets')
setIsLoading(false)
})
fetchDatasetsWithCount().catch(() => {
console.error('There was an error getting the datasets')
setIsLoading(false)
})
}, [datasetRepository, paginationInfo.page])

return {
Expand Down
18 changes: 0 additions & 18 deletions src/stories/dataset/DatasetErrorMockRepository.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { Dataset } from '../../dataset/domain/models/Dataset'
import { DatasetMockRepository } from './DatasetMockRepository'
import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount'
import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview'
import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount'
import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO'
import { FakerHelper } from '../../../tests/component/shared/FakerHelper'

export class DatasetErrorMockRepository implements DatasetMockRepository {
getAll(_collectionId: string, _paginationInfo: DatasetPaginationInfo): Promise<DatasetPreview[]> {
return new Promise((_resolve, reject) => {
setTimeout(() => {
reject('Error thrown from mock')
}, 1000)
})
}

getTotalDatasetsCount(_collectionId: string): Promise<TotalDatasetsCount> {
return new Promise((_resolve, reject) => {
setTimeout(() => {
reject('Error thrown from mock')
}, 1000)
})
}

getAllWithCount: (
collectionId: string,
paginationInfo: DatasetPaginationInfo
Expand Down
5 changes: 0 additions & 5 deletions src/stories/dataset/DatasetLoadingMockRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { DatasetMockRepository } from './DatasetMockRepository'
import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview'
import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount'

export class DatasetLoadingMockRepository extends DatasetMockRepository {
getAll(_collectionId: string, _paginationInfo: DatasetPaginationInfo): Promise<DatasetPreview[]> {
return new Promise(() => {})
}

getDatasetsWithCount: (
collectionId: string,
paginationInfo: DatasetPaginationInfo
Expand Down
18 changes: 0 additions & 18 deletions src/stories/dataset/DatasetMockRepository.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import { Dataset } from '../../dataset/domain/models/Dataset'
import { DatasetRepository } from '../../dataset/domain/repositories/DatasetRepository'
import { DatasetMother } from '../../../tests/component/dataset/domain/models/DatasetMother'
import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount'
import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview'
import { DatasetPreviewMother } from '../../../tests/component/dataset/domain/models/DatasetPreviewMother'
import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO'
import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount'
import { FakerHelper } from '../../../tests/component/shared/FakerHelper'
export class DatasetMockRepository implements DatasetRepository {
getAll(_collectionId: string, paginationInfo: DatasetPaginationInfo): Promise<DatasetPreview[]> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(DatasetPreviewMother.createManyRealistic(paginationInfo.pageSize))
}, FakerHelper.loadingTimout())
})
}

getTotalDatasetsCount(_collectionId: string): Promise<TotalDatasetsCount> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(200)
}, FakerHelper.loadingTimout())
})
}

getAllWithCount: (
collectionId: string,
paginationInfo: DatasetPaginationInfo
Expand Down
18 changes: 0 additions & 18 deletions src/stories/dataset/NoDatasetsMockRepository.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import { DatasetMockRepository } from './DatasetMockRepository'
import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview'
import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount'
import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount'
export class NoDatasetsMockRepository extends DatasetMockRepository {
getAll(_collectionId: string, _paginationInfo: DatasetPaginationInfo): Promise<DatasetPreview[]> {
return new Promise((resolve) => {
setTimeout(() => {
resolve([])
}, 1000)
})
}

getTotalDatasetsCount(_collectionId: string): Promise<TotalDatasetsCount> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(0)
}, 1000)
})
}

getAllWithCount: (
collectionId: string,
paginationInfo: DatasetPaginationInfo
Expand Down
5 changes: 3 additions & 2 deletions tests/component/sections/collection/Collection.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const totalDatasetsCount = 200
const datasets = DatasetPreviewMother.createMany(totalDatasetsCount)
const collectionRepository = {} as CollectionRepository
const collection = CollectionMother.create({ name: 'Collection Name' })
const datasetsWithCount = { datasetPreviews: datasets, totalCount: totalDatasetsCount }

describe('Collection page', () => {
beforeEach(() => {
datasetRepository.getAll = cy.stub().resolves(datasets)
datasetRepository.getTotalDatasetsCount = cy.stub().resolves(totalDatasetsCount)
datasetRepository.getAllWithCount = cy.stub().resolves(datasetsWithCount)
collectionRepository.getById = cy.stub().resolves(collection)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { DatasetRepository } from '../../../../../src/dataset/domain/repositories/DatasetRepository'
import { DatasetsList } from '../../../../../src/sections/collection/datasets-list/DatasetsList'
import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreviewMother } from '../../../dataset/domain/models/DatasetPreviewMother'
import { DatasetPreview } from '@iqss/dataverse-client-javascript'
import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo'

const datasetRepository: DatasetRepository = {} as DatasetRepository
const totalDatasetsCount = 200
const datasets = DatasetPreviewMother.createMany(totalDatasetsCount)
const datasetsWithCount = { datasetPreviews: datasets, totalCount: totalDatasetsCount }
describe('Datasets List', () => {
beforeEach(() => {
datasetRepository.getAll = cy.stub().resolves(datasets)
datasetRepository.getTotalDatasetsCount = cy.stub().resolves(totalDatasetsCount)
datasetRepository.getAllWithCount = cy.stub().resolves(datasetsWithCount)
})

it('renders skeleton while loading', () => {
Expand All @@ -22,7 +23,9 @@ describe('Datasets List', () => {
})

it('renders no datasets message when there are no datasets', () => {
datasetRepository.getAll = cy.stub().resolves([])
const emptyDatasets: DatasetPreview[] = []
const emptyDatasetsWithCount = { datasetPreviews: emptyDatasets, totalCount: 0 }
datasetRepository.getAllWithCount = cy.stub().resolves(emptyDatasetsWithCount)
cy.customMount(<DatasetsList datasetRepository={datasetRepository} collectionId="root" />)

cy.findByText(/This dataverse currently has no datasets./).should('exist')
Expand All @@ -31,12 +34,11 @@ describe('Datasets List', () => {
it('renders the datasets list', () => {
cy.customMount(<DatasetsList datasetRepository={datasetRepository} collectionId="root" />)

cy.wrap(datasetRepository.getAll).should(
cy.wrap(datasetRepository.getAllWithCount).should(
'be.calledOnceWith',
'root',
new DatasetPaginationInfo(1, 10, totalDatasetsCount)
new DatasetPaginationInfo(1, 10, 0)
)

cy.findByText('1 to 10 of 200 Datasets').should('exist')
datasets.forEach((dataset) => {
cy.findByText(dataset.version.title)
Expand All @@ -49,11 +51,10 @@ describe('Datasets List', () => {
cy.customMount(<DatasetsList datasetRepository={datasetRepository} collectionId="root" />)

cy.findByRole('button', { name: '6' }).click()

cy.wrap(datasetRepository.getAll).should(
cy.wrap(datasetRepository.getAllWithCount).should(
'be.calledWith',
'root',
new DatasetPaginationInfo(1, 10, totalDatasetsCount).goToPage(6)
new DatasetPaginationInfo(1, 10, 200).goToPage(6)
)
cy.findByText('51 to 60 of 200 Datasets').should('exist')
})
Expand All @@ -62,11 +63,10 @@ describe('Datasets List', () => {
cy.customMount(
<DatasetsList datasetRepository={datasetRepository} page={5} collectionId="root" />
)

cy.wrap(datasetRepository.getAll).should(
cy.wrap(datasetRepository.getAllWithCount).should(
'be.calledWith',
'root',
new DatasetPaginationInfo(1, 10, totalDatasetsCount).goToPage(5)
new DatasetPaginationInfo(1, 10, 0).goToPage(5)
)
cy.findByText('41 to 50 of 200 Datasets').should('exist')
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Collection JSDataverse Repository', () => {

it('gets the collection by id', async () => {
const collectionResponse = await CollectionHelper.create('new-collection')

console.log('collectionResponse', collectionResponse.id)
await collectionRepository.getById(collectionResponse.id).then((collection) => {
if (!collection) {
throw new Error('Collection not found')
Expand Down
Loading
Loading