1- import { useEffect , useMemo , useRef , useState } from 'react'
1+ import { useEffect , useMemo , useRef } from 'react'
22import { useTranslation } from 'react-i18next'
33import { useForm , Controller , FormProvider } from 'react-hook-form'
44import { toast } from 'react-toastify'
5- import { Form , Row , Col , Button , Alert , Spinner } from '@iqss/dataverse-design-system'
6- import { DatasetLicense , CustomTerms } from '../../../dataset/domain/models/Dataset'
5+ import { Form , Row , Col , Button , Alert } from '@iqss/dataverse-design-system'
6+ import { CustomTerms } from '../../../dataset/domain/models/Dataset'
77import { LicenseRepository } from '../../../licenses/domain/repositories/LicenseRepository'
88import { useGetLicenses } from './useGetLicenses'
99import { DatasetRepository } from '@/dataset/domain/repositories/DatasetRepository'
1010import { useDataset } from '../../dataset/DatasetContext'
11- import { updateDatasetLicense } from '@/dataset/domain/useCases/updateDatasetLicense '
11+ import { useUpdateDatasetLicense } from './useUpdateDatasetLicense '
1212import styles from './DatasetTermsTab.module.scss'
1313
1414interface DatasetTermsFormData {
@@ -17,34 +17,33 @@ interface DatasetTermsFormData {
1717}
1818
1919interface DatasetTermsTabProps {
20- initialLicense : DatasetLicense | CustomTerms
2120 licenseRepository : LicenseRepository
2221 datasetRepository : DatasetRepository
23- isInitialCustomTerms : boolean
2422}
2523
26- export function DatasetTermsTab ( {
27- initialLicense,
28- licenseRepository,
29- datasetRepository,
30- isInitialCustomTerms
31- } : DatasetTermsTabProps ) {
24+ export function DatasetTermsTab ( { licenseRepository, datasetRepository } : DatasetTermsTabProps ) {
3225 const { t } = useTranslation ( 'dataset' )
3326 const { t : tShared } = useTranslation ( 'shared' )
3427 const { dataset, refreshDataset } = useDataset ( )
3528
3629 const formContainerRef = useRef < HTMLDivElement > ( null )
37- const [ isSubmitting , setIsSubmitting ] = useState ( false )
38- const [ submitError , setSubmitError ] = useState < string | null > ( null )
3930
4031 const { licenses, isLoadingLicenses, errorLicenses } = useGetLicenses ( {
4132 licenseRepository,
4233 autoFetch : true
4334 } )
4435
36+ const { handleUpdateLicense, isLoading, error } = useUpdateDatasetLicense ( {
37+ datasetRepository,
38+ onSuccessfulUpdateLicense : ( ) => {
39+ toast . success ( t ( 'alerts.licenseUpdated.alertText' ) )
40+ refreshDataset ( )
41+ }
42+ } )
43+
4544 const initialCustomTerms = useMemo ( ( ) : CustomTerms => {
46- if ( isInitialCustomTerms ) {
47- return initialLicense as CustomTerms
45+ if ( dataset ?. termsOfUse . customTerms ) {
46+ return dataset . termsOfUse . customTerms
4847 }
4948 return {
5049 termsOfUse : '' ,
@@ -56,11 +55,10 @@ export function DatasetTermsTab({
5655 conditions : '' ,
5756 disclaimer : ''
5857 }
59- } , [ initialLicense , isInitialCustomTerms ] )
58+ } , [ dataset ?. termsOfUse . customTerms ] )
6059
6160 const licenseOptions = useMemo ( ( ) => {
6261 const dynamicOptions = licenses
63- . filter ( ( license ) => license . active )
6462 . sort ( ( a , b ) => a . sortOrder - b . sortOrder )
6563 . map ( ( license ) => ( {
6664 value : license . id . toString ( ) ,
@@ -75,23 +73,23 @@ export function DatasetTermsTab({
7573 label : t ( 'editTerms.datasetTerms.customTermsLabel' ) ,
7674 uri : '' ,
7775 iconUri : '' ,
78- isDefault : isInitialCustomTerms
76+ isDefault : dataset ?. termsOfUse . customTerms !== undefined
7977 } )
8078
8179 return dynamicOptions
82- } , [ licenses , t , isInitialCustomTerms ] )
80+ } , [ licenses , t , dataset ?. termsOfUse . customTerms ] )
8381
8482 // Determine the default license value based on initial state and available options
8583 const defaultLicenseValue = useMemo ( ( ) => {
86- if ( isInitialCustomTerms ) {
84+ if ( dataset ?. termsOfUse . customTerms !== undefined ) {
8785 return 'CUSTOM'
8886 } else {
8987 const matchingLicense = licenseOptions . find (
90- ( option ) => option . label === ( initialLicense as DatasetLicense ) . name
88+ ( option ) => option . label === dataset ?. license ? .name
9189 )
9290 return matchingLicense ?. value
9391 }
94- } , [ licenseOptions , initialLicense , isInitialCustomTerms ] )
92+ } , [ licenseOptions , dataset ?. license ?. name , dataset ?. termsOfUse . customTerms ] )
9593
9694 const form = useForm < DatasetTermsFormData > ( {
9795 defaultValues : {
@@ -135,56 +133,27 @@ export function DatasetTermsTab({
135133 } ) )
136134 } , [ initialCustomTerms , isCustomTerms , t ] )
137135
138- const onSubmit = async ( data : DatasetTermsFormData ) => {
136+ const onSubmit = ( data : DatasetTermsFormData ) => {
139137 if ( ! dataset ) return
140138
141- setIsSubmitting ( true )
142- setSubmitError ( null )
143-
144- try {
145- if ( data . license === 'CUSTOM' ) {
146- await updateDatasetLicense ( datasetRepository , dataset . id , {
147- customTerms : data . customTerms
139+ if ( data . license === 'CUSTOM' ) {
140+ void handleUpdateLicense ( dataset . id , {
141+ customTerms : data . customTerms
142+ } )
143+ } else {
144+ const selectedLicense = licenseOptions . find ( ( option ) => option . value === data . license )
145+ if ( selectedLicense ) {
146+ void handleUpdateLicense ( dataset . id , {
147+ name : selectedLicense . label
148148 } )
149- } else {
150- const selectedLicense = licenseOptions . find ( ( option ) => option . value === data . license )
151- if ( selectedLicense ) {
152- await updateDatasetLicense ( datasetRepository , dataset . id , {
153- name : selectedLicense . label
154- } )
155- }
156149 }
157- toast . success ( t ( 'alerts.licenseUpdated.alertText' ) )
158- refreshDataset ( )
159- } catch ( err ) {
160- const errorMessage =
161- err instanceof Error && err . message ? err . message : t ( 'editTerms.defaultLicenseUpdateError' )
162- setSubmitError ( errorMessage )
163- onSubmitError ( )
164- } finally {
165- setIsSubmitting ( false )
166- }
167- }
168-
169- function onSubmitError ( ) {
170- if ( formContainerRef . current ) {
171- formContainerRef . current . scrollIntoView ( { behavior : 'smooth' , block : 'start' } )
172150 }
173151 }
174152
175- const preventEnterSubmit = ( e : React . KeyboardEvent < HTMLFormElement | HTMLButtonElement > ) => {
176- if ( e . key !== 'Enter' ) return
177-
178- const isButton = e . target instanceof HTMLButtonElement
179- const isButtonTypeSubmit = isButton ? ( e . target as HTMLButtonElement ) . type === 'submit' : false
180-
181- if ( ! isButton && ! isButtonTypeSubmit ) e . preventDefault ( )
182- }
183-
184153 return (
185154 < div ref = { formContainerRef } className = { styles [ 'dataset-terms-tab' ] } >
186155 < FormProvider { ...form } >
187- < form onSubmit = { handleSubmit ( onSubmit ) } onKeyDown = { preventEnterSubmit } noValidate = { true } >
156+ < form onSubmit = { handleSubmit ( onSubmit ) } noValidate = { true } >
188157 { /* License/Data Use Agreement Section */ }
189158 < Row style = { { marginBottom : '1rem' } } >
190159 < Col sm = { 4 } >
@@ -206,17 +175,11 @@ export function DatasetTermsTab({
206175 onChange = { onChange }
207176 isInvalid = { invalid }
208177 disabled = { isLoadingLicenses } >
209- { isLoadingLicenses ? (
210- < Spinner variant = "light" animation = "border" size = "sm" />
211- ) : errorLicenses ? (
212- < Alert variant = "danger" > { tShared ( 'loading' ) } </ Alert >
213- ) : (
214- licenseOptions . map ( ( option ) => (
215- < option key = { option . value } value = { option . value } >
216- { option . label }
217- </ option >
218- ) )
219- ) }
178+ { licenseOptions . map ( ( option ) => (
179+ < option key = { option . value } value = { option . value } >
180+ { option . label }
181+ </ option >
182+ ) ) }
220183 </ Form . Group . Select >
221184 < Form . Group . Feedback type = "invalid" > { error ?. message } </ Form . Group . Feedback >
222185 </ Col >
@@ -261,10 +224,7 @@ export function DatasetTermsTab({
261224 name = { `customTerms.${ field . name } ` as keyof DatasetTermsFormData }
262225 control = { control }
263226 rules = { field . rules }
264- render = { ( {
265- field : { onChange, value, ref } ,
266- fieldState : { invalid, error }
267- } ) => (
227+ render = { ( { field : { onChange, value } , fieldState : { invalid, error } } ) => (
268228 < Col sm = { 8 } >
269229 < Row >
270230 < Col >
@@ -275,7 +235,6 @@ export function DatasetTermsTab({
275235 isInvalid = { invalid }
276236 rows = { field . rows }
277237 aria-required = { field . required }
278- ref = { ref }
279238 />
280239 { field . required && (
281240 < Form . Group . Feedback type = "invalid" >
@@ -292,20 +251,20 @@ export function DatasetTermsTab({
292251 </ >
293252 ) }
294253
295- { submitError && (
254+ { error && (
296255 < Alert variant = "danger" dismissible = { false } >
297- { submitError }
256+ { error }
298257 </ Alert >
299258 ) }
300259
301260 < div className = { styles [ 'form-actions' ] } >
302- < Button type = "submit" disabled = { ! isValid || isSubmitting } >
303- { isSubmitting ? tShared ( 'saving' ) : tShared ( 'saveChanges' ) }
261+ < Button type = "submit" disabled = { ! isValid || isLoading } >
262+ { isLoading ? tShared ( 'saving' ) : tShared ( 'saveChanges' ) }
304263 </ Button >
305264 < Button
306265 variant = "secondary"
307266 type = "button"
308- disabled = { isSubmitting }
267+ disabled = { isLoading }
309268 onClick = { ( ) =>
310269 reset ( {
311270 license : defaultLicenseValue ,
0 commit comments