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

Design Picker: Save the selected global styles to the onboard stores #97543

Merged
merged 1 commit into from
Dec 17, 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
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
Loading