-
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.
WIP on 271-add-problem-card-to-alert-center
- Loading branch information
Showing
4 changed files
with
97 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,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; |
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