Skip to content

Commit

Permalink
Add isLoading considerations, add event handler for Cancel btn
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Mangan committed Jan 29, 2024
1 parent 6de18a3 commit 6d184d7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/sections/create-dataset/CreateDatasetForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { ChangeEvent, FormEvent } from 'react'
import { ChangeEvent, FormEvent, MouseEvent, useEffect } from 'react'
import { Alert, Button, Col, Form, Row } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'
import { RequiredFieldText } from '../shared/form/RequiredFieldText/RequiredFieldText'
import { SeparationLine } from '../shared/layout/SeparationLine/SeparationLine'
import { useCreateDatasetForm, SubmissionStatusEnums } from './useCreateDatasetForm'
import styles from '/src/sections/dataset/Dataset.module.scss'
import { useLoading } from '../loading/LoadingContext'

export function CreateDatasetForm() {
const { formErrors, submissionStatus, updateFormData, submitFormData } = useCreateDatasetForm()
const { isLoading, setIsLoading } = useLoading()
const { formErrors, submissionStatus, updateFormData, submitFormData, cancelFormSubmit } =
useCreateDatasetForm()

const { t } = useTranslation('createDataset')

Expand All @@ -21,6 +24,14 @@ export function CreateDatasetForm() {
submitFormData()
}

const handleFormCancel = (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault()
cancelFormSubmit()
}
useEffect(() => {
setIsLoading(false)
}, [isLoading])

return (
<article>
<header className={styles.header}>
Expand Down Expand Up @@ -66,7 +77,7 @@ export function CreateDatasetForm() {
<Button type="submit" disabled={submissionStatus === SubmissionStatusEnums.IsSubmitting}>
{t('saveButton')}
</Button>
<Button withSpacing variant="secondary">
<Button withSpacing variant="secondary" type="button" onClick={handleFormCancel}>
{t('cancelButton')}
</Button>
</Form>
Expand Down
10 changes: 9 additions & 1 deletion src/sections/create-dataset/useCreateDatasetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DatasetFormFields } from '../../dataset/domain/models/DatasetFormFields
import { createDataset } from '../../dataset/domain/useCases/createDataset'
import { validateDataset } from '../../dataset/domain/useCases/validateDataset'
import { DatasetValidationResponse } from '../../dataset/domain/models/DatasetValidationResponse'
import { useNavigate } from 'react-router-dom'
import { Route } from '../Route.enum'
interface FormContextInterface {
fields: DatasetFormFields
}
Expand Down Expand Up @@ -52,12 +54,18 @@ export function useCreateDatasetForm() {
setSubmissionStatus(SubmissionStatusEnums.Errored)
}
}
const navigate = useNavigate()
const cancelFormSubmit = () => {
const path = Route.HOME
navigate(path)
}

return {
formState,
formErrors,
submissionStatus,
updateFormData,
submitFormData
submitFormData,
cancelFormSubmit
}
}

0 comments on commit 6d184d7

Please sign in to comment.