import { Color } from "@material-ui/lab/Alert" ; import { createSlice , PayloadAction } from "@reduxjs/toolkit" ; export type Message = IMessage & { severity : Color ; } ; export interface MessagesState { message : Message | null ; } interface IMessage { text : string ; error ? : any ; } const createMessage = function ( state , severity , text ) { const message = { severity , ... text , } ; state . message = message ; } ; const initialState = { message : null , } ; const messagesSlice = createSlice ( { name : "messages" , initialState , reducers : { error ( state , action ) { createMessage ( state , "error" , action . payload ) ; } , info ( state , action ) { createMessage ( state , "info" , action . payload ) ; } , warning ( state , action ) { createMessage ( state , "warning" , action . payload ) ; } , success ( state , action ) { createMessage ( state , "success" , action . payload ) ; } , close ( state ) { state . message = null ; } , } , } ) ; export const { error , info , close , warning , success } = messagesSlice . actions ; export default messagesSlice . reducer ;