2021-08-11 22:09:32 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-04-25 05:49:11 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2021-08-11 22:09:32 +00:00
|
|
|
import { Button, Card, CardHeader } from '@wordpress/components';
|
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { EllipsisMenu } from '@woocommerce/components';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { createContext, useContext } from '@wordpress/element';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './dismissable-list.scss';
|
|
|
|
|
|
|
|
// using a context provider for the option name so that the option name prop doesn't need to be passed to the `DismissableListHeading` too
|
|
|
|
const OptionNameContext = createContext( '' );
|
|
|
|
|
|
|
|
export const DismissableListHeading: React.FC< {
|
|
|
|
onDismiss?: () => void;
|
|
|
|
} > = ( { children, onDismiss = () => null } ) => {
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const dismissOptionName = useContext( OptionNameContext );
|
|
|
|
|
|
|
|
const handleDismissClick = () => {
|
|
|
|
onDismiss();
|
|
|
|
updateOptions( {
|
|
|
|
[ dismissOptionName ]: 'yes',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CardHeader>
|
|
|
|
<div className="woocommerce-dismissable-list__header">
|
|
|
|
{ children }
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<EllipsisMenu
|
2022-03-30 09:00:04 +00:00
|
|
|
label={ __( 'Task List Options', 'woocommerce' ) }
|
2021-08-11 22:09:32 +00:00
|
|
|
renderContent={ () => (
|
|
|
|
<div className="woocommerce-dismissable-list__controls">
|
|
|
|
<Button onClick={ handleDismissClick }>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Hide this', 'woocommerce' ) }
|
2021-08-11 22:09:32 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</CardHeader>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DismissableList: React.FC< {
|
|
|
|
dismissOptionName: string;
|
|
|
|
className?: string;
|
|
|
|
} > = ( { children, className, dismissOptionName } ) => {
|
2022-04-25 05:49:11 +00:00
|
|
|
const isVisible = useSelect( ( select ) => {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { getOption, hasFinishedResolution } =
|
|
|
|
select( OPTIONS_STORE_NAME );
|
2021-08-11 22:09:32 +00:00
|
|
|
|
|
|
|
const hasFinishedResolving = hasFinishedResolution( 'getOption', [
|
|
|
|
dismissOptionName,
|
|
|
|
] );
|
|
|
|
|
|
|
|
const isDismissed = getOption( dismissOptionName ) === 'yes';
|
|
|
|
|
|
|
|
return hasFinishedResolving && ! isDismissed;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( ! isVisible ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
|
|
|
size="medium"
|
|
|
|
className={ classNames(
|
|
|
|
'woocommerce-dismissable-list',
|
|
|
|
className
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<OptionNameContext.Provider value={ dismissOptionName }>
|
|
|
|
{ children }
|
|
|
|
</OptionNameContext.Provider>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|