Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Onboarding Flow GET Error #612

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
const ThemeContext = createContext();

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

Expand Down Expand Up @@ -51,7 +51,7 @@
return () => {
colorSchemeMediaQuery.removeEventListener( 'change', handleChange );
};
}, [] );

Check warning on line 54 in src/OnboardingSPA/components/ThemeContextProvider/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'currentData' and 'setCurrentOnboardingData'. Either include them or remove the dependency array

const toggleTheme = () => {
setTheme( ( prevTheme ) => {
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;
}
Loading