[Woo beta tester]: add User Preference panel (#43125)

* introduce UserPreference panel

* render user preference panel

* expose UserPreferences type

* introduce force mode to update user pref

* rename global object with `__wcbt`

* changelog

* changelog
This commit is contained in:
Damián Suárez 2024-01-03 17:42:18 -03:00 committed by GitHub
parent 12e65af8ca
commit b6f8ab94f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: tweak
[Data]: expose UserPreference TS prop

View File

@ -109,6 +109,7 @@ export {
export { TaxClass } from './tax-classes/types';
export { ProductTag, Query } from './product-tags/types';
export { WCUser } from './user/types';
export { UserPreferences } from './user/types';
/**
* Internal dependencies

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
[Woo Beta Tester]: add User Preference dev tool panel

View File

@ -83,6 +83,7 @@
font-family: var(--woocommerce-product-editor-dev-tools-code-font-family);
}
.woocommerce-product-editor-dev-tools-user-preferences,
.woocommerce-product-editor-dev-tools-product {
display: flex;
flex-direction: row;
@ -90,6 +91,7 @@
height: 100%;
}
.woocommerce-product-editor-dev-tools-user-preferences-entity,
.woocommerce-product-editor-dev-tools-product-entity {
flex: 1;

View File

@ -24,6 +24,7 @@ import { useEntityProp } from '@wordpress/core-data';
import { BlockInspectorTabPanel } from './block-inspector-tab-panel';
import { HelpTabPanel } from './help-tab-panel';
import { ProductTabPanel } from './product-tab-panel';
import { UserPreferencesTabPanel } from './user-preferences-panel';
function TabButton( {
name,
@ -111,6 +112,15 @@ export function ProductEditorDevToolsBar( {
>
{ __( 'Product', 'woocommerce' ) }
</TabButton>
<TabButton
name="user-preferences"
selectedTab={ selectedTab }
onClick={ handleTabClick }
>
{ __( 'User Preferences', 'woocommerce' ) }
</TabButton>
<TabButton
name="help"
selectedTab={ selectedTab }
@ -137,6 +147,9 @@ export function ProductEditorDevToolsBar( {
evaluationContext={ evaluationContext }
isSelected={ selectedTab === 'product' }
/>
<UserPreferencesTabPanel
isSelected={ selectedTab === 'user-preferences' }
/>
<HelpTabPanel isSelected={ selectedTab === 'help' } />
</div>
</div>

View File

@ -0,0 +1,75 @@
/**
* External dependencies
*/
import { useUserPreferences, UserPreferences } from '@woocommerce/data';
import { useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { TabPanel } from './tab-panel';
declare global {
interface Window {
__wcbt: object;
}
}
type UserPreferenceProp = keyof UserPreferences;
export function UserPreferencesTabPanel( {
isSelected,
}: {
isSelected: boolean;
} ) {
const { updateUserPreferences, ...userPreferences } = useUserPreferences();
const update = useCallback(
(
preferences: UserPreferences | UserPreferenceProp,
value?,
force = false
) => {
const dataToUpdate =
typeof preferences === 'string'
? { [ preferences ]: value }
: preferences;
/*
* When force is not True,
* only update the preferences already defined
* @todo: consider to implement straight in the data layer.
*/
if ( ! force ) {
Object.keys( dataToUpdate ).forEach( ( key: string ) => {
const userPreferenceKey = key as UserPreferenceProp;
if (
! userPreferences.hasOwnProperty( userPreferenceKey )
) {
delete dataToUpdate[ userPreferenceKey ];
}
} );
}
updateUserPreferences( dataToUpdate );
},
[ updateUserPreferences, userPreferences ]
);
// Expose updateUserPreferences globaly for debugging purposes.
window.__wcbt = {
...window.__wcbt,
updateUserPreferences: update,
userPreferences,
};
return (
<TabPanel isSelected={ isSelected }>
<div className="woocommerce-product-editor-dev-tools-user-preferences">
<div className="woocommerce-product-editor-dev-tools-user-preferences-entity">
{ JSON.stringify( userPreferences, null, 4 ) }
</div>
</div>
</TabPanel>
);
}