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

Adds alert center redux store #21649

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions admin/class-yoast-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ public function render() {
return '<div ' . implode( ' ', $attributes ) . '>' . $message . '</div>' . PHP_EOL;
}

/**
* Get the message for the notification.
*
* @return string The message.
*/
public function get_message() {
return wpautop( $this->message );
}

/**
* Wraps the message with a Yoast SEO icon.
*
Expand Down
67 changes: 67 additions & 0 deletions packages/js/src/dashboard/store/alert-center.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { createSlice } from "@reduxjs/toolkit";
import { get } from "lodash";

/**
* @returns {Object} The initial state.
*/
export const createInitialAlertCenterState = () => ( {
notifications: {
active: get( window, "wpseoScriptData.notifications.active", [] ),
dismissed: get( window, "wpseoScriptData.notifications.dismissed", [] ),
},
problems: {
active: get( window, "wpseoScriptData.problems.active", [] ),
dismissed: get( window, "wpseoScriptData.problems.dismissed", [] ),
},
} );

/**
* Change alert visability.
*
* @param {object} state The state.
* @param {string} type The type of the alert.
* @param {string} target The target visability of the alert.
* @param {string} id The id of the alert to hide.
*
* @returns {void}
*/
const changeAlertVisibility = ( state, type, target, id ) => {
const statuses = [ "active", "dismissed" ];
const source = statuses.find( ( status ) => status !== target );

const alertToShow = state[ type ][ source ].find( ( alert ) => alert.id === id );
if ( alertToShow ) {
state[ type ][ source ] = state[ type ].active.filter( ( alert ) => alert.id !== id );
state[ type ][ target ].push( alertToShow );
}
};

const slice = createSlice( {
name: "alertCenter",
initialState: createInitialAlertCenterState(),
reducers: {
hideNotification: ( state, action ) => {
changeAlertVisibility( state, "notifications", "dismissed", action.payload );
},
hideProblem: ( state, action ) => {
changeAlertVisibility( state, "problems", "dismissed", action.payload );
},
showNotification: ( state, action ) => {
changeAlertVisibility( state, "notifications", "active", action.payload );
},
showProblem: ( state, action ) => {
changeAlertVisibility( state, "problems", "active", action.payload );
},
},
} );

export const alertCenterSelectors = {
selectActiveProblems: ( state ) => get( state, "alertCenter.problems.active", [] ),
selectDismissedProblems: ( state ) => get( state, "alertCenter.problems.dismissed", [] ),
selectActiveNotifications: ( state ) => get( state, "alertCenter.notifications.active", [] ),
selectDismissedNotifications: ( state ) => get( state, "alertCenter.notifications.dismissed", [] ),
};

export const alertCenterActions = slice.actions;

export default slice.reducer;
5 changes: 5 additions & 0 deletions packages/js/src/dashboard/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { merge } from "lodash";
import { getInitialLinkParamsState, LINK_PARAMS_NAME, linkParamsActions, linkParamsReducer, linkParamsSelectors } from "../../shared-admin/store";
import { STORE_NAME } from "../constants";
import preferences, { createInitialPreferencesState, preferencesActions, preferencesSelectors } from "./preferences";
import alertCenter, { alertCenterActions, alertCenterSelectors, createInitialAlertCenterState } from "./alert-center";

/** @typedef {import("@wordpress/data/src/types").WPDataStore} WPDataStore */

Expand All @@ -16,22 +17,26 @@ const createStore = ( { initialState } ) => {
actions: {
...linkParamsActions,
...preferencesActions,
...alertCenterActions,
},
selectors: {
...linkParamsSelectors,
...preferencesSelectors,
...alertCenterSelectors,
},
initialState: merge(
{},
{
[ LINK_PARAMS_NAME ]: getInitialLinkParamsState(),
preferences: createInitialPreferencesState(),
alertCenter: createInitialAlertCenterState(),
},
initialState
),
reducer: combineReducers( {
[ LINK_PARAMS_NAME ]: linkParamsReducer,
preferences,
alertCenter,
} ),

} );
Expand Down
20 changes: 16 additions & 4 deletions src/helpers/notification-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ function ( $notification ) {
return [
'dismissed' => \array_map(
static function ( $notification ) {
return $notification->to_array();
return [
'id' => $notification->get_id(),
'message' => $notification->get_message(),
];
},
$dismissed_notifications
),
'active' => \array_map(
static function ( $notification ) {
return $notification->to_array();
return [
'id' => $notification->get_id(),
'message' => $notification->get_message(),
];
},
$active_notifications
),
Expand Down Expand Up @@ -113,13 +119,19 @@ function ( $notification ) {
return [
'dismissed' => \array_map(
static function ( $notification ) {
return $notification->to_array();
return [
'id' => $notification->get_id(),
'message' => $notification->get_message(),
];
},
$dismissed_problems
),
'active' => \array_map(
static function ( $notification ) {
return $notification->to_array();
return [
'id' => $notification->get_id(),
'message' => $notification->get_message(),
];
},
$active_problems
),
Expand Down