Commit 7c13e7ad authored by Robert Knight's avatar Robert Knight

Add missing types to toastMessages store module

parent 38a7e84a
import { createStoreModule } from '../create-store';
import * as util from '../util';
import { createStoreModule, makeAction } from '../create-store';
/**
* @typedef ToastMessage
......@@ -22,21 +20,35 @@ const initialState = {
messages: [],
};
/** @typedef {typeof initialState} State */
const reducers = {
ADD_MESSAGE: function (state, action) {
/**
* @param {State} state
* @param {{ message: ToastMessage }} action
*/
ADD_MESSAGE(state, action) {
return {
messages: state.messages.concat({ ...action.message }),
};
},
REMOVE_MESSAGE: function (state, action) {
/**
* @param {State} state
* @param {{ id: string }} action
*/
REMOVE_MESSAGE(state, action) {
const updatedMessages = state.messages.filter(
message => message.id !== action.id
);
return { messages: updatedMessages };
},
UPDATE_MESSAGE: function (state, action) {
/**
* @param {State} state
* @param {{ message: ToastMessage }} action
*/
UPDATE_MESSAGE(state, action) {
const updatedMessages = state.messages.map(message => {
if (message.id && message.id === action.message.id) {
return { ...action.message };
......@@ -47,15 +59,13 @@ const reducers = {
},
};
const actions = util.actionTypes(reducers);
/** Actions */
/**
* @param {ToastMessage} message
*/
function addMessage(message) {
return { type: actions.ADD_MESSAGE, message };
return makeAction(reducers, 'ADD_MESSAGE', { message });
}
/**
......@@ -64,7 +74,7 @@ function addMessage(message) {
* @param {string} id
*/
function removeMessage(id) {
return { type: actions.REMOVE_MESSAGE, id };
return makeAction(reducers, 'REMOVE_MESSAGE', { id });
}
/**
......@@ -73,7 +83,7 @@ function removeMessage(id) {
* @param {ToastMessage} message
*/
function updateMessage(message) {
return { type: actions.UPDATE_MESSAGE, message };
return makeAction(reducers, 'UPDATE_MESSAGE', { message });
}
/** Selectors */
......@@ -81,8 +91,8 @@ function updateMessage(message) {
/**
* Retrieve a message by `id`
*
* @param {State} state
* @param {string} id
* @return {object|undefined}
*/
function getMessage(state, id) {
return state.messages.find(message => message.id === id);
......@@ -91,7 +101,7 @@ function getMessage(state, id) {
/**
* Retrieve all current messages
*
* @return {object[]}
* @param {State} state
*/
function getMessages(state) {
return state.messages;
......@@ -102,9 +112,9 @@ function getMessages(state) {
* text exists in the state's collection of messages. This matches messages
* by content, not by ID (true uniqueness).
*
* @param {State} state
* @param {string} type
* @param {string} text
* @return {boolean}
*/
function hasMessage(state, type, text) {
return state.messages.some(message => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment