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 9 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
5 changes: 3 additions & 2 deletions packages/design-system/src/lib/components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { FormGroupWithMultipleFields } from './form-group-multiple-fields/FormGr
import { FormInputGroup } from './form-group/form-input-group/FormInputGroup'

interface FormProps {
className?: string
validated?: boolean
onSubmit?: (event: FormEvent<HTMLFormElement>) => void
}

function Form({ validated, onSubmit, children }: PropsWithChildren<FormProps>) {
function Form({ validated, onSubmit, children, className }: PropsWithChildren<FormProps>) {
return (
<FormBS validated={validated} onSubmit={onSubmit}>
<FormBS validated={validated} onSubmit={onSubmit} className={className}>
{children}
</FormBS>
)
Expand Down
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>
)
}
1 change: 1 addition & 0 deletions packages/design-system/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export { Icon } from './components/icon/Icon'
export { IconName } from './components/icon/IconName'
export { Tooltip } from './components/tooltip/Tooltip'
export { Pagination } from './components/pagination/Pagination'
export { RequiredInputSymbol } from './components/form/required-input-symbol/RequiredInputSymbol'
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.
10 changes: 10 additions & 0 deletions src/components/Forms/RequiredFieldText/RequiredFieldText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RequiredInputSymbol } from '@iqss/dataverse-design-system'

export function RequiredFieldText() {
return (
<p>
<RequiredInputSymbol />
Asterisks indicate required fields
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
</p>
)
}
4 changes: 4 additions & 0 deletions src/components/ui/SeparationLine/SeparationLine.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.separation-line {
margin: 1em 0;
border-bottom: 1px solid #dee2e6;
}
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
}
5 changes: 0 additions & 5 deletions src/sections/dataset/Dataset.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@

.tab-container {
padding: 1em 0;
}

.separation-line {
margin: 1em 0;
border-bottom: 1px solid #dee2e6;
}
3 changes: 2 additions & 1 deletion src/sections/dataset/Dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useEffect } from 'react'
import { DatasetAlerts } from './dataset-alerts/DatasetAlerts'
import { useNotImplementedModal } from '../not-implemented/NotImplementedModalContext'
import { NotImplementedModal } from '../not-implemented/NotImplementedModal'
import { SeparationLine } from '../../components/ui/SeparationLine/SeparationLine'

interface DatasetProps {
fileRepository: FileRepository
Expand Down Expand Up @@ -92,7 +93,7 @@ export function Dataset({ fileRepository }: DatasetProps) {
</div>
</Tabs.Tab>
</Tabs>
<div className={styles['separation-line']}></div>
<SeparationLine />
</div>
</article>
)}
Expand Down
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
71 changes: 71 additions & 0 deletions src/views/create-dataset/CreateDataset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 styles from '/src/sections/dataset/Dataset.module.scss'
import { SeparationLine } from '../../components/ui/SeparationLine/SeparationLine'
import { RequiredFieldText } from '../../components/Forms/RequiredFieldText/RequiredFieldText'

/*
* TODO:
* out-of-scope: Find submit action source
* out-of-scope: Loading state management
*/

MellyGray marked this conversation as resolved.
Show resolved Hide resolved
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
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} className={'create-dataset-form'}>
{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}
type="text"
name="createDatasetTitle"
placeholder="Dataset Title"
onChange={handleCreateDatasetFieldChange}
withinMultipleFieldsGroup={false}
/>
</Form.Group>
</Col>
</Row>
<SeparationLine />
<Alert variant={'info'} customHeading={t('metadataTip.title')} dismissible={false}>
{t('metadataTip.content')}
</Alert>
<Button type="submit">{t('saveButton')}</Button>
<Button withSpacing variant="secondary">
{t('cancelButton')}
</Button>
</Form>
</div>
</article>
)
}
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
22 changes: 22 additions & 0 deletions tests/component/views/create-dataset/CreateDataset.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import CreateDatasetContainer from '../../../../src/views/create-dataset/CreateDatasetContainer'

describe('Form component', () => {
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(() => {
cy.customMount(<CreateDatasetContainer />)
})
it('can submit a valid form', () => {
cy.findByText(/Create Dataset/i).should('exist')
cy.findByText(/Title/i).should('exist')

cy.get('input[name="createDatasetTitle"]')
.should('exist')
.type('Test Dataset Title')
.should('have.attr', 'required', 'required')
.and('have.value', 'Test Dataset Title')
M27Mangan marked this conversation as resolved.
Show resolved Hide resolved
cy.findByText(/Save Dataset/i).should('exist')
cy.findByText(/Cancel/i).should('exist')
cy.log('Submit form')
cy.findByText(/Save Dataset/i).click()
cy.findByText('Form Submitted!', { timeout: 5000 })
})
})
Loading