Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/231 create dataset boilerplate #251

Merged
merged 34 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
69327f6
initial commit for boilerplate
Dec 5, 2023
f402382
Added basic tests
Dec 5, 2023
6a9cdce
updating tests
Dec 12, 2023
c384768
update route for Create Dataset
Dec 12, 2023
8d1049f
fixed suggested items from code review. Added better component tests
Dec 13, 2023
c003229
Merge branch 'develop' into feature/231-create-dataset-boilerplate
Dec 14, 2023
54ae319
Merge branch 'develop' into feature/231-create-dataset-boilerplate
Dec 14, 2023
4332efd
Merge branch 'feature/231-create-dataset-boilerplate' of https://gith…
Dec 14, 2023
5f4eb87
removed data-cy attributes from elements
Dec 15, 2023
d0c9af4
updated some file locations
Dec 20, 2023
fcb983e
Added comments and some placeholders
Dec 21, 2023
7f860cf
Created new tests, renamed and relocated use-case file, made updates …
Jan 4, 2024
45578bf
removed comments, changed names for build error
Jan 4, 2024
97acbe9
change Cypress version
Jan 5, 2024
4da6060
removing comments and redundant code from createDataset files
Jan 5, 2024
1a3dd8a
renamed Forms directory
Jan 5, 2024
bdc6e93
updated RequiredFieldText to use translation
Jan 5, 2024
59a8dbb
merge with develop
Jan 8, 2024
e96981e
fix for e2e test condition failing
Jan 8, 2024
3908945
Router changes from Melina, strict type checking
Jan 9, 2024
ea8466d
Refactored form component, added dedicated provider and context methods
Jan 12, 2024
6cb09b3
Merge branch 'develop' into feature/231-create-dataset-boilerplate
Jan 12, 2024
be5388c
changing story
Jan 12, 2024
e4507a0
Update router enums
Jan 12, 2024
ebc2b36
update story to reflect removed attributes on form
Jan 18, 2024
bc14953
re-added required attr to match spec after testing
Jan 18, 2024
4740a0c
merge from develop
Jan 19, 2024
25cca4f
update file import
Jan 22, 2024
b695feb
recast function declarations for components
Jan 22, 2024
4c717f3
relocate files to re-align with current best-practices
Jan 23, 2024
609afd4
renaming use cases to align with prev code
Jan 23, 2024
99b99f5
updated imports in Storybook
Jan 26, 2024
6de18a3
Removed redundant factory function, updated formatting in router
Jan 29, 2024
6d184d7
Add isLoading considerations, add event handler for Cancel btn
Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ interface FormInputProps extends React.HTMLAttributes<FormInputElement> {
type?: 'text' | 'email' | 'password'
readOnly?: boolean
withinMultipleFieldsGroup?: boolean
name?: string
}

export function FormInput({
type = 'text',
name,
readOnly,
withinMultipleFieldsGroup,
...props
}: FormInputProps) {
return (
<FormElementLayout withinMultipleFieldsGroup={withinMultipleFieldsGroup}>
<FormBS.Control type={type} readOnly={readOnly} plaintext={readOnly} {...props} />
<FormBS.Control name={name} type={type} readOnly={readOnly} plaintext={readOnly} {...props} />
</FormElementLayout>
)
}
16 changes: 16 additions & 0 deletions public/locales/en/createDataset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"pageTitle": "Create Dataset",
"metadataTip": {
"title": "Metadata Tip",
"content": "After adding the dataset, click the Edit Dataset button to add more metadata."
},
"datasetForm": {
"title": "Title"
},
"datasetFormStates": {
"submitting": "Form Submitting",
"submissionSuccess": "Form submission successful"
},
"saveButton": "Save Dataset",
"cancelButton": "Cancel"
}
7 changes: 7 additions & 0 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { HelloDataverse } from './sections/hello-dataverse/HelloDataverse'
import { Layout } from './sections/layout/Layout'
import { Route } from './sections/Route.enum'
import { DatasetFactory } from './sections/dataset/DatasetFactory'
import { PageNotFound } from './sections/page-not-found/PageNotFound'
import CreateDatasetContainer from './views/create-dataset/CreateDatasetContainer'

const router = createBrowserRouter(
[
{
path: '/',
element: <Layout />,
errorElement: <PageNotFound />,
children: [
{
path: Route.HOME,
Expand All @@ -17,6 +20,10 @@ const router = createBrowserRouter(
{
path: `${Route.DATASETS}`,
element: DatasetFactory.create()
},
{
path: `${Route.DATASETS}/${Route.CREATE}`,
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
element: <CreateDatasetContainer />
}
]
}
Expand Down
File renamed without changes
File renamed without changes.
20 changes: 20 additions & 0 deletions src/components/Forms/RequiredFieldText/RequiredFieldText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function RequiredFieldText() {
return (
<p>
<RequiredInputSymbol />
Asterisks indicate required fields
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
</p>
)
}

// TODO: Why can't I export this object from Storybook?
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
// Needed to duplicate it on the frontend to be able to use.

const RequiredInputSymbol = () => {
return (
<span role="img" aria-label="Required input symbol" style={{ color: '#a94442' }}>
{' '}
*
</span>
)
}
4 changes: 4 additions & 0 deletions src/components/ui/SeparationLine/SeparationLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import styles from '/src/sections/dataset/Dataset.module.scss'
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
export function SeparationLine() {
return <div className={styles['separation-line']}></div>
}
3 changes: 2 additions & 1 deletion src/sections/Route.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export enum Route {
SIGN_UP = '/dataverseuser.xhtml?editMode=CREATE&redirectPage=%2Fdataverse.xhtml',
LOG_IN = '/loginpage.xhtml?redirectPage=%2Fdataverse.xhtml',
LOG_OUT = '/',
DATASETS = 'datasets'
DATASETS = 'datasets',
CREATE = 'create'
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion src/sections/hello-dataverse/HelloDataverse.module.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import "node_modules/@iqss/dataverse-design-system/src/lib/assets/styles/design-tokens/colors.module";
@import "src/sections/assets/variables";
@import "src/assets/variables";

.container {
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion src/sections/hello-dataverse/HelloDataverse.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logo from '../assets/logo.svg'
import logo from '../../assets/logo.svg'
import styles from './HelloDataverse.module.scss'
import { Trans, useTranslation } from 'react-i18next'

Expand Down
2 changes: 1 addition & 1 deletion src/sections/layout/Layout.module.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import "src/sections/assets/variables";
@import "src/assets/variables";

.body-container {
min-height: $body-available-height;
Expand Down
2 changes: 1 addition & 1 deletion src/sections/layout/header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logo from '../../assets/logo.svg'
import logo from '../../../assets/logo.svg'
import { useTranslation } from 'react-i18next'
import { Navbar } from '@iqss/dataverse-design-system'
import { Route } from '../../Route.enum'
Expand Down
107 changes: 107 additions & 0 deletions src/views/create-dataset/CreateDataset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// components/FormPresenter.tsx

MellyGray marked this conversation as resolved.
Show resolved Hide resolved
import { ChangeEvent, FormEvent } from 'react'
import { Alert, Button, Col, Form, Row } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'
// import { DatasetSkeleton } from '../../sections/dataset/DatasetSkeleton'
// import { useLoading } from '../../sections/loading/LoadingContext'
import styles from '/src/sections/dataset/Dataset.module.scss'
// import { FormInputElement } from '@iqss/dataverse-design-system/src/lib/components/form/form-group/form-element/FormInput'
import { SeparationLine } from '../../components/ui/SeparationLine/SeparationLine'
import { RequiredFieldText } from '../../components/Forms/RequiredFieldText/RequiredFieldText'

/*
* TODO:
* Break out different components
* Find submit action source
* cleanup state objects and submit process
* show submitted info in console
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
* Loading state management
*/

// See comments about https://rjsf-team.github.io/react-jsonschema-form/
// https://github.com/IQSS/dataverse-frontend/issues/231

// const [submitting, setSubmitting] = useState(false)
// const [submitStatus, setSubmitStatus] = useState(false)

interface CreateDatasetFormPresenterProps {
formData: { createDatasetTitle: string }
handleCreateDatasetFieldChange: (event: ChangeEvent<HTMLInputElement>) => void
handleCreateDatasetSubmit: (event: FormEvent<HTMLFormElement>) => void
submitting: boolean
submitComplete: boolean
}
export default function CreateDatasetFormPresenter({
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
// formData,
handleCreateDatasetFieldChange,
handleCreateDatasetSubmit,
submitting,
submitComplete
}: CreateDatasetFormPresenterProps) {
const { t } = useTranslation('createDataset')
// TODO: Probably replace this with a FormSkeleton or remove entirely
// if (loading) {
// return <DatasetSkeleton />
// }
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
return (
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
<article>
<header className={styles.header}>
<h1>{t('pageTitle')}</h1>
</header>
<SeparationLine />
<div className={styles.container}>
<RequiredFieldText />
<Form onSubmit={handleCreateDatasetSubmit}>
{submitComplete && <div>Form Submitted!</div>}
<Row>
<Col md={9}>
<Form.Group controlId="createDatasetTitle" required>
<Form.Group.Label>{t('datasetForm.title')}</Form.Group.Label>
<Form.Group.Input
readOnly={submitting && true}
data-cy="datasetFormInputTitle"
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
type="text"
// FIX: Err - Property 'name' does not exist on type 'IntrinsicAttributes & FormInputProps'.
name="createDatasetTitle"
placeholder="Dataset Title"
onChange={handleCreateDatasetFieldChange}
withinMultipleFieldsGroup={false}
/>
</Form.Group>
</Col>
</Row>
<SeparationLine />
<Alert
key={1}
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
variant={'info'}
customHeading={t('metadataTip.title')}
dismissible={false}>
{t('metadataTip.content')}
</Alert>
{/* SRC: https://github.com/IQSS/dataverse/blob/develop/src/main/webapp/dataset.xhtml */}
{/* <p:commandButton id="saveBottom" styleClass="btn btn-default" value="#{DatasetPage.editMode == 'CREATE' ? bundle['file.addBtn'] : bundle.saveChanges}" rendered="#{DatasetPage.editMode == 'CREATE' or DatasetPage.editMode == 'METADATA'}"
onclick="PF('blockDatasetForm').show();"
update=":datasetForm,:messagePanel"
oncomplete="$(document).scrollTop(0);"
action="#{DatasetPage.save}"
/> */}
{/* Where is datasetSaveCommand() */}
{/* <p:remoteCommand
name="datasetSaveCommand"
action="#{DatasetPage.save()}"
update=":datasetForm,:messagePanel"
/> */}
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
<Button type="submit" data-cy="datasetFormSubmit">
{t('saveButton')}
</Button>
<Button withSpacing variant="secondary" data-cy="datasetFormCancel">
{t('cancelButton')}
</Button>
</Form>
</div>
</article>
)
}

// export default CreateDatasetFormPresenter
77 changes: 77 additions & 0 deletions src/views/create-dataset/CreateDatasetContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// containers/FormContainer.tsx

MellyGray marked this conversation as resolved.
Show resolved Hide resolved
import React, { useState, ChangeEvent, FormEvent } from 'react'
import { FormInputElement } from '@iqss/dataverse-design-system/src/lib/components/form/form-group/form-element/FormInput'
import CreateDatasetUseCase from './CreateDatasetUseCase'
import CreateDatasetFormPresenter from './CreateDataset'

const CreateDatasetContainer: React.FC = () => {
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
const [formData, setFormData] = useState({
createDatasetTitle: ''
})

const [submitComplete, setSubmitComplete] = useState(false)

const [submitting, setSubmitting] = useState(false)

const handleCreateDatasetFieldChange = (event: ChangeEvent<FormInputElement>): void => {
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
const { name, value } = event.target
setFormData((prevData) => ({
...prevData,
[name]: value
}))
}

const handleCreateDatasetSubmit = async (event: FormEvent<HTMLFormElement>): Promise<void> => {
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
event.preventDefault()

const formUseCase = new CreateDatasetUseCase()

console.log('handleCreateDatasetSubmit() formData | ', formData)

if (formUseCase.validateCreateDatasetFormData(formData)) {
try {
setSubmitting(true)
console.log('Status: Submitting | ', submitting)

// Simulate an asynchronous operation, e.g., API call
await new Promise((resolve) => setTimeout(resolve, 3000))
const result = await formUseCase.submitCreateDatasetFormData(formData)
console.log(result)
setSubmitComplete(true)
// Handle success, e.g., show a success message
} catch (error) {
console.error('Error submitting form:', error)
// Handle error, e.g., show an error message
} finally {
setSubmitting(false)
}
} else {
// Handle validation error, e.g., show validation messages
console.error('Form validation failed')
}
// Explicitly return a Promise<void>
return Promise.resolve()
}
MellyGray marked this conversation as resolved.
Show resolved Hide resolved

return (
// TODO: conditional view, needs to be lower in the component heirarchy
// {submitting && <div>Submtting Form...</div>}
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
<>
{submitting && <p>Submitting...</p>}
{!submitting && (
<CreateDatasetFormPresenter
submitComplete={submitComplete}
formData={formData}
handleCreateDatasetFieldChange={handleCreateDatasetFieldChange}
// TODO: Fix using IIFE (Immediately-invoked Function Expression)
// eslint-disable-next-line @typescript-eslint/no-misused-promises
handleCreateDatasetSubmit={handleCreateDatasetSubmit}
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
submitting={submitting}
/>
)}
</>
)
}

export default CreateDatasetContainer
23 changes: 23 additions & 0 deletions src/views/create-dataset/CreateDatasetUseCase.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// usecases/FormUseCase.js

'use strict'

interface CreateDatasetFormData {
createDatasetTitle: string
}

class CreateDatasetUseCase {
validateCreateDatasetFormData(formData: CreateDatasetFormData): boolean {
// Add your validation logic here
// For simplicity, assuming all fields are required
return formData.createDatasetTitle !== undefined && formData.createDatasetTitle.trim() !== ''
}

async submitCreateDatasetFormData(_formData: CreateDatasetFormData): Promise<string> {
// Add your business logic for form submission here
// For simplicity, returning a success message
return Promise.resolve('Form submitted successfully!')
}
}

export default CreateDatasetUseCase
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions tests/component/views/create-dataset/CreateDataset.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import CreateDatasetContainer from '../../../../src/views/create-dataset/CreateDatasetContainer'

describe('Form component', () => {
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
it('should render all form fields', () => {
cy.customMount(<CreateDatasetContainer />)
cy.get('[data-cy=datasetFormInputTitle]').should('be.visible')
cy.get('[data-cy=datasetFormSubmit]').should('be.visible')
cy.get('[data-cy=datasetFormCancel]').should('be.visible')
})

it('should submit the form with correct values', () => {
cy.customMount(<CreateDatasetContainer />)

cy.get('[data-cy=datasetFormInputTitle]').type('Test Dataset Title')

cy.get('[data-cy=datasetFormSubmit]').click()

// Wait for the form submission to complete (adjust the timeout as needed)
cy.wait(4000)

// Assert that the form was successfully submitted
cy.contains('Form Submitted!')
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
})
})
Loading