Initial commit
This commit is contained in:
Executable
+7
@@ -0,0 +1,7 @@
|
||||
enum ActionTypes {
|
||||
SET_USER = 'SET_USER',
|
||||
LOGOUT = 'LOGOUT',
|
||||
ADD_NOTIFICATION = 'ADD_NOTIFICATION'
|
||||
}
|
||||
|
||||
export default ActionTypes;
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
import ActionTypes from "./actionTypes";
|
||||
import React from "react";
|
||||
|
||||
const setUser = (userData: UserType) => ({
|
||||
type: ActionTypes.SET_USER,
|
||||
payload: { userData }
|
||||
});
|
||||
const logout = () => ({
|
||||
type: ActionTypes.LOGOUT
|
||||
});
|
||||
|
||||
const addNotification = (notification: React.ReactNode) => ({
|
||||
type: ActionTypes.ADD_NOTIFICATION,
|
||||
payload: { notification }
|
||||
});
|
||||
|
||||
export { setUser, logout,
|
||||
addNotification };
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
import ActionTypes from "../actionTypes";
|
||||
|
||||
const initialState = {
|
||||
authorized: false
|
||||
};
|
||||
|
||||
type Action = {
|
||||
type: string
|
||||
payload: {
|
||||
userData: UserType
|
||||
}
|
||||
}
|
||||
|
||||
export default (state = initialState, action: Action) => {
|
||||
switch (action.type) {
|
||||
case ActionTypes.SET_USER:
|
||||
if(action.payload.userData) {
|
||||
state = Object.assign({}, state, action.payload.userData);
|
||||
state.authorized = true;
|
||||
}
|
||||
return state;
|
||||
case ActionTypes.LOGOUT:
|
||||
state = {
|
||||
authorized: false
|
||||
};
|
||||
return state;
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
import {combineReducers} from "redux";
|
||||
import currentUser from "./currentUser";
|
||||
import notifications from "./notifications";
|
||||
|
||||
|
||||
export default combineReducers({ currentUser, notifications })
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
import React from "react";
|
||||
import ActionTypes from "../actionTypes";
|
||||
|
||||
const initialState: React.ReactNode[] = [];
|
||||
|
||||
type Action = {
|
||||
type: string
|
||||
payload: {
|
||||
notification: React.ReactNode[]
|
||||
}
|
||||
}
|
||||
|
||||
export default (state = initialState, action: Action) => {
|
||||
switch (action.type) {
|
||||
case ActionTypes.ADD_NOTIFICATION: {
|
||||
if(state.length === 3)
|
||||
return [...state.slice(1), action.payload.notification];
|
||||
return [...state, action.payload.notification];
|
||||
}
|
||||
default: return state;
|
||||
}
|
||||
}
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
import {createStore} from "redux";
|
||||
import index from "./reducers";
|
||||
import {composeWithDevTools} from "redux-devtools-extension";
|
||||
|
||||
const composeEnhancers = composeWithDevTools({
|
||||
trace: true
|
||||
});
|
||||
export default createStore(index, composeEnhancers());
|
||||
Reference in New Issue
Block a user