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

feat: shift course form error handling to @edx/paragon styles #805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zawan-ila
Copy link
Contributor

@zawan-ila zawan-ila commented Dec 15, 2022

PROD-2734

Description

This PR updates the Publisher create course form to incorporate edx/paragon based validation for form fields

Testing Instructions

  1. On the publisher landing page, click on New Course.
  2. Fill in the details for this new course whilst intentionally missing some required fields.
  3. Submit the form and observe the error messages.

ScreenShots

Before

Screenshot 2022-12-16 at 12 52 10 AM

After

Screenshot 2022-12-16 at 12 50 45 AM

ToDo

  • Update test snapshots

@zawan-ila zawan-ila changed the title feat: shift course form error handling and submission from html-based to js-based(in particular following edx/paragon styles) feat: shift course form error handling to @edx/paragon styles Dec 16, 2022
@zawan-ila zawan-ila force-pushed the anawaz/prod2733/part2 branch 3 times, most recently from 7513e46 to fe90c5c Compare December 19, 2022 08:49
@@ -74,6 +74,10 @@ class DateTimeField extends React.Component {
type,
pattern,
placeholder,
meta: {
touched,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is touched needed here? Why cant the decision be done with error variable alone?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to prevent showing errors unnecessarily. Consider a form has just rendered with a required Datefield. Since the field is empty, the error prop will be true. However we do not wish to show it as an error, as the user has yet to interact with the field. To display errors properly, we need to keep track of whether the user has interacted with the field or not. For this we need the touched prop which will become active only if the user has touched the field e.g by clicking on it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Is the prop coming from the react parent component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ummm yeah... we are rendering it inside redux-form so we don't pass the meta prop explicitly but redux-form automagically does that for us

if (pattern.test(value)) {
return undefined;
}
return 'Please Enter a valid course key';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: enter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

if (price < 1 || price > 1000) {
return 'Price should be between 1 and 1000(endpoints inclusive)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before (

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

const pattern = /^[0-9]+(\.)?[0-9]{0,2}$/;

if (!pattern.test(value)) {
return 'Please enter a valid price(with at most 2 decimal digits)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before (

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

@DawoudSheraz DawoudSheraz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will let @AliAdnanSohail have a primary say on this since he had looked into this.

@@ -101,17 +106,28 @@ class BaseCreateCourseForm extends React.Component {
<div className="create-course-form">
<h2>Create New Course</h2>
<hr />
<form onSubmit={handleSubmit}>
<form onSubmit={handleSubmit} noValidate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is validation being removed when the form is submitted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to this? Validation to the form is a must-have.

Copy link
Contributor Author

@zawan-ila zawan-ila Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well this is essentially the point of the PR....this removes the HTML(browser based) validation....if we don't do this then the browser will keep on showing its own tooltips on error fields....

we are doing the validations ourselves now...see the 'validate' prop passed to the Field components

@@ -35,6 +35,13 @@ class CreateCoursePage extends React.Component {
this.setStartedFetching();
}

componentDidUpdate(prevProps) {
// check if errors on form submission and scroll to them
if (this.props.courseInfo.error && (!prevProps.courseInfo || !prevProps.courseInfo.error)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checking that previous props did not have course info and error but current props have errors, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Comment on lines +131 to +146
const handleCourseCreateFail = (errors) => {
const fieldName = getFieldName(errors);
if (fieldName) {
const node = document.getElementsByName(fieldName)[0];

if (node) { node.scrollIntoView({ behavior: 'smooth' }); }
}
};

Copy link
Contributor

@DawoudSheraz DawoudSheraz Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this live in CourseCreateForm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleCourseEditFail also lives in the same file so I though it would be better to follow the precedent

@@ -101,17 +106,28 @@ class BaseCreateCourseForm extends React.Component {
<div className="create-course-form">
<h2>Create New Course</h2>
<hr />
<form onSubmit={handleSubmit}>
<form onSubmit={handleSubmit} noValidate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to this? Validation to the form is a must-have.

src/components/CreateCoursePage/CreateCourseForm.jsx Outdated Show resolved Hide resolved
src/components/CreateCoursePage/CreateCourseForm.jsx Outdated Show resolved Hide resolved
src/components/CreateCoursePage/CreateCourseForm.jsx Outdated Show resolved Hide resolved
src/components/CreateCoursePage/CreateCourseForm.jsx Outdated Show resolved Hide resolved
@@ -188,6 +216,11 @@ class BaseCreateCourseForm extends React.Component {
<Field
name="courseRunKey"
component={RenderInputTextField}
props={
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to make inline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -164,6 +171,14 @@ class CreateCoursePage extends React.Component {
{ showForm
&& (
<div>
{errorArray.length > 1 && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would errorArray contain if length is 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know...essentially this seems to be some sort of condition for displaying an error message....i have only changed the position of where the error message is being displayed....

if this ^ doesn't make sense, just look at the complete file and hopefully it will be clear

Copy link
Contributor

@AliAdnanSohail AliAdnanSohail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just few nits to address

if (pattern.test(value)) {
return undefined;
}
return 'Enter a valid course key';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: To make it consistent with other error messages, update this to Please enter a valid course key

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -17,6 +21,11 @@ const PriceList = ({
key={seatType}
name={`prices.${seatType}`}
component={RenderInputTextField}
props={
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
props={
props={{name: `prices.${seatType}`}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@zawan-ila zawan-ila force-pushed the anawaz/prod2733/part2 branch 2 times, most recently from 6d1d2a0 to ae6a40b Compare January 13, 2023 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants