@@ -19,23 +19,27 @@ import { getConfig } from '@edx/frontend-platform';
1919import { useIntl } from '@edx/frontend-platform/i18n' ;
2020import ScheduleEmailForm from '@communications-app/src/components/bulk-email-tool/bulk-email-form/ScheduleEmailForm' ;
2121import useMobileResponsive from '@communications-app/src/utils/useMobileResponsive' ;
22+ import { useSelector , useDispatch } from '@communications-app/src/components/bulk-email-tool/bulk-email-form/BuildEmailFormExtensible/context' ;
23+ import { actionCreators as formActions } from '@communications-app/src/components/bulk-email-tool/bulk-email-form/BuildEmailFormExtensible/context/reducer' ;
2224
2325import messages from './messages' ;
2426
2527const formStatusToast = [ 'error' , 'complete' , 'completeSchedule' ] ;
2628
27- const ScheduleSection = ( { formState , setFormState , openTaskAlert } ) => {
29+ const ScheduleSection = ( { openTaskAlert } ) => {
2830 const intl = useIntl ( ) ;
2931 const isMobile = useMobileResponsive ( ) ;
3032 const [ scheduleInputChanged , isScheduleInputChanged ] = useState ( false ) ;
33+ const formData = useSelector ( ( state ) => state . form ) ;
34+ const dispatch = useDispatch ( ) ;
3135 const {
3236 isScheduled,
3337 scheduleDate = '' ,
3438 scheduleTime = '' ,
3539 isEditMode,
3640 formStatus,
3741 isScheduledSubmitted = false ,
38- } = formState ?? { } ;
42+ } = formData ;
3943
4044 const formStatusErrors = {
4145 error : intl . formatMessage ( messages . ScheduleSectionSubmitFormError ) ,
@@ -46,14 +50,19 @@ const ScheduleSection = ({ formState, setFormState, openTaskAlert }) => {
4650 const handleChangeScheduled = ( ) => {
4751 const newSchedule = ! isScheduled ;
4852 const newFormStatus = newSchedule ? 'schedule' : 'default' ;
49- setFormState ( { ...formState , formStatus : newFormStatus , isScheduled : newSchedule } ) ;
53+ // setFormState({ ...formState, formStatus: newFormStatus, isScheduled: newSchedule });
54+ dispatch ( formActions . updateForm ( { formStatus : newFormStatus , isScheduled : newSchedule } ) ) ;
5055 } ;
5156
5257 const handleScheduleDate = ( { target : { name, value } } ) => {
53- setFormState ( { ... formState , [ name ] : value } ) ;
58+ dispatch ( formActions . updateForm ( { [ name ] : value } ) ) ;
5459 if ( ! scheduleInputChanged ) {
5560 isScheduleInputChanged ( true ) ;
5661 }
62+ /* setFormState({ ...formState, [name]: value });
63+ if (!scheduleInputChanged) {
64+ isScheduleInputChanged(true);
65+ } */
5766 } ;
5867
5968 const scheduleFields = isScheduledSubmitted ? scheduleDate . length > 0 && scheduleTime . length > 0
@@ -62,24 +71,11 @@ const ScheduleSection = ({ formState, setFormState, openTaskAlert }) => {
6271 const checkIsValidSchedule = isScheduled ? scheduleFields : true ;
6372
6473 const handleResetFormValues = ( ) => {
65- const { emailRecipients, subject, body } = formState ?? { } ;
66- const newRecipientsValue = { ...emailRecipients , value : [ ] } ;
67- const newSubjectValue = { ...subject , value : '' } ;
68- const newBodyValue = { ...body , value : '' } ;
69-
70- setFormState ( {
71- ...formState ,
72- emailRecipients : newRecipientsValue ,
73- subject : newSubjectValue ,
74- body : newBodyValue ,
75- scheduleDate : '' ,
76- scheduleTime : '' ,
77- isScheduled : false ,
78- isEditMode : false ,
79- formStatus : 'default' ,
80- isScheduleButtonClicked : false ,
81- isScheduledSubmitted : false ,
82- } ) ;
74+ dispatch ( formActions . resetForm ( ) ) ;
75+ } ;
76+
77+ const handleCloseToast = ( ) => {
78+ dispatch ( formActions . updateForm ( { formStatus : 'default' } ) ) ;
8379 } ;
8480
8581 return (
@@ -90,7 +86,7 @@ const ScheduleSection = ({ formState, setFormState, openTaskAlert }) => {
9086 name = "scheduleEmailBox"
9187 checked = { isScheduled }
9288 onChange = { handleChangeScheduled }
93- disabled = { formState === 'pending' }
89+ disabled = { formStatus === 'pending' }
9490 >
9591 { intl . formatMessage ( messages . ScheduleSectionSubmitScheduleBox ) }
9692 </ Form . Checkbox >
@@ -124,11 +120,13 @@ const ScheduleSection = ({ formState, setFormState, openTaskAlert }) => {
124120
125121 < StatefulButton
126122 className = "send-email-btn"
123+ data-testid = "send-email-btn"
127124 variant = "primary"
128125 onClick = { ( event ) => {
129126 event . preventDefault ( ) ;
130127 if ( formStatus === 'schedule' && ! isScheduledSubmitted ) {
131- setFormState ( { ...formState , isScheduleButtonClicked : true } ) ;
128+ dispatch ( formActions . updateForm ( { isScheduleButtonClicked : true } ) ) ;
129+ // setFormState({ ...formState, isScheduleButtonClicked: true });
132130 }
133131 openTaskAlert ( ) ;
134132 } }
@@ -160,24 +158,20 @@ const ScheduleSection = ({ formState, setFormState, openTaskAlert }) => {
160158
161159 < Toast
162160 show = { formStatusToast . includes ( formStatus ) }
163- onClose = { ( ) => { setFormState ( { ... formState , formStatus : 'default' } ) ; } }
161+ onClose = { handleCloseToast }
164162 >
165- { formStatusErrors [ formStatus ] || null }
163+ { formStatusErrors [ formStatus ] || '' }
166164 </ Toast >
167165 </ div >
168166 </ Form . Group >
169167 ) ;
170168} ;
171169
172170ScheduleSection . defaultProps = {
173- formState : { } ,
174- setFormState : ( ) => { } ,
175171 openTaskAlert : ( ) => { } ,
176172} ;
177173
178174ScheduleSection . propTypes = {
179- formState : PropTypes . shape ( { } ) ,
180- setFormState : PropTypes . func ,
181175 openTaskAlert : PropTypes . func ,
182176} ;
183177
0 commit comments