File tree Expand file tree Collapse file tree 8 files changed +83
-0
lines changed
Expand file tree Collapse file tree 8 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 22enum AlertTypes {
33 SUCCESS = 'sucess' ,
44 ERROR = 'error' ,
5+ INFO = 'info' ,
6+ WARNING = 'warning' ,
7+ }
8+
9+ interface IAlert {
10+ id : string ;
11+ msg : string ;
12+ type : AlertTypes ;
513}
Original file line number Diff line number Diff line change 1+ import * as types from './Alert.constants' ;
2+
3+ export const setAlertSuccess = ( payload : IAlert ) => ( {
4+ type : types . SET_ALERT ,
5+ payload,
6+ } ) ;
7+
8+ export const removeAlertSuccess = ( payload : string ) => ( {
9+ type : types . REMOVE_ALERT ,
10+ payload,
11+ } ) ;
Original file line number Diff line number Diff line change 1+ export const SET_ALERT = 'view/Alert/SET_ALERT' ;
2+ export const REMOVE_ALERT = 'view/Alert/REMOVE_ALERT' ;
Original file line number Diff line number Diff line change 1+ import * as types from './Alert.constants' ;
2+
3+ const initialState = [ ] as IAlert [ ] ;
4+
5+ export const alertReducer = ( state = initialState , action : ActionRedux ) => {
6+ const { type, payload } = action ;
7+ switch ( type ) {
8+ case types . SET_ALERT :
9+ return [ ...state , payload ] ;
10+ case types . REMOVE_ALERT :
11+ return state . filter ( alert => alert . id !== payload ) ;
12+ default :
13+ return state ;
14+ }
15+ } ;
Original file line number Diff line number Diff line change 1+ import * as actions from './Alert.actions' ;
2+ import { v4 as uuid } from 'uuid' ;
3+
4+ interface PayloadAlert {
5+ msg : string ;
6+ type : AlertTypes ;
7+ }
8+ export const setAlert = ( payload : PayloadAlert ) => async dispatch => {
9+ const newAlert = { ...payload , id : uuid ( ) } ;
10+ dispatch ( actions . setAlertSuccess ( newAlert ) ) ;
11+ setTimeout ( ( ) => dispatch ( actions . removeAlertSuccess ( newAlert . id ) ) , 4000 ) ;
12+ } ;
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import { connect , ConnectedProps } from 'react-redux' ;
3+ import { Alert } from 'antd' ;
4+
5+ const mapStateToProps = ( state : AppState ) => ( {
6+ alerts : state . alerts ,
7+ } ) ;
8+ const mapDispatchToProps = { } ;
9+
10+ const connector = connect ( mapStateToProps , mapDispatchToProps ) ;
11+ interface Props extends ConnectedProps < typeof connector > { }
12+
13+ const _AppAlert = ( props : Props ) => {
14+ const { alerts } = props ;
15+ return (
16+ alerts !== null &&
17+ alerts . length > 0 &&
18+ alerts . map ( ( alert , index ) => {
19+ return (
20+ < Alert
21+ message = { alert . msg }
22+ type = { alert . type }
23+ key = { index }
24+ showIcon
25+ closable
26+ > </ Alert >
27+ ) ;
28+ } )
29+ ) ;
30+ } ;
31+
32+ export const AppAlert = connector ( _AppAlert ) ;
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import axios from 'axios';
22import { URL } from 'src/constants/urls' ;
33import * as actions from './Auth.actions' ;
44import { v4 as uuid } from 'uuid' ;
5+ import { setAlert } from 'src/components/Alert/Alert.thunks' ;
56
67export const loadUser = ( ) => async dispatch => {
78 const userJson = localStorage . getItem ( 'user' ) || '{}' ;
Original file line number Diff line number Diff line change 11import { combineReducers } from 'redux' ;
22import { authReducer } from 'src/components/Auth/Auth.reducers' ;
33import { productReducer } from 'src/components/Products/Product.reducers' ;
4+ import { alertReducer } from 'src/components/Alert/Alert.reducers' ;
45
56export const RootReducer = combineReducers ( {
67 auth : authReducer ,
78 products : productReducer ,
9+ alerts : alertReducer ,
810} ) ;
You can’t perform that action at this time.
0 commit comments