Skip to content

Commit

Permalink
Added onSaveSuccess callback support.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Aug 27, 2024
1 parent 6e012f6 commit cd0fa2a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
16 changes: 5 additions & 11 deletions src/js/settings/components/classifai-onboarding/enable-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@ import {
BaseControl,
Button,
} from '@wordpress/components';
import { useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

import { FeatureContext } from '../feature-settings/context';
import { EnableToggleControl } from '../feature-settings/enable-feature';
import { SaveSettingsButton } from '../../components/feature-settings/save-settings-button';
import { useFeatureSettings } from '../../data/hooks';
import { useSetupPage } from './hooks';
import { useNavigate } from 'react-router-dom';

export const EnableFeatures = () => {
const { features, services } = window.classifAISettings;
const { isSaving } = useFeatureSettings();
const { step, nextStepPath } = useSetupPage();
const { nextStepPath } = useSetupPage();
const navigate = useNavigate();

useEffect( () => {
if ( 'enable_features' === step && false === isSaving ) {
navigate( nextStepPath );
}
}, [ isSaving, nextStepPath, step ] );
const onSaveSuccess = () => {
navigate( nextStepPath );
};

const featureToggles = Object.keys( services ).map(
( service, serviceIndex ) => (
Expand Down Expand Up @@ -121,7 +115,7 @@ export const EnableFeatures = () => {
</Button>
<SaveSettingsButton
label={ __( 'Start Setup', 'classifai' ) }
disableErrorReporting={ true }
onSaveSuccess={ onSaveSuccess }
/>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/js/settings/components/classifai-onboarding/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useState } from '@wordpress/element';
import { Outlet } from 'react-router-dom';

// Export the steps of the onboarding process.
Expand Down
4 changes: 2 additions & 2 deletions src/js/settings/components/feature-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const PersonalizerDeprecationNotice = () => (
/**
* Feature Settings component.
*/
export const FeatureSettings = () => {
export const FeatureSettings = ( { onSaveSuccess = () => {} } ) => {
const { featureName } = useFeatureContext();
const { setCurrentFeature } = useDispatch( STORE_NAME );

Expand Down Expand Up @@ -95,7 +95,7 @@ export const FeatureSettings = () => {
</Panel>
<div className="classifai-settings-footer">
<SaveButtonSlot>
<SaveSettingsButton />
<SaveSettingsButton onSaveSuccess={ onSaveSuccess } />
</SaveButtonSlot>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useSetupPage } from '../classifai-onboarding/hooks';
* Save Settings Button component.
*/
export const SaveSettingsButton = ( {
disableErrorReporting = false,
onSaveSuccess = () => {},
label = __( 'Save Settings', 'classifai' ),
} ) => {
const { featureName } = useFeatureSettings();
Expand Down Expand Up @@ -61,19 +61,18 @@ export const SaveSettingsButton = ( {
} )
.then( ( res ) => {
if ( res.errors && res.errors.length ) {
if ( ! disableErrorReporting ) {
res.errors.forEach( ( error ) => {
createErrorNotice( error.message, {
id: `error-${ featureName }`,
} );
res.errors.forEach( ( error ) => {
createErrorNotice( error.message, {
id: `error-${ featureName }`,
} );
}
} );
setSettings( res.settings );
setIsSaving( false );
setSaveErrors( res.errors );
return;
}
setSaveErrors( [] );
onSaveSuccess();
setSettings( res.settings );
setIsSaving( false );
} )
Expand Down

0 comments on commit cd0fa2a

Please sign in to comment.