Skip to content

Commit

Permalink
Design Picker: Save the selected global styles to the onboard stores
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur791004 committed Dec 17, 2024
1 parent 5252131 commit f29763d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ const useRecipe = (
) => {
const [ searchParams, setSearchParams ] = useSearchParams();
const isPreviewingDesign = !! searchParams.get( 'theme' );
const { selectedDesign, selectedStyleVariation } = useSelect( ( select ) => {
const { getSelectedDesign, getSelectedStyleVariation } = select(
ONBOARD_STORE
) as OnboardSelect;
return {
selectedDesign: getSelectedDesign(),
selectedStyleVariation: getSelectedStyleVariation(),
};
}, [] );

const { setSelectedDesign, setSelectedStyleVariation } = useDispatch( ONBOARD_STORE );
const { selectedDesign, selectedStyleVariation, selectedGlobalStyles } = useSelect(
( select ) => {
const { getSelectedDesign, getSelectedStyleVariation, getSelectedGlobalStyles } = select(
ONBOARD_STORE
) as OnboardSelect;
return {
selectedDesign: getSelectedDesign(),
selectedStyleVariation: getSelectedStyleVariation(),
selectedGlobalStyles: getSelectedGlobalStyles(),
};
},
[]
);

const { setSelectedDesign, setSelectedStyleVariation, setSelectedGlobalStyles } =
useDispatch( ONBOARD_STORE );

const [ selectedColorVariation, setSelectedColorVariation ] =
useState< GlobalStylesObject | null >( null );
Expand All @@ -53,8 +58,6 @@ const useRecipe = (
!! selectedFontVariation,
].filter( Boolean ).length;

const [ globalStyles, setGlobalStyles ] = useState< GlobalStylesObject | null >( null );

/**
* Get the preselect data only when mounting and ignore any changes later.
*/
Expand Down Expand Up @@ -183,7 +186,7 @@ const useRecipe = (
handleSelectedStyleVariationChange();
handleSelectedColorVariationChange( null );
handleSelectedFontVariationChange( null );
setGlobalStyles( null );
setSelectedGlobalStyles( undefined );
};

// Unset the selected design, thus restarting the design picking experience.
Expand Down Expand Up @@ -258,14 +261,14 @@ const useRecipe = (
selectedColorVariation,
selectedFontVariation,
numOfSelectedGlobalStyles,
globalStyles,
globalStyles: selectedGlobalStyles,
previewDesign,
previewDesignVariation,
setSelectedDesign,
setSelectedStyleVariation,
setSelectedColorVariation: handleSelectedColorVariationChange,
setSelectedFontVariation: handleSelectedFontVariationChange,
setGlobalStyles,
setGlobalStyles: setSelectedGlobalStyles,
resetPreview,
};
};
Expand Down
8 changes: 7 additions & 1 deletion packages/data-stores/src/onboard/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { guessTimezone, getLanguage } from '@automattic/i18n-utils';
import { dispatch, select } from '@wordpress/data-controls';
import { __ } from '@wordpress/i18n';
import { STORE_KEY as SITE_STORE } from '../site';
import { Visibility } from '../site/types';
import { Visibility, GlobalStyles } from '../site/types';
import { SiteGoal, STORE_KEY } from './constants';
import { ProfilerData, ReadymadeTemplate } from './types';
import type { DomainTransferData, State } from '.';
Expand Down Expand Up @@ -171,6 +171,11 @@ export const setSelectedStyleVariation = (
selectedStyleVariation,
} );

export const setSelectedGlobalStyles = ( selectedGlobalStyles: GlobalStyles | undefined ) => ( {
type: 'SET_SELECTED_GLOBAL_STYLES' as const,
selectedGlobalStyles,
} );

export const setSelectedReadymadeTemplate = ( readymadeTemplate: ReadymadeTemplate ) => ( {
type: 'SET_READYMADE_TEMPLATE' as const,
readymadeTemplate,
Expand Down Expand Up @@ -410,6 +415,7 @@ export type OnboardAction = ReturnType<
| typeof setRandomizedDesigns
| typeof setSelectedDesign
| typeof setSelectedStyleVariation
| typeof setSelectedGlobalStyles
| typeof setSelectedSite
| typeof setSelectedReadymadeTemplate
| typeof setShowSignupDialog
Expand Down
18 changes: 18 additions & 0 deletions packages/data-stores/src/onboard/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from './types';
import type { DomainSuggestion } from '../domain-suggestions';
import type { FeatureId } from '../shared-types';
import type { GlobalStyles } from '../site';
// somewhat hacky, but resolves the circular dependency issue
import type { Design, StyleVariation } from '@automattic/design-picker/src/types';
import type { MinimalRequestCartProduct } from '@automattic/shopping-cart';
Expand Down Expand Up @@ -130,6 +131,22 @@ const selectedStyleVariation: Reducer< StyleVariation | undefined, OnboardAction
return state;
};

const selectedGlobalStyles: Reducer< GlobalStyles | undefined, OnboardAction > = (
state,
action
) => {
switch ( action.type ) {
case 'SET_SELECTED_GLOBAL_STYLES':
return action.selectedGlobalStyles;

case 'RESET_ONBOARD_STORE':
return undefined;

default:
return state;
}
};

const readymadeTemplate: Reducer< ReadymadeTemplate | undefined, OnboardAction > = (
state = undefined, // Initial state is set to undefined
action
Expand Down Expand Up @@ -629,6 +646,7 @@ const reducer = combineReducers( {
storeType,
selectedDesign,
selectedStyleVariation,
selectedGlobalStyles,
selectedSite,
siteTitle,
showSignupDialog,
Expand Down
1 change: 1 addition & 0 deletions packages/data-stores/src/onboard/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const getLastLocation = ( state: State ) => state.lastLocation;
export const getRandomizedDesigns = ( state: State ) => state.randomizedDesigns;
export const getSelectedDesign = ( state: State ) => state.selectedDesign;
export const getSelectedStyleVariation = ( state: State ) => state.selectedStyleVariation;
export const getSelectedGlobalStyles = ( state: State ) => state.selectedGlobalStyles;
export const getSelectedDomain = ( state: State ) => state.domain;
export const getSelectedFeatures = ( state: State ) => state.selectedFeatures;
export const getSelectedSite = ( state: State ) => state.selectedSite;
Expand Down

0 comments on commit f29763d

Please sign in to comment.