2019-02-19 21:07:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-06-18 00:22:20 +00:00
|
|
|
import { __, _n } from '@wordpress/i18n';
|
2024-06-27 20:37:25 +00:00
|
|
|
import { Fragment, useState } from '@wordpress/element';
|
2020-12-21 19:06:26 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Card,
|
|
|
|
CardBody,
|
|
|
|
CardFooter,
|
|
|
|
CardHeader,
|
|
|
|
SelectControl,
|
|
|
|
} from '@wordpress/components';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2022-02-21 02:34:25 +00:00
|
|
|
import interpolateComponents from '@automattic/interpolate-components';
|
2024-06-27 20:37:25 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2019-03-18 20:41:35 +00:00
|
|
|
import moment from 'moment';
|
2024-06-18 00:22:20 +00:00
|
|
|
import { Icon, chevronLeft, chevronRight, close } from '@wordpress/icons';
|
2024-06-27 20:37:25 +00:00
|
|
|
import {
|
|
|
|
NOTES_STORE_NAME,
|
|
|
|
QUERY_DEFAULTS,
|
|
|
|
OPTIONS_STORE_NAME,
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
useUserPreferences,
|
|
|
|
} from '@woocommerce/data';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2024-06-27 20:37:25 +00:00
|
|
|
import {
|
|
|
|
navigateTo,
|
|
|
|
parseAdminUrl,
|
|
|
|
getScreenFromPath,
|
|
|
|
isWCAdmin,
|
|
|
|
} from '@woocommerce/navigation';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-13 02:05:22 +00:00
|
|
|
import sanitizeHTML from '../../lib/sanitize-html';
|
2019-03-06 22:50:47 +00:00
|
|
|
import StoreAlertsPlaceholder from './placeholder';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2024-06-18 00:22:20 +00:00
|
|
|
import { getScreenName } from '../../utils';
|
2024-06-27 20:37:25 +00:00
|
|
|
import { hasTwoColumnLayout } from '../../homescreen/layout';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2019-03-01 17:20:28 +00:00
|
|
|
import './style.scss';
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const ALERTS_QUERY = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
type: 'error,update',
|
|
|
|
status: 'unactioned',
|
|
|
|
};
|
2019-02-20 15:25:56 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
function getUnactionedVisibleAlerts( alerts ) {
|
|
|
|
return ( alerts || [] ).filter(
|
|
|
|
( note ) => note.status === 'unactioned' && note.is_deleted !== true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const StoreAlerts = () => {
|
|
|
|
const [ currentIndex, setCurrentIndex ] = useState( 0 );
|
|
|
|
|
|
|
|
const {
|
|
|
|
alerts = [],
|
|
|
|
isLoading,
|
|
|
|
defaultHomescreenLayout,
|
|
|
|
taskListComplete,
|
|
|
|
isTaskListHidden,
|
|
|
|
isLoadingTaskLists,
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const { getNotes, hasFinishedResolution } = select( NOTES_STORE_NAME );
|
|
|
|
const { getOption } = select( OPTIONS_STORE_NAME );
|
|
|
|
const { getTaskList, hasFinishedResolution: taskListFinishResolution } =
|
|
|
|
select( ONBOARDING_STORE_NAME );
|
|
|
|
|
|
|
|
return {
|
|
|
|
alerts: getUnactionedVisibleAlerts( getNotes( ALERTS_QUERY ) ),
|
|
|
|
isLoading: ! hasFinishedResolution( 'getNotes', [ ALERTS_QUERY ] ),
|
|
|
|
defaultHomescreenLayout:
|
|
|
|
getOption( 'woocommerce_default_homepage_layout' ) ||
|
|
|
|
'single_column',
|
|
|
|
taskListComplete: getTaskList( 'setup' )?.isComplete,
|
|
|
|
isTaskListHidden: getTaskList( 'setup' )?.isHidden,
|
|
|
|
isLoadingTaskLists: ! taskListFinishResolution( 'getTaskLists' ),
|
2019-02-19 21:07:19 +00:00
|
|
|
};
|
2024-06-27 20:37:25 +00:00
|
|
|
} );
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const { triggerNoteAction, updateNote, removeNote } =
|
|
|
|
useDispatch( NOTES_STORE_NAME );
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const userPrefs = useUserPreferences();
|
2024-06-18 00:22:20 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
function previousAlert( event ) {
|
2023-07-04 19:54:13 +00:00
|
|
|
event?.stopPropagation();
|
2019-02-19 21:07:19 +00:00
|
|
|
|
2019-02-20 16:18:10 +00:00
|
|
|
if ( currentIndex > 0 ) {
|
2024-06-27 20:37:25 +00:00
|
|
|
setCurrentIndex( currentIndex - 1 );
|
2019-02-19 21:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
function nextAlert( event ) {
|
2019-02-19 21:07:19 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
|
2019-02-20 16:18:10 +00:00
|
|
|
if ( currentIndex < alerts.length - 1 ) {
|
2024-06-27 20:37:25 +00:00
|
|
|
setCurrentIndex( currentIndex + 1 );
|
2019-02-19 21:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
function renderActions( alert ) {
|
2024-07-16 06:19:33 +00:00
|
|
|
const actions = alert.actions.map( ( action, idx ) => {
|
|
|
|
const variant = idx === 0 ? 'secondary' : 'tertiary';
|
2019-03-18 19:05:44 +00:00
|
|
|
return (
|
|
|
|
<Button
|
2024-08-09 08:44:09 +00:00
|
|
|
key={ `${ alert.id }-${ action.label }-${ action.name }` }
|
2024-07-16 06:19:33 +00:00
|
|
|
variant={ variant }
|
2019-05-24 17:05:12 +00:00
|
|
|
href={ action.url || undefined }
|
2023-05-02 14:17:50 +00:00
|
|
|
onClick={ async ( event ) => {
|
|
|
|
const url = event.currentTarget.getAttribute( 'href' );
|
|
|
|
event.preventDefault();
|
2023-07-04 19:54:13 +00:00
|
|
|
|
|
|
|
// navigate to previous alert to avoid an out of bounds error in case it's the last alert from the array
|
2024-06-27 20:37:25 +00:00
|
|
|
previousAlert();
|
2023-08-28 14:48:07 +00:00
|
|
|
try {
|
|
|
|
await triggerNoteAction( alert.id, action.id );
|
|
|
|
if (
|
|
|
|
url &&
|
|
|
|
url !== '#' &&
|
|
|
|
parseAdminUrl( url ).href !==
|
|
|
|
window.location.href
|
|
|
|
) {
|
|
|
|
navigateTo( { url } );
|
|
|
|
}
|
|
|
|
} catch ( e ) {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
|
|
|
`Something went wrong while triggering this note's action.`,
|
|
|
|
'woocommerce'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
throw e;
|
2023-05-02 14:17:50 +00:00
|
|
|
}
|
|
|
|
} }
|
2019-03-18 19:05:44 +00:00
|
|
|
>
|
|
|
|
{ action.label }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2019-03-18 20:41:35 +00:00
|
|
|
// TODO: should "next X" be the start, or exactly 1X from the current date?
|
2019-03-18 19:05:44 +00:00
|
|
|
const snoozeOptions = [
|
|
|
|
{
|
2020-07-28 02:32:58 +00:00
|
|
|
value: moment().add( 4, 'hours' ).unix().toString(),
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Later Today', 'woocommerce' ),
|
2019-03-18 19:05:44 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 17:35:29 +00:00
|
|
|
value: moment()
|
2019-03-18 20:41:35 +00:00
|
|
|
.add( 1, 'day' )
|
|
|
|
.hour( 9 )
|
|
|
|
.minute( 0 )
|
|
|
|
.second( 0 )
|
|
|
|
.millisecond( 0 )
|
2019-12-13 17:35:29 +00:00
|
|
|
.unix()
|
|
|
|
.toString(),
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Tomorrow', 'woocommerce' ),
|
2019-03-18 19:05:44 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 17:35:29 +00:00
|
|
|
value: moment()
|
2019-03-18 20:41:35 +00:00
|
|
|
.add( 1, 'week' )
|
|
|
|
.hour( 9 )
|
|
|
|
.minute( 0 )
|
|
|
|
.second( 0 )
|
|
|
|
.millisecond( 0 )
|
2019-12-13 17:35:29 +00:00
|
|
|
.unix()
|
|
|
|
.toString(),
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Next Week', 'woocommerce' ),
|
2019-03-18 19:05:44 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-13 17:35:29 +00:00
|
|
|
value: moment()
|
2019-03-18 20:41:35 +00:00
|
|
|
.add( 1, 'month' )
|
|
|
|
.hour( 9 )
|
|
|
|
.minute( 0 )
|
|
|
|
.second( 0 )
|
|
|
|
.millisecond( 0 )
|
2019-12-13 17:35:29 +00:00
|
|
|
.unix()
|
|
|
|
.toString(),
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Next Month', 'woocommerce' ),
|
2019-03-18 19:05:44 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const setReminderDate = ( snoozeOption ) => {
|
|
|
|
updateNote( alert.id, {
|
|
|
|
status: 'snoozed',
|
|
|
|
date_reminder: snoozeOption.value,
|
|
|
|
} );
|
2019-12-13 17:35:29 +00:00
|
|
|
|
|
|
|
const eventProps = {
|
|
|
|
alert_name: alert.name,
|
|
|
|
alert_title: alert.title,
|
|
|
|
snooze_duration: snoozeOption.value,
|
|
|
|
snooze_label: snoozeOption.label,
|
2019-03-18 19:05:44 +00:00
|
|
|
};
|
2019-12-13 17:35:29 +00:00
|
|
|
recordEvent( 'store_alert_snooze', eventProps );
|
2019-03-18 19:05:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const snooze = alert.is_snoozable && (
|
2019-12-13 17:35:29 +00:00
|
|
|
<SelectControl
|
2019-03-19 15:50:37 +00:00
|
|
|
className="woocommerce-store-alerts__snooze"
|
2019-12-13 17:35:29 +00:00
|
|
|
options={ [
|
|
|
|
{
|
2022-03-30 09:00:04 +00:00
|
|
|
label: __( 'Remind Me Later', 'woocommerce' ),
|
2019-12-13 17:35:29 +00:00
|
|
|
value: '0',
|
|
|
|
},
|
|
|
|
...snoozeOptions,
|
|
|
|
] }
|
2020-02-14 02:23:21 +00:00
|
|
|
onChange={ ( value ) => {
|
|
|
|
if ( value === '0' ) {
|
2019-12-13 17:35:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const reminderOption = snoozeOptions.find(
|
|
|
|
( option ) => option.value === value
|
|
|
|
);
|
2019-12-13 17:35:29 +00:00
|
|
|
const reminderDate = {
|
|
|
|
value,
|
|
|
|
label: reminderOption && reminderOption.label,
|
|
|
|
};
|
|
|
|
setReminderDate( reminderDate );
|
|
|
|
} }
|
2019-03-18 19:05:44 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( actions || snooze ) {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-store-alerts__actions">
|
|
|
|
{ actions }
|
|
|
|
{ snooze }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
if ( isLoadingTaskLists ) {
|
|
|
|
return null;
|
2021-02-09 15:19:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const preloadAlertCount = getAdminSetting( 'alertCount', 0, ( count ) =>
|
|
|
|
parseInt( count, 10 )
|
|
|
|
);
|
2019-03-12 13:13:20 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const isWCAdminPage = isWCAdmin();
|
|
|
|
const isHomescreen = isWCAdminPage && getScreenFromPath() === 'homescreen';
|
2024-06-18 00:22:20 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const hasTwoColumns = hasTwoColumnLayout(
|
|
|
|
userPrefs.homepage_layout,
|
|
|
|
defaultHomescreenLayout,
|
|
|
|
taskListComplete,
|
|
|
|
isTaskListHidden
|
|
|
|
);
|
2024-06-18 00:22:20 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
if ( preloadAlertCount > 0 && isLoading ) {
|
2019-03-08 16:17:48 +00:00
|
|
|
return (
|
2024-06-27 20:37:25 +00:00
|
|
|
<StoreAlertsPlaceholder
|
|
|
|
className={ clsx( {
|
|
|
|
'is-wc-admin-page': isWCAdminPage,
|
|
|
|
'is-homescreen': isHomescreen,
|
|
|
|
'two-columns': hasTwoColumns && isHomescreen,
|
|
|
|
} ) }
|
|
|
|
hasMultipleAlerts={ preloadAlertCount > 1 }
|
|
|
|
/>
|
2019-02-19 21:07:19 +00:00
|
|
|
);
|
2024-06-27 20:37:25 +00:00
|
|
|
} else if ( alerts.length === 0 ) {
|
|
|
|
return null;
|
2019-02-19 21:07:19 +00:00
|
|
|
}
|
2019-02-20 15:25:56 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const numberOfAlerts = alerts.length;
|
|
|
|
const alert = alerts[ currentIndex ];
|
|
|
|
const type = alert.type;
|
|
|
|
const className = clsx( 'woocommerce-store-alerts', {
|
|
|
|
'is-alert-error': type === 'error',
|
|
|
|
'is-alert-update': type === 'update',
|
|
|
|
'is-wc-admin-page': isWCAdminPage,
|
|
|
|
'is-homescreen': isHomescreen,
|
|
|
|
'two-columns': hasTwoColumns && isHomescreen,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const onDismiss = async ( note ) => {
|
|
|
|
const screen = getScreenName();
|
|
|
|
|
|
|
|
recordEvent( 'inbox_action_dismiss', {
|
|
|
|
note_name: note.name,
|
|
|
|
note_title: note.title,
|
|
|
|
note_name_dismiss_all: false,
|
|
|
|
note_name_dismiss_confirmation: true,
|
|
|
|
screen,
|
|
|
|
} );
|
2019-02-20 16:37:03 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
const noteId = note.id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await removeNote( noteId );
|
|
|
|
createNotice( 'success', __( 'Message dismissed', 'woocommerce' ) );
|
|
|
|
} catch ( e ) {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
_n(
|
|
|
|
'Message could not be dismissed',
|
|
|
|
'Messages could not be dismissed',
|
|
|
|
1,
|
|
|
|
'woocommerce'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card className={ className } size={ null }>
|
2024-07-16 06:19:33 +00:00
|
|
|
<CardHeader
|
|
|
|
className="woocommerce-store-alerts__header"
|
|
|
|
isBorderless
|
2024-06-27 20:37:25 +00:00
|
|
|
>
|
2024-07-16 06:19:33 +00:00
|
|
|
<span className="woocommerce-store-alerts__title">
|
2024-06-27 20:37:25 +00:00
|
|
|
{ alert.title }
|
2024-07-16 06:19:33 +00:00
|
|
|
</span>
|
2024-06-27 20:37:25 +00:00
|
|
|
{ numberOfAlerts > 1 && (
|
|
|
|
<div className="woocommerce-store-alerts__pagination">
|
|
|
|
<span
|
|
|
|
className="woocommerce-store-alerts__pagination-label"
|
|
|
|
role="status"
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: __(
|
|
|
|
'{{current /}} of {{total /}}',
|
|
|
|
'woocommerce'
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
current: (
|
|
|
|
<Fragment>
|
|
|
|
{ currentIndex + 1 }
|
|
|
|
</Fragment>
|
|
|
|
),
|
|
|
|
total: (
|
|
|
|
<Fragment>{ numberOfAlerts }</Fragment>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</span>
|
2024-07-16 06:19:33 +00:00
|
|
|
<Button
|
|
|
|
onClick={ previousAlert }
|
|
|
|
disabled={ currentIndex === 0 }
|
|
|
|
label={ __( 'Previous Alert', 'woocommerce' ) }
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
icon={ chevronLeft }
|
|
|
|
className="arrow-left-icon"
|
|
|
|
/>
|
|
|
|
</Button>
|
2024-06-27 20:37:25 +00:00
|
|
|
<Button
|
|
|
|
onClick={ nextAlert }
|
|
|
|
disabled={ numberOfAlerts - 1 === currentIndex }
|
|
|
|
label={ __( 'Next Alert', 'woocommerce' ) }
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
icon={ chevronRight }
|
|
|
|
className="arrow-right-icon"
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) }
|
2024-07-16 06:19:33 +00:00
|
|
|
<Button
|
|
|
|
className="woocommerce-store-alerts__close"
|
|
|
|
onClick={ () => onDismiss( alert ) }
|
|
|
|
>
|
|
|
|
<Icon width="18" height="18" icon={ close } />
|
|
|
|
</Button>
|
2024-06-27 20:37:25 +00:00
|
|
|
</CardHeader>
|
|
|
|
<CardBody>
|
|
|
|
<div
|
|
|
|
className="woocommerce-store-alerts__message"
|
|
|
|
dangerouslySetInnerHTML={ sanitizeHTML( alert.content ) }
|
|
|
|
/>
|
|
|
|
</CardBody>
|
|
|
|
<CardFooter isBorderless>{ renderActions( alert ) }</CardFooter>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
2019-05-24 20:22:46 +00:00
|
|
|
|
2024-06-27 20:37:25 +00:00
|
|
|
export default StoreAlerts;
|