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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Better Code
officiallygod committed Aug 29, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 4b20cc2d8f1efb692e55d84b68fe2d61dcfee34c
24 changes: 8 additions & 16 deletions src/OnboardingSPA/index.js
Original file line number Diff line number Diff line change
@@ -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().
@@ -49,22 +50,13 @@ export async function initializeNFDOnboarding( id, runtime ) {
);
}

try {
let count = 0;
do {
const currentData = await getFlow();
count++;
if ( currentData.error === null ) {
currentData.body = initializeFlowData( currentData.body );
dispatch( nfdOnboardingStore ).setCurrentOnboardingData(
currentData.body
);
break;
}
} while ( count < 3 );
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( 'Failed to retrieve flow data', error );
const currentData = await functionRetryHandler( getFlow, 3 );

if ( currentData !== false ) {
currentData.body = initializeFlowData( currentData.body );
dispatch( nfdOnboardingStore ).setCurrentOnboardingData(
currentData.body
);
}

if ( null !== DOM_TARGET && 'undefined' !== typeof render ) {
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;
}

Unchanged files with check annotations Beta

return () => {
colorSchemeMediaQuery.removeEventListener( 'change', handleChange );
};
}, [] );

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

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 ) => {