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

IQSS / dataverse-frontend / 8509236605

01 Apr 2024 02:19PM UTC coverage: 97.539%. Remained the same
8509236605

Pull #353

github

MellyGray
Merge branch 'develop' of https://github.com/IQSS/dataverse-frontend into docs/26-add-frontend-guides
Pull Request #353: 26 - Add frontend guides

595 of 620 branches covered (95.97%)

Branch coverage included in aggregate %.

1704 of 1737 relevant lines covered (98.1%)

3566.65 hits per line

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

82.05
/src/sections/collection/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
  collectionId: string,
12
  onPaginationInfoChange: (paginationInfo: DatasetPaginationInfo) => void,
13
  paginationInfo: DatasetPaginationInfo
14
) {
185✔
15
  const [pageNumberNotFound, setPageNumberNotFound] = useState<boolean>(false)
185✔
16
  const [datasets, setDatasets] = useState<DatasetPreview[]>([])
185✔
17
  const [isLoading, setIsLoading] = useState<boolean>(true)
185✔
18
  const [totalDatasetsCount, setTotalDatasetsCount] = useState<TotalDatasetsCount>()
185✔
19

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

57
  useEffect(() => {
185✔
58
    setIsLoading(true)
56✔
59

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

68
  return {
185✔
69
    datasets,
70
    totalDatasetsCount,
71
    isLoading,
72
    pageNumberNotFound
73
  }
74
}
14✔
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