-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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", [] ), | ||
}, | ||
} ); | ||
|
||
/** | ||
* Hide an alert. | ||
* | ||
* @param {object} state The state. | ||
* @param {string} type The type of the alert. | ||
* @param {string} id The id of the alert to hide. | ||
* | ||
* @returns {void} | ||
*/ | ||
const hideAlert = ( state, type, id ) => { | ||
const alertToHide = state[ type ].active.find( ( alert ) => alert.id === id ); | ||
if ( alertToHide ) { | ||
state[ type ].active = state[ type ].active.filter( ( alert ) => alert.id !== id ); | ||
state[ type ].dismissed.push( alertToHide ); | ||
} | ||
}; | ||
|
||
/** | ||
* Show an alert. | ||
* | ||
* @param {object} state The state. | ||
* @param {string} type The type of the alert. | ||
* @param {string} id The id of the alert to hide. | ||
* | ||
* @returns {void} | ||
*/ | ||
const showAlert = ( state, type, id ) => { | ||
const alertToShow = state[ type ].dismissed.find( ( alert ) => alert.id === id ); | ||
if ( alertToShow ) { | ||
state[ type ].dismissed = state[ type ].active.filter( ( alert ) => alert.id !== id ); | ||
state[ type ].active.push( alertToShow ); | ||
} | ||
}; | ||
|
||
const slice = createSlice( { | ||
name: "alertCenter", | ||
initialState: createInitialAlertCenterState(), | ||
reducers: { | ||
hideNotification: ( state, action ) => { | ||
hideAlert( state, "notifications", action.payload ); | ||
}, | ||
hideProblem: ( state, action ) => { | ||
hideAlert( state, "problems", action.payload ); | ||
}, | ||
showNotification: ( state, action ) => { | ||
showAlert( state, "notifications", action.payload ); | ||
}, | ||
showProblem: ( state, action ) => { | ||
showAlert( state, "problems", 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters