-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Mangan
committed
Jan 22, 2024
1 parent
4740a0c
commit 25cca4f
Showing
8 changed files
with
111 additions
and
151 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 /> | ||
} | ||
} |
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,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
10
src/stories/dataset/dataset-create/DatasetCreate.stories.tsx
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 |
---|---|---|
@@ -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 /> | ||
} |
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