Skip to content

Commit 372e521

Browse files
committed
Create Alert actions
1 parent cc03700 commit 372e521

File tree

8 files changed

+83
-0
lines changed

8 files changed

+83
-0
lines changed

src/@types/alert.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22
enum 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
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const SET_ALERT = 'view/Alert/SET_ALERT';
2+
export const REMOVE_ALERT = 'view/Alert/REMOVE_ALERT';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};

src/components/Alert/index.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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);

src/components/Auth/Auth.thunks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import axios from 'axios';
22
import { URL } from 'src/constants/urls';
33
import * as actions from './Auth.actions';
44
import { v4 as uuid } from 'uuid';
5+
import { setAlert } from 'src/components/Alert/Alert.thunks';
56

67
export const loadUser = () => async dispatch => {
78
const userJson = localStorage.getItem('user') || '{}';

src/store/reducers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { combineReducers } from 'redux';
22
import { authReducer } from 'src/components/Auth/Auth.reducers';
33
import { productReducer } from 'src/components/Products/Product.reducers';
4+
import { alertReducer } from 'src/components/Alert/Alert.reducers';
45

56
export const RootReducer = combineReducers({
67
auth: authReducer,
78
products: productReducer,
9+
alerts: alertReducer,
810
});

0 commit comments

Comments
 (0)