Skip to content

Commit

Permalink
update file import
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Mangan committed Jan 22, 2024
1 parent 4740a0c commit 25cca4f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 151 deletions.
7 changes: 6 additions & 1 deletion public/locales/en/createDataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
},
"requiredFields": "Asterisks indicate required fields",
"datasetForm": {
"title": "Title"
"title": "Title",
"status": {
"submitting": "Submitting...",
"success": "Form submitted successfully!",
"failed": "Error: Submission failed."
}
},
"datasetFormStates": {
"submitting": "Form Submitting",
Expand Down
8 changes: 4 additions & 4 deletions src/dataset/domain/useCases/createDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export interface FormValidationResult {
errors: Record<keyof CreateDatasetFormFields, string | undefined>
}

export interface FormValidationService {
export interface FormValidation {
validateForm: (fields: CreateDatasetFormFields) => FormValidationResult
}

export const formValidationService: FormValidationService = {
export const formValidation: FormValidation = {
validateForm: (fields: CreateDatasetFormFields): FormValidationResult => {
const errors: Record<keyof CreateDatasetFormFields, string | undefined> = {
createDatasetTitle: undefined
Expand All @@ -28,10 +28,10 @@ export const formValidationService: FormValidationService = {
}
}

export interface FormSubmissionService {
export interface FormSubmission {
submitFormData: (fields: CreateDatasetFormFields) => Promise<void>
}
export const formSubmissionService: FormSubmissionService = {
export const formSubmission: FormSubmission = {
submitFormData: async (fields: CreateDatasetFormFields): Promise<void> => {
console.log('Submitting form data:', fields)
const sendDataMock = (fields: CreateDatasetFormFields) => {
Expand Down
102 changes: 0 additions & 102 deletions src/sections/create-dataset/CreateDatasetContext.tsx

This file was deleted.

15 changes: 4 additions & 11 deletions src/sections/create-dataset/CreateDatasetFactory.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { CreateDatasetFormPresenter } from './CreateDatasetForm'
import { formValidationService } from '../../dataset/domain/useCases/createDataset'
import { formSubmissionService } from '../../dataset/domain/useCases/createDataset'
import { CreateDatasetForm } from './CreateDatasetForm'
import { ReactElement } from 'react'
export const CreateDatasetForm: React.FC = () => {
return (
<CreateDatasetFormPresenter
formValidationService={formValidationService}
formSubmissionService={formSubmissionService}
/>
)
export const CreateDatasetFormPresenter: React.FC = () => {
return <CreateDatasetForm />
}

export class CreateDatasetFactory {
static create(): ReactElement {
return <CreateDatasetForm />
return <CreateDatasetFormPresenter />
}
}
45 changes: 22 additions & 23 deletions src/sections/create-dataset/CreateDatasetForm.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import React from 'react'
import React, { ChangeEvent, FormEvent } from 'react'
import { Alert, Button, Col, Form, Row } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'
import { RequiredFieldText } from '../../components/form/RequiredFieldText/RequiredFieldText'
import { SeparationLine } from '../../components/layout/SeparationLine/SeparationLine'
import { useCreateDatasetForm, SubmissionStatusEnums } from './CreateDatasetContext'
import {
FormValidationService,
FormSubmissionService
} from '../../dataset/domain/useCases/createDataset'
import { useCreateDatasetForm, SubmissionStatusEnums } from './useCreateDatasetForm'
import styles from '/src/sections/dataset/Dataset.module.scss'
interface FormPresenterProps {
formValidationService: FormValidationService
formSubmissionService: FormSubmissionService
}
export const CreateDatasetFormPresenter: React.FC<FormPresenterProps> = ({
formValidationService,
formSubmissionService
}: FormPresenterProps) => {
const {
formErrors,
submissionStatus,
handleCreateDatasetFieldChange,
handleCreateDatasetSubmit
} = useCreateDatasetForm({ formValidationService, formSubmissionService })

export const CreateDatasetForm: React.FC = () => {
const { formErrors, submissionStatus, updateFormData, submitFormData } = 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()
}

return (
<>
<article>
Expand All @@ -35,11 +30,15 @@ export const CreateDatasetFormPresenter: React.FC<FormPresenterProps> = ({
<SeparationLine />
<div className={styles.container}>
<RequiredFieldText />
{submissionStatus === SubmissionStatusEnums.IsSubmitting && <p>Submitting...</p>}
{submissionStatus === SubmissionStatusEnums.IsSubmitting && (
<p>{t('datasetForm.status.submitting')}</p>
)}
{submissionStatus === SubmissionStatusEnums.SubmitComplete && (
<p>Form submitted successfully!</p>
<p>{t('datasetForm.status.success')}</p>
)}
{submissionStatus === SubmissionStatusEnums.Errored && (
<p>{t('datasetForm.status.fail')}</p>
)}
{submissionStatus === SubmissionStatusEnums.Errored && <p>Error: Submission failed.</p>}
<Form
onSubmit={(event) => {
handleCreateDatasetSubmit(event)
Expand Down
66 changes: 66 additions & 0 deletions src/sections/create-dataset/useCreateDatasetForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { useState } from 'react'
import {
CreateDatasetFormFields,
FormValidationResult,
formSubmission,
formValidation
} from '../../dataset/domain/useCases/createDataset'

interface FormContextInterface {
fields: CreateDatasetFormFields
}

const defaultFormState: CreateDatasetFormFields = {
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 CreateDatasetFormFields, string | undefined>
>({ createDatasetTitle: undefined })

const updateFormData = (updatedFormData: object) => {
setFormState((prevState) => ({
...prevState,
fields: { ...prevState.fields, ...updatedFormData }
}))
}

const submitFormData = () => {
setSubmissionStatus(SubmissionStatusEnums.IsSubmitting)

const validationResult: FormValidationResult = formValidation.validateForm(formState.fields)

if (validationResult.isValid) {
formSubmission
.submitFormData(formState.fields)
.then(() => setSubmissionStatus(SubmissionStatusEnums.IsSubmitting))
.catch(() => setSubmissionStatus(SubmissionStatusEnums.Errored))
.finally(() => setSubmissionStatus(SubmissionStatusEnums.SubmitComplete))
} else {
setFormErrors(validationResult.errors)
setSubmissionStatus(SubmissionStatusEnums.Errored)
}
}

return {
formState,
formErrors,
submissionStatus,
updateFormData,
submitFormData
}
}
10 changes: 5 additions & 5 deletions src/stories/dataset/dataset-create/DatasetCreate.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { StoryObj, Meta } from '@storybook/react'
import { CreateDatasetForm } from '../../../sections/create-dataset/CreateDatasetFactory'
import { CreateDatasetFormPresenter } from '../../../sections/create-dataset/CreateDatasetFactory'
import { WithLayout } from '../../WithLayout'
import { WithI18next } from '../../WithI18next'

const meta: Meta<typeof CreateDatasetForm> = {
const meta: Meta<typeof CreateDatasetFormPresenter> = {
title: 'Pages/Create Dataset',
component: CreateDatasetForm,
component: CreateDatasetFormPresenter,
decorators: [WithI18next, WithLayout]
}
export default meta
type Story = StoryObj<typeof CreateDatasetForm>
type Story = StoryObj<typeof CreateDatasetFormPresenter>

export const Default: Story = {
render: () => <CreateDatasetForm />
render: () => <CreateDatasetFormPresenter />
}
9 changes: 4 additions & 5 deletions tests/component/views/create-dataset/CreateDataset.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// import DatasetCreate from '../../../../src/sections/create-dataset/CreateDatasetContext'
import { CreateDatasetForm } from '../../../../src/sections/create-dataset/CreateDatasetFactory'
import { CreateDatasetFormPresenter } from '../../../../src/sections/create-dataset/CreateDatasetFactory'
import { mount } from 'cypress/react18'

describe('Form component', () => {
it('renders the Create Dataset page and its contents', () => {
mount(<CreateDatasetForm />)
cy.customMount(<CreateDatasetFormPresenter />)
cy.findByText(/Create Dataset/i).should('exist')

cy.findByLabelText(/Title/i).should('exist')
Expand All @@ -15,8 +14,8 @@ describe('Form component', () => {
})

it('can submit a valid form', () => {
cy.log('Submit form')
mount(<CreateDatasetForm />)
// cy.log('Submit form')
mount(<CreateDatasetFormPresenter />)
cy.findByLabelText(/Title/i)
.should('exist')
.type('Test Dataset Title')
Expand Down

0 comments on commit 25cca4f

Please sign in to comment.