Skip to content

Commit

Permalink
refactor: Use Formik + Yup in CreateCollectionModal
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Sep 9, 2024
1 parent 36f2e8b commit aa870b4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 85 deletions.
12 changes: 3 additions & 9 deletions src/library-authoring/LibraryAuthoringPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -731,18 +731,14 @@ describe('<LibraryAuthoringPage />', () => {
const createButton = screen.getByRole('button', { name: /create/i });
fireEvent.click(createButton);

expect(screen.getByText(/collection name is required/i)).toBeInTheDocument();
expect(await screen.findByText(/collection name is required/i)).toBeInTheDocument();
});

it('should show error on conflict response', async () => {
it('should show error on create collection', async () => {
const title = 'This is a Test';
const description = 'This is the description of the Test';
const url = getLibraryCollectionsApiUrl(libraryData.id);
axiosMock.onPost(url).reply(409, {
customAttributes: {
httpErrorStatus: 409,
},
});
axiosMock.onPost(url).reply(500);
await renderLibraryPage();

expect(await screen.findByRole('heading')).toBeInTheDocument();
Expand All @@ -767,7 +763,5 @@ describe('<LibraryAuthoringPage />', () => {
fireEvent.change(nameField, { target: { value: title } });
fireEvent.change(descriptionField, { target: { value: description } });
fireEvent.click(createButton);

expect(await screen.findByText(/there is another collection with the same name/i)).toBeInTheDocument();
});
});
149 changes: 73 additions & 76 deletions src/library-authoring/create-collection/CreateCollectionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
ModalDialog,
} from '@openedx/paragon';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Formik } from 'formik';
import * as Yup from 'yup';
import FormikControl from '../../generic/FormikControl';
import { useParams } from 'react-router-dom';

Check failure on line 12 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

`react-router-dom` import should occur before import of `../../generic/FormikControl`

Check failure on line 12 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

`react-router-dom` import should occur before import of `../../generic/FormikControl`
import { LibraryContext } from '../common/context';
import messages from './messages';
Expand All @@ -22,51 +25,24 @@ const CreateCollectionModal = () => {
} = React.useContext(LibraryContext);
const { showToast } = React.useContext(ToastContext);

const [collectionName, setCollectionName] = React.useState<string | null>(null);
const [collectionNameInvalidMsg, setCollectionNameInvalidMsg] = React.useState<string | null>(null);
const [collectionDescription, setCollectionDescription] = React.useState<string | null>(null);
const [isCreatingCollection, setIsCreatingCollection] = React.useState<boolean>(false);

const handleNameOnChange = React.useCallback((value : string) => {
setCollectionName(value);
setCollectionNameInvalidMsg(null);
}, []);

const handleOnClose = React.useCallback(() => {
closeCreateCollectionModal();
setCollectionNameInvalidMsg(null);
setCollectionName(null);
setCollectionDescription(null);
setIsCreatingCollection(false);
}, []);

const handleCreate = React.useCallback(() => {
if (collectionName === null || collectionName === '') {
setCollectionNameInvalidMsg(
intl.formatMessage(messages.createCollectionModalNameInvalid),
);
return;
}

const handleCreate = React.useCallback((values) => {
setIsCreatingCollection(true);

create.mutateAsync({
title: collectionName,
description: collectionDescription || '',
}).then(() => {
create.mutateAsync(values).then(() => {
handleOnClose();
showToast(intl.formatMessage(messages.createCollectionSuccess));
}).catch((err) => {
}).catch(() => {
setIsCreatingCollection(false);
if (err.response.status === 409) {
setCollectionNameInvalidMsg(
intl.formatMessage(messages.createCollectionModalNameConflict),
);
} else {
showToast(intl.formatMessage(messages.createCollectionError));
}
showToast(intl.formatMessage(messages.createCollectionError));
});
}, [collectionName, collectionDescription]);
}, []);

return (
<ModalDialog
Expand All @@ -83,50 +59,71 @@ const CreateCollectionModal = () => {
</ModalDialog.Title>
</ModalDialog.Header>

<ModalDialog.Body className="mw-sm">
<Form>
<Form.Group>
<Form.Label className="font-weight-bold h3">
{intl.formatMessage(messages.createCollectionModalNameLabel)}
</Form.Label>
<Form.Control
placeholder={intl.formatMessage(messages.createCollectionModalNamePlaceholder)}
value={collectionName}
onChange={(e) => handleNameOnChange(e.target.value)}
/>
{ collectionNameInvalidMsg && (
<Form.Control.Feedback type="invalid">
{collectionNameInvalidMsg}
</Form.Control.Feedback>
)}
</Form.Group>
<Form.Group>
<Form.Label className="font-weight-bold h3">
{intl.formatMessage(messages.createCollectionModalDescriptionLabel)}
</Form.Label>
<Form.Control
as="textarea"
placeholder={intl.formatMessage(messages.createCollectionModalDescriptionPlaceholder)}
value={collectionDescription}
onChange={(e) => setCollectionDescription(e.target.value)}
rows="5"
/>
<Form.Text muted>
{intl.formatMessage(messages.createCollectionModalDescriptionDetails)}
</Form.Text>
</Form.Group>
</Form>
</ModalDialog.Body>
<ModalDialog.Footer>
<ActionRow>
<ModalDialog.CloseButton variant="tertiary">
{intl.formatMessage(messages.createCollectionModalCancel)}
</ModalDialog.CloseButton>
<Button variant="primary" onClick={handleCreate} disabled={isCreatingCollection}>
{intl.formatMessage(messages.createCollectionModalCreate)}
</Button>
</ActionRow>
</ModalDialog.Footer>
<Formik
initialValues={{

Check failure on line 63 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

Expected indentation of 8 space characters but found 10

Check failure on line 63 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

Expected indentation of 8 space characters but found 10
title: '',
description: '',
}}
validationSchema={

Check failure on line 67 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

Expected indentation of 8 space characters but found 10

Check failure on line 67 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

Expected indentation of 8 space characters but found 10
Yup.object().shape({
title: Yup.string()
.required(intl.formatMessage(messages.createCollectionModalNameInvalid)),
description: Yup.string(),
})
}
onSubmit={(values) => handleCreate(values)}

Check failure on line 74 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

Expected indentation of 8 space characters but found 10

Check failure on line 74 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

Expected indentation of 8 space characters but found 10
>

Check failure on line 75 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

The closing bracket must be aligned with the line containing the opening tag (expected column 7)

Check failure on line 75 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

The closing bracket must be aligned with the line containing the opening tag (expected column 7)
{(formikProps) => (
<>
<ModalDialog.Body className="mw-sm">
<Form onSubmit={formikProps.handleSubmit}>
<FormikControl
name="title"
label={
<Form.Label className="font-weight-bold h3">

Check failure on line 83 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

Missing parentheses around multilines JSX

Check failure on line 83 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

Missing parentheses around multilines JSX
{intl.formatMessage(messages.createCollectionModalNameLabel)}
</Form.Label>
}
value={formikProps.values.title}
placeholder={intl.formatMessage(messages.createCollectionModalNamePlaceholder)}
help=""
className=""
controlClasses="pb-2"
/>

Check failure on line 92 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

Trailing spaces not allowed

Check failure on line 92 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

Trailing spaces not allowed
<FormikControl
name="description"
as="textarea"
label={
<Form.Label className="font-weight-bold h3">

Check failure on line 97 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (18)

Missing parentheses around multilines JSX

Check failure on line 97 in src/library-authoring/create-collection/CreateCollectionModal.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

Missing parentheses around multilines JSX
{intl.formatMessage(messages.createCollectionModalDescriptionLabel)}
</Form.Label>
}
value={formikProps.values.description}
placeholder={intl.formatMessage(messages.createCollectionModalDescriptionPlaceholder)}
help={intl.formatMessage(messages.createCollectionModalDescriptionDetails)}
className=""
controlClasses="pb-2"
rows="5"
/>
</Form>
</ModalDialog.Body>
<ModalDialog.Footer>
<ActionRow>
<ModalDialog.CloseButton variant="tertiary">
{intl.formatMessage(messages.createCollectionModalCancel)}
</ModalDialog.CloseButton>
<Button
variant="primary"
onClick={formikProps.submitForm}
disabled={isCreatingCollection}
>
{intl.formatMessage(messages.createCollectionModalCreate)}
</Button>
</ActionRow>
</ModalDialog.Footer>
</>
)}
</Formik>
</ModalDialog>
);
};
Expand Down

0 comments on commit aa870b4

Please sign in to comment.