Skip to content

Commit

Permalink
refactor: addressing some some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvente committed Dec 27, 2023
1 parent 13003f1 commit 5528a12
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 69 deletions.
4 changes: 2 additions & 2 deletions plugins/communications-app/BodyForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const BodyForm = () => {
dispatch(formActions.updateForm({ body: value }));
};

const bodyValidation = body.length > 0;
const isBodyValid = body.length > 0;

return (
<Form.Group controlId="emailBody">
Expand All @@ -26,7 +26,7 @@ const BodyForm = () => {
onChange={handleChangeTextEditor}
value={body}
/>
{isFormSubmitted && !bodyValidation && (
{isFormSubmitted && !isBodyValid && (
<Form.Control.Feedback className="px-3" hasIcon type="invalid">
{intl.formatMessage(messages.bodyFormFieldError)}
</Form.Control.Feedback>
Expand Down
2 changes: 1 addition & 1 deletion plugins/communications-app/BodyForm/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const messages = defineMessages({
bodyFormFieldLabel: {
id: 'body.form.field.label',
defaultMessage: 'Body',
description: 'Email Body label. Meant to have colon or equivilant punctuation.',
description: 'Email Body label. Meant to have colon or equivalent punctuation.',
},
bodyFormFieldError: {
id: 'body.form.field.error',
Expand Down
5 changes: 4 additions & 1 deletion plugins/communications-app/InputForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import PropTypes from 'prop-types';
import { Form, Container } from '@edx/paragon';

const InputForm = ({
isValid, controlId, label, feedbackText,
isValid,
controlId,
label,
feedbackText,
}) => {
const feedbackType = isValid ? 'valid' : 'invalid';
return (
Expand Down
14 changes: 7 additions & 7 deletions plugins/communications-app/RecipientsForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { actionCreators as formActions } from '@communications-app/src/component
import './styles.scss';

const disableIsHasLearners = ['track', 'cohort'];
const recipientsFormDescription = 'A selectable choice from a list of potential email recipients';

const RecipientsForm = (props) => {
const { cohorts: additionalCohorts } = props;
const RecipientsForm = ({ cohorts: additionalCohorts }) => {
const formData = useSelector((state) => state.form);
const dispatch = useDispatch();
const { isEditMode, emailRecipients, isFormSubmitted } = formData;
Expand Down Expand Up @@ -65,7 +65,7 @@ const RecipientsForm = (props) => {
<FormattedMessage
id="bulk.email.form.recipients.myself"
defaultMessage="Myself"
description="A selectable choice from a list of potential email recipients"
description={recipientsFormDescription}
/>
</Form.Checkbox>
<Form.Checkbox
Expand All @@ -76,7 +76,7 @@ const RecipientsForm = (props) => {
<FormattedMessage
id="bulk.email.form.recipients.staff"
defaultMessage="Staff/Administrators"
description="A selectable choice from a list of potential email recipients"
description={recipientsFormDescription}
/>
</Form.Checkbox>
<Form.Checkbox
Expand All @@ -88,7 +88,7 @@ const RecipientsForm = (props) => {
<FormattedMessage
id="bulk.email.form.recipients.verified"
defaultMessage="Learners in the verified certificate track"
description="A selectable choice from a list of potential email recipients"
description={recipientsFormDescription}
/>
</Form.Checkbox>
{
Expand Down Expand Up @@ -118,7 +118,7 @@ const RecipientsForm = (props) => {
<FormattedMessage
id="bulk.email.form.recipients.audit"
defaultMessage="Learners in the audit track"
description="A selectable choice from a list of potential email recipients"
description={recipientsFormDescription}
/>
</Form.Checkbox>
<Form.Checkbox
Expand All @@ -129,7 +129,7 @@ const RecipientsForm = (props) => {
<FormattedMessage
id="bulk.email.form.recipients.learners"
defaultMessage="All Learners"
description="A selectable choice from a list of potential email recipients"
description={recipientsFormDescription}
/>
</Form.Checkbox>
</Form.CheckboxSet>
Expand Down
12 changes: 6 additions & 6 deletions plugins/communications-app/RecipientsForm/styles.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.recipient-groups {
> div {
padding-right: 0.5rem;
input {
padding: 0.5rem !important;
}
> div {
padding-right: 0.5rem;
input {
padding: 0.5rem !important;
}
}
}
}
70 changes: 39 additions & 31 deletions plugins/communications-app/ScheduleSection/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useMemo } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {
Expand Down Expand Up @@ -73,6 +73,40 @@ const ScheduleSection = ({ openTaskAlert }) => {
dispatch(formActions.updateForm({ formStatus: 'default' }));
};

const handleClickStatefulButton = (event) => {
event.preventDefault();
if (formStatus === 'schedule' && !isScheduledSubmitted) {
dispatch(formActions.updateForm({ isScheduleButtonClicked: true }));
}
openTaskAlert();
};

const statefulButtonIcons = useMemo(() => ({
default: <Icon src={Send} />,
schedule: <Icon src={Event} />,
reschedule: <Icon src={Event} />,
pending: <Icon src={SpinnerSimple} className="icon-spin" />,
complete: <Icon src={Check} />,
completeSchedule: <Icon src={Check} />,
error: <Icon src={Cancel} />,
}), []);

const statefulButtonLabels = useMemo(() => ({
default: intl.formatMessage(messages.ScheduleSectionSubmitButtonDefault),
schedule: intl.formatMessage(messages.ScheduleSectionSubmitButtonSchedule),
reschedule: intl.formatMessage(messages.ScheduleSectionSubmitButtonReschedule),
pending: intl.formatMessage(messages.ScheduleSectionSubmitButtonPending),
complete: intl.formatMessage(messages.ScheduleSectionSubmitButtonComplete),
completeSchedule: intl.formatMessage(messages.ScheduleSectionSubmitButtonCompleteSchedule),
error: intl.formatMessage(messages.ScheduleSectionSubmitButtonError),
}), [intl]);

const statefulButtonDisableStates = useMemo(() => [
'pending',
'complete',
'completeSchedule',
], []);

return (
<Form.Group>
{getConfig().SCHEDULE_EMAIL_SECTION && (
Expand Down Expand Up @@ -117,37 +151,11 @@ const ScheduleSection = ({ openTaskAlert }) => {
className="send-email-btn"
data-testid="send-email-btn"
variant="primary"
onClick={(event) => {
event.preventDefault();
if (formStatus === 'schedule' && !isScheduledSubmitted) {
dispatch(formActions.updateForm({ isScheduleButtonClicked: true }));
}
openTaskAlert();
}}
onClick={handleClickStatefulButton}
state={formStatus}
icons={{
default: <Icon src={Send} />,
schedule: <Icon src={Event} />,
reschedule: <Icon src={Event} />,
pending: <Icon src={SpinnerSimple} className="icon-spin" />,
complete: <Icon src={Check} />,
completeSchedule: <Icon src={Check} />,
error: <Icon src={Cancel} />,
}}
labels={{
default: intl.formatMessage(messages.ScheduleSectionSubmitButtonDefault),
schedule: intl.formatMessage(messages.ScheduleSectionSubmitButtonSchedule),
reschedule: intl.formatMessage(messages.ScheduleSectionSubmitButtonReschedule),
pending: intl.formatMessage(messages.ScheduleSectionSubmitButtonPending),
complete: intl.formatMessage(messages.ScheduleSectionSubmitButtonComplete),
completeSchedule: intl.formatMessage(messages.ScheduleSectionSubmitButtonCompleteSchedule),
error: intl.formatMessage(messages.ScheduleSectionSubmitButtonError),
}}
disabledStates={[
'pending',
'complete',
'completeSchedule',
]}
icons={statefulButtonIcons}
labels={statefulButtonLabels}
disabledStates={statefulButtonDisableStates}
/>

<Toast
Expand Down
4 changes: 2 additions & 2 deletions plugins/communications-app/SubjectForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SubjectForm = () => {
dispatch(formActions.updateForm({ subject: value }));
};

const subjectValidation = subject.length > 0;
const isSubjectValid = subject.length > 0;

return (
<Form.Group controlId="emailSubject" className="my-5">
Expand All @@ -27,7 +27,7 @@ const SubjectForm = () => {
onChange={handleChangeEmailSubject}
value={subject}
/>
{ isFormSubmitted && !subjectValidation && (
{ isFormSubmitted && !isSubjectValid && (
<Form.Control.Feedback className="px-3" hasIcon type="invalid">
{intl.formatMessage(messages.bulkEmailFormSubjectError)}
</Form.Control.Feedback>
Expand Down
37 changes: 20 additions & 17 deletions plugins/communications-app/TaskAlertModalForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const TaskAlertModalForm = ({
} = formData;

const changeFormStatus = (status) => dispatchForm(formActions.updateForm({ formStatus: status }));
const handleResetFormValues = () => dispatchForm(formActions.resetForm());

const handlePostEmailTask = async () => {
const emailData = new FormData();
Expand All @@ -51,7 +52,7 @@ const TaskAlertModalForm = ({
await postBulkEmailInstructorTask(emailData, courseId);
const newFormStatus = isScheduled ? 'completeSchedule' : 'complete';
changeFormStatus(newFormStatus);
setTimeout(() => changeFormStatus('default'), 3000);
setTimeout(() => handleResetFormValues(), 3000);
} catch {
changeFormStatus('error');
}
Expand All @@ -77,7 +78,7 @@ const TaskAlertModalForm = ({
try {
await patchScheduledBulkEmailInstructorTask(emailData, courseId, schedulingId);
changeFormStatus('completeSchedule');
setTimeout(() => changeFormStatus('default'), 3000);
setTimeout(() => handleResetFormValues(), 3000);
} catch {
changeFormStatus('error');
}
Expand All @@ -101,6 +102,22 @@ const TaskAlertModalForm = ({
}
};

const handleCloseTaskAlert = (event) => {
closeTaskAlert();

if (event.target.name === 'continue') {
if (!isFormSubmitted) {
dispatchForm(formActions.updateForm({ isFormSubmitted: true }));
}

if (isScheduleButtonClicked) {
dispatchForm(formActions.updateForm({ isScheduledSubmitted: true }));
}

createEmailTask();
}
};

return (
<TaskAlertModal
isOpen={isTaskAlertOpen}
Expand All @@ -119,21 +136,7 @@ const TaskAlertModalForm = ({
isScheduled={isScheduled}
/>
)}
close={(event) => {
closeTaskAlert();

if (event.target.name === 'continue') {
if (!isFormSubmitted) {
dispatchForm(formActions.updateForm({ isFormSubmitted: true }));
}

if (isScheduleButtonClicked) {
dispatchForm(formActions.updateForm({ isScheduledSubmitted: true }));
}

createEmailTask();
}
}}
close={handleCloseTaskAlert}
/>
);
};
Expand Down
3 changes: 1 addition & 2 deletions src/components/bulk-email-tool/BulkEmailTool.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ export default function BulkEmailTool() {
<PluggableComponent
id="build-email-task-manager"
as="communications-app-build-email-task-manager"
courseId={courseId}
>
<BulkEmailTaskManager />
<BulkEmailTaskManager courseId={courseId} />
</PluggableComponent>
</div>
</Container>
Expand Down

0 comments on commit 5528a12

Please sign in to comment.