@@ -80,6 +80,7 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
8080 } = useDispatch ( 'optimole' ) ;
8181
8282 const [ modal , setModal ] = useState ( null ) ;
83+ const [ initialRadioValue , setInitialRadioValue ] = useState ( null ) ;
8384
8485 const isOffloadingInProgress = 'disabled' !== settings [ 'offloading_status' ] ;
8586 const isRollbackInProgress = 'disabled' !== settings [ 'rollback_status' ] ;
@@ -108,6 +109,7 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
108109 const nextSettings = { ...settings } ;
109110 nextSettings [ 'show_offload_finish_notice' ] = '' ;
110111 nextSettings [ 'offloading_status' ] = 'enabled' ;
112+ nextSettings [ 'offload_media' ] = 'enabled' ;
111113 setSettings ( nextSettings ) ;
112114 saveSettings ( nextSettings , false , true , ( ) => {
113115 setErrorMedia ( false ) ;
@@ -131,6 +133,7 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
131133 const nextSettings = { ...settings } ;
132134 nextSettings [ 'show_offload_finish_notice' ] = '' ;
133135 nextSettings [ 'rollback_status' ] = 'enabled' ;
136+ nextSettings [ 'offload_media' ] = 'disabled' ;
134137 saveSettings ( nextSettings , false , true , ( ) => {
135138 setErrorMedia ( false ) ;
136139 setProcessedImages ( 0 ) ;
@@ -161,26 +164,33 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
161164 const radioBoxValue = 'enabled' === settings [ 'offload_media' ] ? 'offload' : 'rollback' ;
162165
163166 const updateRadioBoxValue = value => {
167+ setInitialRadioValue ( radioBoxValue ) ;
168+
164169 const offloadEnabled = 'offload' === value ;
165- const nextSettings = { ...settings } ;
166- nextSettings [ 'offload_media' ] = offloadEnabled ? 'enabled' : 'disabled' ;
167- setSettings ( nextSettings ) ;
168- setCanSave ( true ) ;
169170
170171 if ( offloadEnabled ) {
171- setModal ( show_exceed_plan_quota_notice ? MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE : MODAL_STATE_OFFLOAD ) ;
172+ setModal ( {
173+ type : show_exceed_plan_quota_notice ? MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE : MODAL_STATE_OFFLOAD ,
174+ optionValue : value
175+ } ) ;
172176
173177 return ;
174178 }
175179
176- setModal ( MODAL_STATE_ROLLBACK ) ;
180+ setModal ( { type : MODAL_STATE_ROLLBACK , optionValue : value } ) ;
177181 } ;
178182
179- const getModalProps = ( type ) => {
183+ const getModalProps = ( { type, optionValue } ) => {
184+ const nextSettings = { ...settings } ;
185+
180186 const props = {
181187 [ MODAL_STATE_OFFLOAD ] : {
182188 icon : offload ,
183189 onConfirm : ( ) => {
190+ nextSettings [ 'offload_media' ] = 'offload' === optionValue ? 'enabled' : 'disabled' ;
191+ setSettings ( nextSettings ) ;
192+ setCanSave ( true ) ;
193+
184194 onOffloadMedia ( ) ;
185195 setModal ( null ) ;
186196 } ,
@@ -193,6 +203,10 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
193203 [ MODAL_STATE_ROLLBACK ] : {
194204 icon : rollbackIcon ,
195205 onConfirm : ( ) => {
206+ nextSettings [ 'offload_media' ] = 'offload' === optionValue ? 'enabled' : 'disabled' ;
207+ setSettings ( nextSettings ) ;
208+ setCanSave ( true ) ;
209+
196210 onRollbackdMedia ( ) ;
197211 setModal ( null ) ;
198212 } ,
@@ -210,7 +224,9 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
210224 setModal ( null ) ;
211225 const options = settings ;
212226 options . offloading_status = 'disabled' ;
227+ options . offload_media = 'offload' === initialRadioValue ? 'enabled' : 'disabled' ;
213228 saveSettings ( options ) ;
229+ setInitialRadioValue ( null ) ;
214230 } ,
215231 labels : {
216232 title : options_strings . offloading_stop_title ,
@@ -225,7 +241,9 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
225241 setModal ( null ) ;
226242 const options = settings ;
227243 options . rollback_status = 'disabled' ;
244+ options . offload_media = 'offload' === initialRadioValue ? 'enabled' : 'disabled' ;
228245 saveSettings ( options ) ;
246+ setInitialRadioValue ( null ) ;
229247 } ,
230248 labels : {
231249 title : options_strings . rollback_stop_title ,
@@ -237,6 +255,10 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
237255 variant : 'warning' ,
238256 icon : warningAlt ,
239257 onConfirm : ( ) => {
258+ nextSettings [ 'offload_media' ] = 'offload' === optionValue ? 'enabled' : 'disabled' ;
259+ setSettings ( nextSettings ) ;
260+ setCanSave ( true ) ;
261+
240262 onOffloadMedia ( ) ;
241263 setModal ( null ) ;
242264 } ,
@@ -268,23 +290,26 @@ const OffloadMedia = ({ settings, canSave, setSettings, setCanSave }) => {
268290
269291 const onCancelProgress = ( ) => {
270292 if ( loadingSync ) {
271- setModal ( MODAL_STATE_STOP_OFFLOAD ) ;
293+ setModal ( { type : MODAL_STATE_STOP_OFFLOAD , optionValue : initialRadioValue } ) ;
272294 }
273295
274296 if ( loadingRollback ) {
275- setModal ( MODAL_STATE_STOP_ROLLBACK ) ;
297+ setModal ( { type : MODAL_STATE_STOP_ROLLBACK , optionValue : initialRadioValue } ) ;
276298 }
277299 } ;
278300
279301 const handleForceSync = ( e ) => {
280302 e . preventDefault ( ) ;
281303
282304 if ( 'offload' === radioBoxValue ) {
283- setModal ( show_exceed_plan_quota_notice ? MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE : MODAL_STATE_OFFLOAD ) ;
305+ setModal ( {
306+ type : show_exceed_plan_quota_notice ? MODAL_STATE_EXCEED_PLAN_QUOTA_NOTICE : MODAL_STATE_OFFLOAD ,
307+ optionValue : radioBoxValue
308+ } ) ;
284309 }
285310
286311 if ( 'rollback' === radioBoxValue ) {
287- setModal ( MODAL_STATE_ROLLBACK ) ;
312+ setModal ( { type : MODAL_STATE_ROLLBACK , optionValue : radioBoxValue } ) ;
288313 }
289314 } ;
290315
0 commit comments