forked from uncch-rdmc/dataverse-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request IQSS#251 from IQSS/feature/231-create-dataset-boil…
…erplate Feature/231 create dataset boilerplate
- Loading branch information
Showing
27 changed files
with
337 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"pageTitle": "Create Dataset", | ||
"metadataTip": { | ||
"title": "Metadata Tip", | ||
"content": "After adding the dataset, click the Edit Dataset button to add more metadata." | ||
}, | ||
"requiredFields": "Asterisks indicate required fields", | ||
"datasetForm": { | ||
"title": "Title", | ||
"status": { | ||
"submitting": "Submitting...", | ||
"success": "Form submitted successfully!", | ||
"failed": "Error: Submission failed." | ||
} | ||
}, | ||
"datasetFormStates": { | ||
"submitting": "Form Submitting", | ||
"submissionSuccess": "Form submission successful" | ||
}, | ||
"saveButton": "Save Dataset", | ||
"cancelButton": "Cancel" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface DatasetFormFields { | ||
createDatasetTitle: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { DatasetFormFields } from '../models/DatasetFormFields' | ||
|
||
export interface DatasetValidationResponse { | ||
isValid: boolean | ||
errors: Record<keyof DatasetFormFields, string | undefined> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DatasetFormFields } from '../models/DatasetFormFields' | ||
import { DatasetRepository } from '../repositories/DatasetRepository' | ||
import { DatasetJSDataverseRepository } from '../../infrastructure/repositories/DatasetJSDataverseRepository' | ||
|
||
const repo = new DatasetJSDataverseRepository() | ||
function createDatasetMockHelper( | ||
datasetRepository: DatasetRepository, | ||
formFieldsToSubmit: DatasetFormFields | ||
): Promise<string> { | ||
return datasetRepository.createDataset(formFieldsToSubmit).catch((error: Error) => { | ||
throw new Error(error.message) | ||
}) | ||
} | ||
|
||
export function createDataset(fields: DatasetFormFields): Promise<string> { | ||
return createDatasetMockHelper(repo, fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { DatasetFormFields } from '../models/DatasetFormFields' | ||
import { DatasetValidationResponse } from '../models/DatasetValidationResponse' | ||
const NAME_REQUIRED = 'Name is required' | ||
|
||
export function validateDataset(fieldsToSubmit: DatasetFormFields) { | ||
const errors: Record<keyof DatasetFormFields, string | undefined> = { | ||
createDatasetTitle: undefined | ||
} | ||
|
||
if (!fieldsToSubmit.createDatasetTitle) { | ||
errors.createDatasetTitle = NAME_REQUIRED | ||
} | ||
|
||
const validationResponse: DatasetValidationResponse = { | ||
isValid: Object.values(errors).every((error) => error === undefined), | ||
errors | ||
} | ||
return validationResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { CreateDatasetForm } from './CreateDatasetForm' | ||
import { ReactElement } from 'react' | ||
|
||
export class CreateDatasetFactory { | ||
static create(): ReactElement { | ||
return <CreateDatasetForm /> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 { isLoading, setIsLoading } = useLoading() | ||
const { formErrors, submissionStatus, updateFormData, submitFormData, cancelFormSubmit } = | ||
useCreateDatasetForm() | ||
|
||
const { t } = useTranslation('createDataset') | ||
|
||
const handleCreateDatasetFieldChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const { name, value } = event.target | ||
updateFormData({ [name]: value }) | ||
} | ||
|
||
const handleCreateDatasetSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault() | ||
submitFormData() | ||
} | ||
|
||
const handleFormCancel = (event: MouseEvent<HTMLButtonElement>) => { | ||
event.preventDefault() | ||
cancelFormSubmit() | ||
} | ||
useEffect(() => { | ||
setIsLoading(false) | ||
}, [isLoading]) | ||
|
||
return ( | ||
<article> | ||
<header className={styles.header}> | ||
<h1>{t('pageTitle')}</h1> | ||
</header> | ||
<SeparationLine /> | ||
<div className={styles.container}> | ||
<RequiredFieldText /> | ||
{submissionStatus === SubmissionStatusEnums.IsSubmitting && ( | ||
<p>{t('datasetForm.status.submitting')}</p> | ||
)} | ||
{submissionStatus === SubmissionStatusEnums.SubmitComplete && ( | ||
<p>{t('datasetForm.status.success')}</p> | ||
)} | ||
{submissionStatus === SubmissionStatusEnums.Errored && ( | ||
<p>{t('datasetForm.status.fail')}</p> | ||
)} | ||
<Form | ||
onSubmit={(event: FormEvent<HTMLFormElement>) => { | ||
handleCreateDatasetSubmit(event) | ||
}} | ||
className={'create-dataset-form'}> | ||
<Row> | ||
<Col md={9}> | ||
<Form.Group controlId="createDatasetTitle" required> | ||
<Form.Group.Label>{t('datasetForm.title')}</Form.Group.Label> | ||
<Form.Group.Input | ||
readOnly={submissionStatus === SubmissionStatusEnums.IsSubmitting && true} | ||
type="text" | ||
name="createDatasetTitle" | ||
placeholder="Dataset Title" | ||
onChange={handleCreateDatasetFieldChange} | ||
withinMultipleFieldsGroup={false} | ||
/> | ||
</Form.Group> | ||
{formErrors.createDatasetTitle && <span>{formErrors.createDatasetTitle}</span>} | ||
</Col> | ||
</Row> | ||
<SeparationLine /> | ||
<Alert variant={'info'} customHeading={t('metadataTip.title')} dismissible={false}> | ||
{t('metadataTip.content')} | ||
</Alert> | ||
<Button type="submit" disabled={submissionStatus === SubmissionStatusEnums.IsSubmitting}> | ||
{t('saveButton')} | ||
</Button> | ||
<Button withSpacing variant="secondary" type="button" onClick={handleFormCancel}> | ||
{t('cancelButton')} | ||
</Button> | ||
</Form> | ||
</div> | ||
</article> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useState } from 'react' | ||
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 | ||
} | ||
|
||
const defaultFormState: DatasetFormFields = { | ||
createDatasetTitle: '' | ||
} | ||
|
||
export enum SubmissionStatusEnums { | ||
NotSubmitted = 'NotSubmitted', | ||
IsSubmitting = 'IsSubmitting', | ||
SubmitComplete = 'SubmitComplete', | ||
Errored = 'Errored' | ||
} | ||
|
||
export function useCreateDatasetForm() { | ||
const [formState, setFormState] = useState<FormContextInterface>({ | ||
fields: defaultFormState | ||
}) | ||
|
||
const [submissionStatus, setSubmissionStatus] = useState<SubmissionStatusEnums>( | ||
SubmissionStatusEnums.NotSubmitted | ||
) | ||
const [formErrors, setFormErrors] = useState<Record<keyof DatasetFormFields, string | undefined>>( | ||
{ createDatasetTitle: undefined } | ||
) | ||
|
||
const updateFormData = (updatedFormData: object) => { | ||
setFormState((prevState) => ({ | ||
...prevState, | ||
fields: { ...prevState.fields, ...updatedFormData } | ||
})) | ||
} | ||
|
||
const submitFormData = () => { | ||
setSubmissionStatus(SubmissionStatusEnums.IsSubmitting) | ||
|
||
const validationResult: DatasetValidationResponse = validateDataset(formState.fields) | ||
|
||
if (validationResult.isValid) { | ||
createDataset(formState.fields) | ||
.then(() => setSubmissionStatus(SubmissionStatusEnums.IsSubmitting)) | ||
.catch(() => setSubmissionStatus(SubmissionStatusEnums.Errored)) | ||
.finally(() => setSubmissionStatus(SubmissionStatusEnums.SubmitComplete)) | ||
} else { | ||
setFormErrors(validationResult.errors) | ||
setSubmissionStatus(SubmissionStatusEnums.Errored) | ||
} | ||
} | ||
const navigate = useNavigate() | ||
const cancelFormSubmit = () => { | ||
const path = Route.HOME | ||
navigate(path) | ||
} | ||
|
||
return { | ||
formState, | ||
formErrors, | ||
submissionStatus, | ||
updateFormData, | ||
submitFormData, | ||
cancelFormSubmit | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,4 @@ | |
|
||
.tab-container { | ||
padding: 1em 0; | ||
} | ||
|
||
.separation-line { | ||
margin: 1em 0; | ||
border-bottom: 1px solid #dee2e6; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.