Skip to content

Commit

Permalink
Merge pull request #612 from newfold-labs/fix/onboarding-flow-error
Browse files Browse the repository at this point in the history
Fix Onboarding Flow GET Error
  • Loading branch information
arunshenoy99 authored Sep 11, 2024
2 parents c1855b8 + ff902ce commit 85242b3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/OnboardingSPA/components/ThemeContextProvider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { THEME_DARK, THEME_LIGHT } from '../../../constants';
const ThemeContext = createContext();

function usePreferredColorScheme( currentData ) {
if ( currentData.sitegenThemeMode ) {
if ( currentData?.sitegenThemeMode ) {
return currentData.sitegenThemeMode;
}

Expand Down
5 changes: 3 additions & 2 deletions src/OnboardingSPA/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { dispatch } from '@wordpress/data';
import { render } from '@wordpress/element';
import { getInitialChapters } from './data/flows';
import { stateToStore } from './chapters/utils';
import { functionRetryHandler } from './utils/api/utils';

/**
* Component passed to wp.element.render().
Expand Down Expand Up @@ -49,9 +50,9 @@ export async function initializeNFDOnboarding( id, runtime ) {
);
}

const currentData = await getFlow();
const currentData = await functionRetryHandler( getFlow, 3 );

if ( currentData.error === null ) {
if ( currentData !== false ) {
currentData.body = initializeFlowData( currentData.body );
dispatch( nfdOnboardingStore ).setCurrentOnboardingData(
currentData.body
Expand Down
6 changes: 3 additions & 3 deletions src/OnboardingSPA/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ export function getNavErrorPath( state ) {
}

export function getExperienceLevel( state ) {
return state.data.flowData.data.wpComfortLevel;
return state.data.flowData?.data.wpComfortLevel;
}

export function getTopPriority( state ) {
return state.data.flowData.data.topPriority.priority1;
return state.data.flowData?.data.topPriority.priority1;
}

/**
Expand All @@ -461,7 +461,7 @@ export function getCustomizeSidebarData( state ) {
}

export function getSiteGenErrorStatus( state ) {
return state.data.flowData.sitegen.siteGenErrorMeta.status;
return state.data.flowData?.sitegen.siteGenErrorMeta.status;
}

export function getInteractionDisabled( state ) {
Expand Down
28 changes: 28 additions & 0 deletions src/OnboardingSPA/utils/api/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Retries a given API function until the specified retry count is reached.
*
* @param {Function} func - The API function to be retried. It should return a promise.
* @param {number} retryCount - The number of times to retry the function before giving up.
* @return {Promise<any|boolean>} - Resolves with the data returned by the API function if successful, or `false` if all retries fail.
*/
export async function functionRetryHandler( func, retryCount ) {
let attempts = 0;
while ( attempts < retryCount ) {
try {
const data = await func();
attempts++;
if ( data.error === null ) {
return data;
}
} catch ( error ) {
attempts++;
// eslint-disable-next-line no-console
console.error( 'Failed to retrieve data', error );

if ( attempts >= retryCount ) {
return false;
}
}
}
return false;
}

0 comments on commit 85242b3

Please sign in to comment.