• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IQSS / dataverse-frontend / 7960501805

19 Feb 2024 01:42PM CUT coverage: 97.453% (-0.007%) from 97.46%
7960501805

push

github

web-flow
Merge pull request #284 from IQSS/feature/280-use-query-param-in-home-page-pagination

280 - Use page search param in home page pagination

533 of 554 branches covered (96.21%)

Branch coverage included in aggregate %.

1571 of 1605 relevant lines covered (97.88%)

3749.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.05
/src/sections/home/datasets-list/useDatasets.tsx
1
import { useEffect, useState } from 'react'
2
import { DatasetRepository } from '../../../dataset/domain/repositories/DatasetRepository'
3
import { getDatasets } from '../../../dataset/domain/useCases/getDatasets'
4
import { getTotalDatasetsCount } from '../../../dataset/domain/useCases/getTotalDatasetsCount'
5
import { TotalDatasetsCount } from '../../../dataset/domain/models/TotalDatasetsCount'
6
import { DatasetPaginationInfo } from '../../../dataset/domain/models/DatasetPaginationInfo'
7
import { DatasetPreview } from '../../../dataset/domain/models/DatasetPreview'
8

9
export function useDatasets(
10
  datasetRepository: DatasetRepository,
11
  onPaginationInfoChange: (paginationInfo: DatasetPaginationInfo) => void,
12
  paginationInfo: DatasetPaginationInfo
13
) {
144✔
14
  const [pageNumberNotFound, setPageNumberNotFound] = useState<boolean>(false)
144✔
15
  const [datasets, setDatasets] = useState<DatasetPreview[]>([])
144✔
16
  const [isLoading, setIsLoading] = useState<boolean>(true)
144✔
17
  const [totalDatasetsCount, setTotalDatasetsCount] = useState<TotalDatasetsCount>()
144✔
18

19
  const fetchTotalDatasetsCount: () => Promise<TotalDatasetsCount> = () => {
144✔
20
    return getTotalDatasetsCount(datasetRepository)
43✔
21
      .then((totalDatasetsCount: TotalDatasetsCount) => {
22
        setTotalDatasetsCount(totalDatasetsCount)
43✔
23
        if (totalDatasetsCount !== paginationInfo.totalItems) {
43✔
24
          paginationInfo = paginationInfo.withTotal(totalDatasetsCount)
36✔
25
          onPaginationInfoChange(paginationInfo)
36✔
26
        }
27
        return totalDatasetsCount
43✔
28
      })
29
      .catch(() => {
30
        throw new Error('There was an error getting the datasets count info')
×
31
      })
32
  }
33
  const fetchDatasets = (totalDatasetsCount: TotalDatasetsCount) => {
144✔
34
    if (typeof totalDatasetsCount !== 'undefined') {
43✔
35
      if (totalDatasetsCount === 0) {
43!
36
        setIsLoading(false)
×
37
        return
×
38
      }
39
      if (paginationInfo.page > paginationInfo.totalPages) {
43✔
40
        setPageNumberNotFound(true)
2✔
41
        setIsLoading(false)
2✔
42
        return
2✔
43
      }
44
      return getDatasets(datasetRepository, paginationInfo)
41✔
45
        .then((datasets: DatasetPreview[]) => {
46
          setDatasets(datasets)
41✔
47
          setIsLoading(false)
41✔
48
          return datasets
41✔
49
        })
50
        .catch(() => {
51
          throw new Error('There was an error getting the datasets')
×
52
        })
53
    }
54
  }
55

56
  useEffect(() => {
144✔
57
    setIsLoading(true)
43✔
58

59
    fetchTotalDatasetsCount()
43✔
60
      .then((totalDatasetsCount) => fetchDatasets(totalDatasetsCount))
43✔
61
      .catch(() => {
62
        console.error('There was an error getting the datasets')
×
63
        setIsLoading(false)
×
64
      })
65
  }, [datasetRepository, paginationInfo.page])
66

67
  return {
144✔
68
    datasets,
69
    totalDatasetsCount,
70
    isLoading,
71
    pageNumberNotFound
72
  }
73
}
11✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc