Skip to content

Commit

Permalink
code update for error state and retry only failed apis
Browse files Browse the repository at this point in the history
  • Loading branch information
girish-lokapure committed Feb 21, 2024
1 parent 1362793 commit 209d3d1
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/OnboardingSPA/steps/SiteGen/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const SiteGenPreview = () => {
const [ homepages, setHomepages ] = useState( false );
const [ isRegenerating, setIsRegenerating ] = useState( false );
const [ isPreviewLoading, setIsPreviewLoading ] = useState( false );
const [ isMetaApiSuccess, setIsMetaApiSuccess ] = useState( false );
const [ globalStyles, setGlobalStyles ] = useState( false );
const [ failedApi, setFailedApi ] = useState( [] );

const prevSiteGenErrorStatus = useRef();

Expand All @@ -52,18 +54,6 @@ const SiteGenPreview = () => {
}
);

// Define fetchData outside of useEffect
async function fetchPreviewData() {
await generateSiteGenData();
if ( ! siteGenErrorStatus ) {
console.log( 'Load homepages' );
loadHomepages();
loadGlobalStyles();
} else {
console.log( 'Do not load home pages' );
}
}

useEffect( () => {
setIsHeaderEnabled( true );
setHideFooterNav( true );
Expand All @@ -73,13 +63,28 @@ const SiteGenPreview = () => {
updateInitialize( true );
}, [ currentData ] );

useEffect( () => {
generateSiteGenData();
}, [] );

useEffect( () => {
if ( isMetaApiSuccess ) {
loadHomepages();
loadGlobalStyles();
}
}, [ isMetaApiSuccess ] );

useEffect( () => {
if (
prevSiteGenErrorStatus.current === true &&
siteGenErrorStatus === false
) {
loadHomepages();
loadGlobalStyles();
if ( ! isMetaApiSuccess ) {
generateSiteGenData();
} else {
loadHomepages();
loadGlobalStyles();
}
}
prevSiteGenErrorStatus.current = siteGenErrorStatus;
}, [ siteGenErrorStatus ] );
Expand All @@ -103,6 +108,7 @@ const SiteGenPreview = () => {
currentData.sitegen.siteGenMetaStatus.totalCount
) {
currentData.sitegen.skipCache = false;
setIsMetaApiSuccess( true );
}
setCurrentOnboardingData( currentData );
}
Expand All @@ -115,6 +121,13 @@ const SiteGenPreview = () => {
retryCount + 1
);
} else {
setFailedApi( ( prevState ) => {
if ( ! prevState.includes( identifier ) ) {
return [ ...prevState, identifier ];
}
return prevState;
} );
currentData.sitegen.siteGenErrorStatus = true;
updateSiteGenErrorStatus( true );
setIsPreviewLoading( false );
}
Expand All @@ -124,12 +137,20 @@ const SiteGenPreview = () => {
async function generateSiteGenData() {
setIsPreviewLoading( true );
// Start the API Requests when the loader is shown.
let identifiers = await getSiteGenIdentifiers();
identifiers = identifiers.body;

const midIndex = Math.floor( identifiers.length / 2 );
identifiers = identifiers.slice( midIndex, identifiers.length );
currentData.sitegen.siteGenMetaStatus.currentStatus = midIndex;
let identifiers;
if ( Array.isArray( failedApi ) && failedApi.length > 0 ) {
identifiers = failedApi;
setFailedApi( [] );
} else {
identifiers = await getSiteGenIdentifiers();
identifiers = identifiers.body;

const midIndex = Math.floor( identifiers.length / 2 );
identifiers = identifiers.slice( midIndex, identifiers.length );
currentData.sitegen.siteGenMetaStatus.currentStatus = midIndex;
}

setCurrentOnboardingData( currentData );
const siteInfo = {
site_description: currentData.sitegen?.siteDetails?.prompt,
Expand Down Expand Up @@ -180,10 +201,6 @@ const SiteGenPreview = () => {
setGlobalStyles( globalStylesResponse.body );
};

useEffect( () => {
fetchPreviewData();
}, [] );

const handlePreview = ( slug ) => {
if ( ! ( slug in homepages ) ) {
return false;
Expand Down

0 comments on commit 209d3d1

Please sign in to comment.