Skip to content

Commit

Permalink
Handle homepage failure
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Apr 8, 2024
1 parent 0066d44 commit b66c868
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 86 deletions.
22 changes: 13 additions & 9 deletions src/OnboardingSPA/components/Loaders/SiteGenLoader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ const SiteGenLoader = ( { customNavPercentage, watcher = null } ) => {
const [ percentage, setPercentage ] = useState( 0 );
const [ status, setStatus ] = useState( content.status[ statusIdx ].title );

const { currentData, nextStep } = useSelect( ( select ) => {
return {
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
nextStep: select( nfdOnboardingStore ).getNextStep(),
};
} );
const { currentData, nextStep, isGeneratingHomepages } = useSelect(
( select ) => {
return {
currentData:
select( nfdOnboardingStore ).getCurrentOnboardingData(),
nextStep: select( nfdOnboardingStore ).getNextStep(),
isGeneratingHomepages:
select( nfdOnboardingStore ).isGeneratingHomepages(),
};
}
);

useEffect( () => {
const statusTimer = setInterval( () => {
Expand All @@ -42,15 +46,15 @@ const SiteGenLoader = ( { customNavPercentage, watcher = null } ) => {
}, [ currentData?.sitegen?.siteGenMetaStatus?.currentStatus ] );

Check warning on line 46 in src/OnboardingSPA/components/Loaders/SiteGenLoader/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has a missing dependency: 'currentData?.sitegen?.siteGenMetaStatus?.totalCount'. Either include it or remove the dependency array

useEffect( () => {
if ( percentage === customNavPercentage ) {
if ( percentage === customNavPercentage && ! isGeneratingHomepages ) {
if ( nextStep ) {
if ( watcher !== null && watcher === false ) {
return;
}
navigate( nextStep.path );
}
}
}, [ percentage, watcher ] );
}, [ percentage, watcher, isGeneratingHomepages ] );

Check warning on line 57 in src/OnboardingSPA/components/Loaders/SiteGenLoader/index.js

View workflow job for this annotation

GitHub Actions / Run Lint Checks

React Hook useEffect has missing dependencies: 'customNavPercentage', 'navigate', and 'nextStep'. Either include them or remove the dependency array

return (
<div className={ 'nfd-sg-loader' }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const SiteGen = () => {
setCurrentOnboardingData,
updateSiteGenErrorStatus,
setActiveChapter,
setIsGeneratingHomepages,
} = useDispatch( nfdOnboardingStore );

const { getEditedEntityRecord } = useSelect( ( select ) => {
Expand Down Expand Up @@ -219,18 +220,23 @@ const SiteGen = () => {
) {
// Once all requests are completed use cache to get data
currentData.sitegen.skipCache = false;
setIsGeneratingSiteMeta( false );
// Increase count after site meta calls to ensure systematic call of homepages
currentData.sitegen.siteGenMetaStatus.totalCount += 1;
// Get the homepages and set that in flow
setIsGeneratingHomepages( true );
const response = await getHomepages(
currentData.sitegen.siteDetails.prompt
);

if ( response.body ) {
currentData.sitegen.homepages.data = response.body;
currentData.sitegen.siteGenMetaStatus.currentStatus += 1;
setIsGeneratingSiteMeta( false );
if ( response.error ) {
updateSiteGenErrorStatus( true );
setIsGeneratingHomepages( false );
setCurrentOnboardingData( currentData );
return;
}

currentData.sitegen.homepages.data = response.body;
setIsGeneratingHomepages( false );
}
// Sync the current request changed to State
setCurrentOnboardingData( currentData );
Expand Down
2 changes: 1 addition & 1 deletion src/OnboardingSPA/steps/SiteGen/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const SiteGenPreview = () => {
setHeaderActiveView( HEADER_SITEGEN );
setDrawerActiveView( false );
updateInitialize( true );
setIsHeaderNavigationEnabled( false );
}, [] );

useEffect( () => {
Expand All @@ -85,6 +84,7 @@ const SiteGenPreview = () => {
) {
loadHomepages();
loadGlobalStyles();
setIsHeaderNavigationEnabled( false );
}
prevSiteGenErrorStatus.current = siteGenErrorStatus;
}, [ siteGenErrorStatus ] );
Expand Down
20 changes: 3 additions & 17 deletions src/OnboardingSPA/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,24 +292,10 @@ export function resetNavError() {
};
}

export const setHomepagesData = ( homepagesData ) => {
export const setIsGeneratingHomepages = ( isGeneratingHomepages ) => {
return {
type: 'SET_HOMEPAGES_DATA',
homepagesData,
};
};

export const setActiveHomepage = ( activeHomepage ) => {
return {
type: 'SET_ACTIVE_HOMEPAGE',
activeHomepage,
};
};

export const toggleFavorite = ( slug ) => {
return {
type: 'TOGGLE_FAVORITE',
slug,
type: 'SET_IS_GENERATING_HOMEPAGES',
isGeneratingHomepages,
};
};

Expand Down
30 changes: 6 additions & 24 deletions src/OnboardingSPA/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export function drawer(

return state;
}

export function data( state = {}, action ) {
switch ( action.type ) {
case 'SET_CURRENT_DATA':
Expand All @@ -178,42 +179,23 @@ export function data( state = {}, action ) {
...action.socialData,
},
};
case 'SET_HOMEPAGES_DATA':
return {
...state,
flowData: {
...state.flowData,
sitegen: {
...state.flowData.sitegen,
homepages: action.homepagesData,
},
},
};

case 'SET_ACTIVE_HOMEPAGE':
case 'SET_SITEGEN_AI_ERROR_STATUS':
return {
...state,
flowData: {
...state.flowData,
sitegen: {
...state.flowData.sitegen,
homepages: {
...state.flowData.sitegen.homepages,
active: action.activeHomepage,
},
siteGenErrorStatus: action.siteGenErrorStatus,
},
},
};
case 'SET_SITEGEN_AI_ERROR_STATUS':

case 'SET_IS_GENERATING_HOMEPAGES':
return {
...state,
flowData: {
...state.flowData,
sitegen: {
...state.flowData.sitegen,
siteGenErrorStatus: action.siteGenErrorStatus,
},
},
isGeneratingHomepages: action.isGeneratingHomepages,
};
}

Expand Down
32 changes: 2 additions & 30 deletions src/OnboardingSPA/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,36 +431,8 @@ export function getCurrentUserDetails( state ) {
return currentUserInfo;
}

/**
* Gets homepages
*
* @param {*} state
* @return {Object} homepages
*/
export const getHomepagesData = ( state ) => {
return state.data.flowData.sitegen.homepages;
};

/**
* Gets actove homepage
*
* @param {*} state
* @return {Object} active
*/

export const getActiveHomepage = ( state ) => {
return state.data.flowData.sitegen.homepages.active;
};

/**
* Gets all homepage
*
* @param {*} state
* @return {Object} data
*/

export const getAllHomepages = ( state ) => {
return state.data.flowData.sitegen.homepages.data;
export const isGeneratingHomepages = ( state ) => {
return state.data.isGeneratingHomepages;
};

export function getCustomizeSidebarData( state ) {
Expand Down

0 comments on commit b66c868

Please sign in to comment.