Skip to content

Commit 78a5bbb

Browse files
committed
fix: Added an [enabled] prevent multiple calls of GetGuestbook and clean extra props drilling
1 parent 4e93a2e commit 78a5bbb

15 files changed

Lines changed: 354 additions & 242 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This changelog follows the principles of [Keep a Changelog](https://keepachangel
1717
- Added Notifications tab in Account Page
1818
- Added runtime configuration options for homepage branding and support link.
1919
- Added an environment variable to docker-compose-dev.yml to hide the OIDC client used in the SPA from the JSF frontend: DATAVERSE_AUTH_OIDC_HIDDEN_JSF: 1
20+
- Download with terms of use and guestbook.
2021

2122
### Changed
2223

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@dnd-kit/sortable": "8.0.0",
1919
"@dnd-kit/utilities": "3.2.2",
2020
"@faker-js/faker": "7.6.0",
21-
"@iqss/dataverse-client-javascript": "2.1.0-pr435.0c8eb79",
21+
"@iqss/dataverse-client-javascript": "2.1.0-alpha.2",
2222
"@iqss/dataverse-design-system": "*",
2323
"@istanbuljs/nyc-config-typescript": "1.0.2",
2424
"@tanstack/react-table": "8.9.2",

src/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function AccessDatasetMenu({
9898
/>
9999
<DatasetExploreOptions persistentId={persistentId} />
100100
</DropdownButton>
101-
{hasGuestbook && (
101+
{hasGuestbook && showDownloadWithGuestbookModal && (
102102
<DownloadWithGuestbookModal
103103
show={showDownloadWithGuestbookModal}
104104
handleClose={() => setShowDownloadWithGuestbookModal(false)}

src/sections/dataset/dataset-files/files-table/file-actions/download-files/DownloadFilesButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function DownloadFilesButton({ files, fileSelection }: DownloadFilesButto
9797
show={showNoFilesSelectedModal}
9898
handleClose={() => setShowNoFilesSelectedModal(false)}
9999
/>
100-
{hasGuestbook && (
100+
{hasGuestbook && showDownloadWithGuestbookModal && (
101101
<DownloadWithGuestbookModal
102102
fileIds={fileIdsForGuestbookSubmission}
103103
datasetId={allFilesSelected ? dataset.id : undefined}

src/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/file-options-menu/DownloadWithGuestbookModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function DownloadWithGuestbookModal({
5555
const [formValues, setFormValues] = useState<GuestbookFormValues>({})
5656
const { guestbook, isLoadingGuestbook, errorGetGuestbook } = useGetGuestbookById({
5757
guestbookRepository,
58-
guestbookId
58+
guestbookId,
59+
enabled: show
5960
})
6061
const accountFieldKeys = useMemo(() => ['name', 'email', 'institution', 'position'], [])
6162

src/sections/dataset/dataset-guestbook/useGetGuestbookById.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ import { JSDataverseReadErrorHandler } from '@/shared/helpers/JSDataverseReadErr
99
interface useGetGuestbookByIdProps {
1010
guestbookRepository: GuestbookRepository
1111
guestbookId?: number
12+
enabled?: boolean
1213
}
1314

1415
export const useGetGuestbookById = ({
1516
guestbookRepository,
16-
guestbookId
17+
guestbookId,
18+
enabled = true
1719
}: useGetGuestbookByIdProps) => {
1820
const { t } = useTranslation('guestbooks')
1921
const [guestbook, setGuestbook] = useState<Guestbook | undefined>(undefined)
20-
const [isLoadingGuestbook, setIsLoadingGuestbook] = useState<boolean>(guestbookId !== undefined)
22+
const [isLoadingGuestbook, setIsLoadingGuestbook] = useState<boolean>(
23+
enabled && guestbookId !== undefined
24+
)
2125
const [errorGetGuestbook, setErrorGetGuestbook] = useState<string | null>(null)
2226

2327
const fetchGuestbook = useCallback(async () => {
24-
if (guestbookId === undefined) {
28+
if (!enabled || guestbookId === undefined) {
2529
setGuestbook(undefined)
2630
setIsLoadingGuestbook(false)
2731
setErrorGetGuestbook(null)
@@ -47,7 +51,7 @@ export const useGetGuestbookById = ({
4751
} finally {
4852
setIsLoadingGuestbook(false)
4953
}
50-
}, [guestbookId, guestbookRepository, t])
54+
}, [enabled, guestbookId, guestbookRepository, t])
5155

5256
useEffect(() => {
5357
void fetchGuestbook()

src/sections/file/file-action-buttons/access-file-menu/FileDownloadOptions.tsx

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { useState } from 'react'
12
import { DropdownHeader } from '@iqss/dataverse-design-system'
23
import { Download } from 'react-bootstrap-icons'
34
import { FileTabularDownloadOptions } from './FileTabularDownloadOptions'
45
import { FileNonTabularDownloadOptions } from './FileNonTabularDownloadOptions'
56
import { useTranslation } from 'react-i18next'
6-
import { FileDownloadUrls, FileType } from '../../../../files/domain/models/FileMetadata'
7+
import {
8+
FileDownloadMode,
9+
FileDownloadUrls,
10+
FileType
11+
} from '../../../../files/domain/models/FileMetadata'
712
import { useDataset } from '@/sections/dataset/DatasetContext'
813
import { CustomTerms, DatasetLicense } from '@/dataset/domain/models/Dataset'
14+
import { DownloadWithGuestbookModal } from '@/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/file-options-menu/DownloadWithGuestbookModal'
915

1016
interface FileDownloadOptionsProps {
1117
fileId: number
@@ -34,6 +40,10 @@ export function FileDownloadOptions({
3440
}: FileDownloadOptionsProps) {
3541
const { t } = useTranslation('files')
3642
const { dataset } = useDataset()
43+
const [showDownloadWithGuestbookModal, setShowDownloadWithGuestbookModal] = useState(false)
44+
const [selectedDownloadFormat, setSelectedDownloadFormat] = useState<string | FileDownloadMode>(
45+
FileDownloadMode.ORIGINAL
46+
)
3747

3848
if (!userHasDownloadPermission) {
3949
return <></>
@@ -44,6 +54,12 @@ export function FileDownloadOptions({
4454
const resolvedDatasetLicense = datasetLicense ?? dataset?.license
4555
const resolvedDatasetCustomTerms = datasetCustomTerms ?? dataset?.termsOfUse?.customTerms
4656
const isLockedFromFileDownload = !!dataset?.isLockedFromFileDownload
57+
const hasGuestbook = resolvedGuestbookId !== undefined
58+
59+
const openGuestbookModal = (format: string | FileDownloadMode) => {
60+
setSelectedDownloadFormat(format)
61+
setShowDownloadWithGuestbookModal(true)
62+
}
4763

4864
return (
4965
<>
@@ -56,25 +72,33 @@ export function FileDownloadOptions({
5672
type={type}
5773
ingestInProgress={ingestInProgress}
5874
downloadUrls={downloadUrls}
59-
guestbookId={resolvedGuestbookId}
60-
datasetPersistentId={resolvedDatasetPersistentId}
61-
datasetLicense={resolvedDatasetLicense}
62-
datasetCustomTerms={resolvedDatasetCustomTerms}
75+
hasGuestbook={hasGuestbook}
76+
onOpenGuestbookModal={openGuestbookModal}
6377
isLockedFromFileDownload={isLockedFromFileDownload}
6478
/>
6579
) : (
6680
<FileNonTabularDownloadOptions
6781
fileId={fileId}
68-
guestbookId={resolvedGuestbookId}
69-
datasetPersistentId={resolvedDatasetPersistentId}
70-
datasetLicense={resolvedDatasetLicense}
71-
datasetCustomTerms={resolvedDatasetCustomTerms}
82+
hasGuestbook={hasGuestbook}
83+
onOpenGuestbookModal={() => openGuestbookModal(FileDownloadMode.ORIGINAL)}
7284
type={type}
7385
ingestIsInProgress={ingestInProgress}
7486
downloadUrlOriginal={downloadUrls.original}
7587
isLockedFromFileDownload={isLockedFromFileDownload}
7688
/>
7789
)}
90+
{hasGuestbook && showDownloadWithGuestbookModal && (
91+
<DownloadWithGuestbookModal
92+
fileId={fileId}
93+
guestbookId={resolvedGuestbookId}
94+
format={selectedDownloadFormat}
95+
datasetPersistentId={resolvedDatasetPersistentId}
96+
datasetLicense={resolvedDatasetLicense}
97+
datasetCustomTerms={resolvedDatasetCustomTerms}
98+
show={showDownloadWithGuestbookModal}
99+
handleClose={() => setShowDownloadWithGuestbookModal(false)}
100+
/>
101+
)}
78102
</>
79103
)
80104
}
Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { DropdownButtonItem } from '@iqss/dataverse-design-system'
22
import { useTranslation } from 'react-i18next'
33
import { FileDownloadMode, FileType } from '../../../../files/domain/models/FileMetadata'
4-
import { DownloadWithGuestbookModal } from '@/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/file-options-menu/DownloadWithGuestbookModal'
5-
import { MouseEvent, useState } from 'react'
6-
import { CustomTerms, DatasetLicense } from '@/dataset/domain/models/Dataset'
4+
import { MouseEvent } from 'react'
75
import { toast } from 'react-toastify'
86
import { useAccessRepository } from '@/sections/access/AccessRepositoryContext'
97
import {
@@ -14,10 +12,8 @@ import {
1412

1513
interface FileNonTabularDownloadOptionsProps {
1614
fileId: number
17-
guestbookId?: number
18-
datasetPersistentId?: string
19-
datasetLicense?: DatasetLicense
20-
datasetCustomTerms?: CustomTerms
15+
hasGuestbook: boolean
16+
onOpenGuestbookModal: () => void
2117
type: FileType
2218
downloadUrlOriginal: string
2319
ingestIsInProgress: boolean
@@ -26,18 +22,14 @@ interface FileNonTabularDownloadOptionsProps {
2622

2723
export function FileNonTabularDownloadOptions({
2824
fileId,
29-
guestbookId,
30-
datasetPersistentId,
31-
datasetLicense,
32-
datasetCustomTerms,
25+
hasGuestbook,
26+
onOpenGuestbookModal,
3327
type,
3428
ingestIsInProgress,
3529
isLockedFromFileDownload
3630
}: FileNonTabularDownloadOptionsProps) {
3731
const { t } = useTranslation('files')
3832
const accessRepository = useAccessRepository()
39-
const hasGuestbook = guestbookId !== undefined
40-
const [showDownloadWithGuestbookModal, setShowDownloadWithGuestbookModal] = useState(false)
4133
const downloadDisabled = ingestIsInProgress || isLockedFromFileDownload
4234

4335
const handleDownloadClick = (event: MouseEvent<HTMLElement>) => {
@@ -47,7 +39,7 @@ export function FileNonTabularDownloadOptions({
4739

4840
if (hasGuestbook) {
4941
event.preventDefault()
50-
setShowDownloadWithGuestbookModal(true)
42+
onOpenGuestbookModal()
5143
return
5244
}
5345

@@ -68,27 +60,13 @@ export function FileNonTabularDownloadOptions({
6860
}
6961

7062
return (
71-
<>
72-
<DropdownButtonItem
73-
data-testid={`download-original-file`}
74-
onClick={handleDownloadClick}
75-
disabled={downloadDisabled}>
76-
{type.displayFormatIsUnknown
77-
? t('actions.accessFileMenu.downloadOptions.options.original')
78-
: type.toDisplayFormat()}
79-
</DropdownButtonItem>
80-
{hasGuestbook && showDownloadWithGuestbookModal && (
81-
<DownloadWithGuestbookModal
82-
fileId={fileId}
83-
guestbookId={guestbookId}
84-
format={FileDownloadMode.ORIGINAL}
85-
datasetPersistentId={datasetPersistentId}
86-
datasetLicense={datasetLicense}
87-
datasetCustomTerms={datasetCustomTerms}
88-
show={showDownloadWithGuestbookModal}
89-
handleClose={() => setShowDownloadWithGuestbookModal(false)}
90-
/>
91-
)}
92-
</>
63+
<DropdownButtonItem
64+
data-testid={`download-original-file`}
65+
onClick={handleDownloadClick}
66+
disabled={downloadDisabled}>
67+
{type.displayFormatIsUnknown
68+
? t('actions.accessFileMenu.downloadOptions.options.original')
69+
: type.toDisplayFormat()}
70+
</DropdownButtonItem>
9371
)
9472
}

src/sections/file/file-action-buttons/access-file-menu/FileTabularDownloadOptions.tsx

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { MouseEvent, useState } from 'react'
1+
import { MouseEvent } from 'react'
22
import { DropdownButtonItem } from '@iqss/dataverse-design-system'
33
import { useTranslation } from 'react-i18next'
44
import { toast } from 'react-toastify'
55
import { useAccessRepository } from '@/sections/access/AccessRepositoryContext'
6-
import { DownloadWithGuestbookModal } from '@/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/file-options-menu/DownloadWithGuestbookModal'
76
import {
87
downloadFromSignedUrl,
98
EMPTY_GUESTBOOK_RESPONSE,
@@ -12,17 +11,14 @@ import {
1211
import { FileDownloadMode, FileType } from '../../../../files/domain/models/FileMetadata'
1312
import type { FileDownloadUrls } from '../../../../files/domain/models/FileMetadata'
1413
import FileTypeToFriendlyTypeMap from '../../../../files/domain/models/FileTypeToFriendlyTypeMap'
15-
import { CustomTerms, DatasetLicense } from '@/dataset/domain/models/Dataset'
1614

1715
interface FileTabularDownloadOptionsProps {
1816
fileId: number
1917
type: FileType
2018
ingestInProgress: boolean
2119
downloadUrls: FileDownloadUrls
22-
guestbookId?: number
23-
datasetPersistentId?: string
24-
datasetLicense?: DatasetLicense
25-
datasetCustomTerms?: CustomTerms
20+
hasGuestbook: boolean
21+
onOpenGuestbookModal: (format: string) => void
2622
isLockedFromFileDownload: boolean
2723
}
2824

@@ -31,20 +27,13 @@ export function FileTabularDownloadOptions({
3127
type,
3228
ingestInProgress,
3329
downloadUrls,
34-
guestbookId,
35-
datasetPersistentId,
36-
datasetLicense,
37-
datasetCustomTerms,
30+
hasGuestbook,
31+
onOpenGuestbookModal,
3832
isLockedFromFileDownload
3933
}: FileTabularDownloadOptionsProps) {
4034
const { t } = useTranslation('files')
4135
const accessRepository = useAccessRepository()
4236
const downloadDisabled = ingestInProgress || isLockedFromFileDownload
43-
const hasGuestbook = guestbookId !== undefined
44-
const [showDownloadWithGuestbookModal, setShowDownloadWithGuestbookModal] = useState(false)
45-
const [selectedDownloadFormat, setSelectedDownloadFormat] = useState<string>(
46-
FileDownloadMode.ORIGINAL
47-
)
4837

4938
const handleDownloadClick = (event: MouseEvent<HTMLElement>, format: string) => {
5039
if (downloadDisabled) {
@@ -54,8 +43,7 @@ export function FileTabularDownloadOptions({
5443
event.preventDefault()
5544

5645
if (hasGuestbook) {
57-
setSelectedDownloadFormat(format)
58-
setShowDownloadWithGuestbookModal(true)
46+
onOpenGuestbookModal(format)
5947
return
6048
}
6149

@@ -96,18 +84,6 @@ export function FileTabularDownloadOptions({
9684
{t('actions.accessFileMenu.downloadOptions.options.RData')}
9785
</DropdownButtonItem>
9886
)}
99-
{hasGuestbook && showDownloadWithGuestbookModal && (
100-
<DownloadWithGuestbookModal
101-
fileId={fileId}
102-
guestbookId={guestbookId}
103-
format={selectedDownloadFormat}
104-
datasetPersistentId={datasetPersistentId}
105-
datasetLicense={datasetLicense}
106-
datasetCustomTerms={datasetCustomTerms}
107-
show={showDownloadWithGuestbookModal}
108-
handleClose={() => setShowDownloadWithGuestbookModal(false)}
109-
/>
110-
)}
11187
</>
11288
)
11389
}

0 commit comments

Comments
 (0)