1- import { useState , useEffect , useContext } from 'react' ;
1+ import { useContext } from 'react' ;
22import classNames from 'classnames' ;
33import PropTypes from 'prop-types' ;
4+ import useDeepCompareEffect from 'use-deep-compare-effect' ;
45import {
56 Form ,
67 Spinner ,
78 useToggle ,
89} from '@edx/paragon' ;
9- import { BulkEmailContext } from '../bulk-email-context' ;
10- import useMobileResponsive from '../../../utils/useMobileResponsive' ;
11- import PluggableComponent from '../../PluggableComponent' ;
10+ import { BulkEmailContext } from '../../ bulk-email-context' ;
11+ import useMobileResponsive from '../../../../ utils/useMobileResponsive' ;
12+ import PluggableComponent from '../../../ PluggableComponent' ;
1213
13- function BuildEmailFormExtensible ( { courseId, cohorts } ) {
14+ import { withContextProvider , useDispatch } from './context' ;
15+ import { actionCreators as formActions } from './context/reducer' ;
16+
17+ const BuildEmailFormExtensible = ( { courseId, cohorts } ) => {
1418 const isMobile = useMobileResponsive ( ) ;
1519 const [ { editor } ] = useContext ( BulkEmailContext ) ;
1620 const [ isTaskAlertOpen , openTaskAlert , closeTaskAlert ] = useToggle ( false ) ;
17- const [ formState , setFormState ] = useState ( {
18- isFormValid : true ,
19- isFormSubmitted : false ,
20- scheduleValid : true ,
21- isScheduled : false ,
22- isEditMode : false ,
23- formStatus : 'default' ,
24- isScheduleButtonClicked : false ,
25- courseId,
26- cohorts,
27- scheduleDate : '' ,
28- scheduleTime : '' ,
29- isScheduledSubmitted : false ,
30- emailId : '' ,
31- schedulingId : '' ,
32- emailRecipients : { value : [ ] , isLoaded : false } ,
33- subject : { value : '' , isLoaded : false } ,
34- body : { value : '' , isLoaded : false } ,
35- } ) ;
21+ const dispatch = useDispatch ( ) ;
3622
37- useEffect ( ( ) => {
23+ useDeepCompareEffect ( ( ) => {
3824 if ( editor . editMode ) {
39- const { emailRecipients, subject, body } = formState ;
40- const newRecipientsValue = { ...emailRecipients , value : editor . emailRecipients } ;
41- const newSubjectValue = { ...subject , value : editor . emailSubject } ;
42- const newBodyValue = { ...body , value : editor . emailBody } ;
25+ const newRecipientsValue = editor . emailRecipients ;
26+ const newSubjectValue = editor . emailSubject ;
27+ const newBodyValue = editor . emailBody ;
4328 const newScheduleDate = editor . scheduleDate ;
4429 const newScheduleTime = editor . scheduleTime ;
4530 const newEmailId = editor . emailId ;
4631 const newSchedulingId = editor . schedulingId ;
4732
48- setFormState ( {
49- ...formState ,
33+ dispatch ( formActions . updateForm ( {
5034 isEditMode : true ,
5135 formStatus : 'reschedule' ,
5236 isScheduled : true ,
@@ -57,10 +41,9 @@ function BuildEmailFormExtensible({ courseId, cohorts }) {
5741 emailRecipients : newRecipientsValue ,
5842 subject : newSubjectValue ,
5943 body : newBodyValue ,
60- } ) ;
44+ } ) ) ;
6145 }
62- // eslint-disable-next-line react-hooks/exhaustive-deps
63- } , [ editor . editMode , editor . emailRecipients , editor . emailSubject , editor . emailBody ] ) ;
46+ } , [ editor , dispatch ] ) ;
6447
6548 return (
6649 < div className = { classNames ( 'w-100 m-auto' , ! isMobile && 'p-4 border border-primary-200' ) } >
@@ -69,23 +52,19 @@ function BuildEmailFormExtensible({ courseId, cohorts }) {
6952 < PluggableComponent
7053 id = "build-email-form-tasks-alert-modal"
7154 as = "communications-app-task-alert-modal"
72- formState = { formState }
73- setFormState = { setFormState }
55+ courseId = { courseId }
7456 { ...{ isTaskAlertOpen, openTaskAlert, closeTaskAlert } }
7557 />
7658
7759 < PluggableComponent
7860 id = "build-email-form-recipients-field"
7961 as = "communications-app-recipients-checks"
80- formState = { formState }
81- setFormState = { setFormState }
62+ cohorts = { cohorts }
8263 />
8364
8465 < PluggableComponent
8566 id = "build-email-form-subject-field"
8667 as = "communications-app-subject-form"
87- formState = { formState }
88- setFormState = { setFormState }
8968 />
9069
9170 < PluggableComponent
@@ -99,29 +78,23 @@ function BuildEmailFormExtensible({ courseId, cohorts }) {
9978 screenReaderText = "loading"
10079 />
10180 ) }
102- formState = { formState }
103- setFormState = { setFormState }
10481 />
10582
10683 < PluggableComponent
10784 id = "build-email-form-instructions-form"
10885 as = "communications-app-instructions-pro-freading"
109- formState = { formState }
110- setFormState = { setFormState }
11186 />
11287
11388 < PluggableComponent
11489 id = "build-email-form-schedule-section"
11590 as = "communications-app-schedule-section"
116- formState = { formState }
117- setFormState = { setFormState }
11891 openTaskAlert = { openTaskAlert }
11992 />
12093
12194 </ Form >
12295 </ div >
12396 ) ;
124- }
97+ } ;
12598
12699BuildEmailFormExtensible . defaultProps = {
127100 cohorts : [ ] ,
@@ -132,4 +105,4 @@ BuildEmailFormExtensible.propTypes = {
132105 cohorts : PropTypes . arrayOf ( PropTypes . string ) ,
133106} ;
134107
135- export default BuildEmailFormExtensible ;
108+ export default withContextProvider ( BuildEmailFormExtensible ) ;
0 commit comments