From 7105bb4d9d162f6a7f5b2dc5695f5889f5b96ca1 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frsposito@users.noreply.github.com> Date: Wed, 31 Jul 2019 16:04:00 +0200 Subject: [PATCH] feat: redux --- src/_actions/index.js | 1 + src/_actions/user.actions.js | 37 +++++++++++++++++++++++++++++ src/_constants/index.js | 1 + src/_constants/user.constants.js | 8 +++++++ src/_helpers/auth-header.js | 8 +++++++ src/_helpers/index.js | 3 +++ src/_helpers/store.js | 17 ++++++++++++++ src/_reducers/index.js | 9 +++++++ src/_reducers/user.reducer.js | 40 ++++++++++++++++++++++++++++++++ src/index.js | 9 ++++++- 10 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/_actions/index.js create mode 100644 src/_actions/user.actions.js create mode 100644 src/_constants/index.js create mode 100644 src/_constants/user.constants.js create mode 100644 src/_helpers/auth-header.js create mode 100644 src/_helpers/index.js create mode 100644 src/_helpers/store.js create mode 100644 src/_reducers/index.js create mode 100644 src/_reducers/user.reducer.js diff --git a/src/_actions/index.js b/src/_actions/index.js new file mode 100644 index 0000000..45f26de --- /dev/null +++ b/src/_actions/index.js @@ -0,0 +1 @@ +export * from './user.actions'; \ No newline at end of file diff --git a/src/_actions/user.actions.js b/src/_actions/user.actions.js new file mode 100644 index 0000000..1ba0f8b --- /dev/null +++ b/src/_actions/user.actions.js @@ -0,0 +1,37 @@ +import { userConstants } from '../_constants/user.constants'; +import { userAPI } from '../api/user'; + +export const userActions = { + login, + logout, + reset, +} + +function login(email, password) { + return dispatch => { + dispatch(request({ email })) + + userAPI.login(email, password) + .then(user => { + dispatch(success(user)); + }, + error => { + dispatch(failure(error.toString())); + } + ); + + }; + + function request(user) { return { type: userConstants.LOGIN_REQUEST, user } }; + function success(user) { return { type: userConstants.LOGIN_SUCCESS, user } }; + function failure(error) { return { type: userConstants.LOGIN_FAILURE, error } } +} + +function logout() { + userAPI.logout(); + return { type: userConstants.LOGOUT }; +} + +function reset() { + return { type: userConstants.LOGIN_RESET} +} \ No newline at end of file diff --git a/src/_constants/index.js b/src/_constants/index.js new file mode 100644 index 0000000..b5b76e8 --- /dev/null +++ b/src/_constants/index.js @@ -0,0 +1 @@ +export * from './user.constants'; \ No newline at end of file diff --git a/src/_constants/user.constants.js b/src/_constants/user.constants.js new file mode 100644 index 0000000..7bdc12c --- /dev/null +++ b/src/_constants/user.constants.js @@ -0,0 +1,8 @@ +export const userConstants = { + LOGIN_REQUEST: 'LOGIN_REQUEST', + LOGIN_SUCCESS: 'LOGIN_SUCCESS', + LOGIN_FAILURE: 'LOGIN_FAILURE', + LOGIN_RESET: 'LOGIN_RESET', + + LOGOUT: 'LOGOUT', +} \ No newline at end of file diff --git a/src/_helpers/auth-header.js b/src/_helpers/auth-header.js new file mode 100644 index 0000000..3ffe25e --- /dev/null +++ b/src/_helpers/auth-header.js @@ -0,0 +1,8 @@ +export function authHeader() { + let user = JSON.parse(localStorage.getItem('user')); + + if (user.data.token) { + return `Bearer ${user.data.token}` ; + } else + return null; +} \ No newline at end of file diff --git a/src/_helpers/index.js b/src/_helpers/index.js new file mode 100644 index 0000000..a73a6c2 --- /dev/null +++ b/src/_helpers/index.js @@ -0,0 +1,3 @@ +export * from './history'; +export * from './store'; +export * from './auth-header'; \ No newline at end of file diff --git a/src/_helpers/store.js b/src/_helpers/store.js new file mode 100644 index 0000000..99d9014 --- /dev/null +++ b/src/_helpers/store.js @@ -0,0 +1,17 @@ +import { createStore, applyMiddleware} from 'redux'; +import { composeWithDevTools } from 'redux-devtools-extension'; +import thunkMiddleware from 'redux-thunk'; + +import { createLogger } from 'redux-logger'; + +import rootReducer from '../_reducers'; + +const loggerMiddleware = createLogger(); + +export const store = createStore( + rootReducer, + composeWithDevTools(applyMiddleware( + thunkMiddleware, + loggerMiddleware, + )), +) \ No newline at end of file diff --git a/src/_reducers/index.js b/src/_reducers/index.js new file mode 100644 index 0000000..fa3510f --- /dev/null +++ b/src/_reducers/index.js @@ -0,0 +1,9 @@ +import { combineReducers } from 'redux'; +import { user } from './user.reducer'; + + + const rootReducers = combineReducers({ + user, +}); + +export default rootReducers; \ No newline at end of file diff --git a/src/_reducers/user.reducer.js b/src/_reducers/user.reducer.js new file mode 100644 index 0000000..b372971 --- /dev/null +++ b/src/_reducers/user.reducer.js @@ -0,0 +1,40 @@ +import { userConstants } from '../_constants/user.constants'; + +let userLocalStorage = JSON.parse(localStorage.getItem('user')); + +const initialState = userLocalStorage ? { loggedIn: true } : { loggedIn: false}; + +export function user(state = initialState, action) { + switch (action.type) { + case userConstants.LOGIN_REQUEST: + return { + loggedIn: false, + }; + + case userConstants.LOGIN_SUCCESS: + return { + loggedIn: true, + }; + + case userConstants.LOGIN_FAILURE: + return { + loggedIn: false, + error: action.error, + }; + + case userConstants.LOGIN_RESET: + return { + loggedIn: false, + error: '' + }; + + case userConstants.LOGOUT: + return { + loggedIn: false, + }; + + default: + return {}; + + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index e42803b..7df5527 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,17 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import { Provider } from 'react-redux'; + +import { store } from './_helpers' import App from './App'; import * as serviceWorker from './serviceWorker'; + require('dotenv').config(); -ReactDOM.render(<App />, document.getElementById('root')); +ReactDOM.render( + <Provider store={store}> + <App /> + </Provider>, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls.