[Product Block Editor]: Be able to dismiss AdviceCard instances (#43124)

* introduce packages/js/data/src/user/types.ts pref

* be able to dismiss the Upsells advice

* rename user pref key

* typify product_advice_card_dismissed prop

* store is dismissed state in the user pref

* delegate the dismiss state to the component

* changelog

* remove console.log :'(

* fix TS issue

* fix TS issue

* changelog
This commit is contained in:
Damián Suárez 2024-01-04 12:17:03 -03:00 committed by GitHub
parent 8550da9959
commit 17c8354ad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
[Product Block Editor]: dismiss AdviceCard by clicking close button

View File

@ -30,12 +30,16 @@ export type UserPreferences = {
variations_report_columns?: string;
local_attributes_notice_dismissed_ids?: number[];
variable_items_without_price_notice_dismissed?: Record< number, string >;
product_advice_card_dismissed?: {
[ key: string ]: 'yes' | 'no';
};
};
export type WoocommerceMeta = UserPreferences & {
task_list_tracked_started_tasks?: string;
variable_items_without_price_notice_dismissed?: string;
local_attributes_notice_dismissed_ids?: string;
product_advice_card_dismissed?: string;
};
export type WCUser<

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
[Product Block Editor]: dismiss AdviceCard by clicking close button

View File

@ -28,7 +28,6 @@ export function CrossSellsBlockEdit( {
'woocommerce'
) }
image={ <CashRegister /> }
isDismissible={ true }
/>
</div>
);

View File

@ -26,7 +26,6 @@ export function UpsellsBlockEdit( { attributes }: UpsellsBlockEditComponent ) {
'woocommerce'
) }
image={ <ShoppingBags /> }
isDismissible={ true }
/>
</div>
);

View File

@ -11,6 +11,8 @@ import {
CardHeader,
} from '@wordpress/components';
import { close } from '@wordpress/icons';
import { useInstanceId } from '@wordpress/compose';
import { useUserPreferences } from '@woocommerce/data';
export interface AdviceCardProps {
tip?: string;
@ -22,9 +24,37 @@ export interface AdviceCardProps {
export const AdviceCard: React.FC< AdviceCardProps > = ( {
tip,
image = null,
isDismissible = false,
onDismiss = () => {},
isDismissible = true,
onDismiss,
} ) => {
// Generate a unique ID for the advice card.
const adviceCardId = useInstanceId(
AdviceCard,
'advice-card-instance'
) as string;
const { updateUserPreferences, product_advice_card_dismissed } =
useUserPreferences();
if ( ! onDismiss ) {
onDismiss = () => {
updateUserPreferences( {
product_advice_card_dismissed: {
...product_advice_card_dismissed,
[ adviceCardId ]: 'yes',
},
} );
};
}
// Check if the advice card has been dismissed.
if (
product_advice_card_dismissed &&
product_advice_card_dismissed?.[ adviceCardId ] === 'yes'
) {
return null;
}
return (
<Card className="woocommerce-advice-card">
{ isDismissible && (

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
[Product Block Editor]: dismiss AdviceCard by clicking close button

View File

@ -185,6 +185,7 @@ class Init {
'variable_product_block_tour_shown',
'local_attributes_notice_dismissed_ids',
'variable_items_without_price_notice_dismissed',
'product_advice_card_dismissed',
)
);
}