1
0
mirror of https://github.com/franjsco/tick3t synced 2025-01-29 17:59:28 +01:00

feat: redux

This commit is contained in:
Francesco Esposito 2019-07-31 16:04:00 +02:00
parent 2a46594dde
commit 7105bb4d9d
10 changed files with 132 additions and 1 deletions

1
src/_actions/index.js Normal file
View File

@ -0,0 +1 @@
export * from './user.actions';

View File

@ -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}
}

1
src/_constants/index.js Normal file
View File

@ -0,0 +1 @@
export * from './user.constants';

View File

@ -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',
}

View File

@ -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;
}

3
src/_helpers/index.js Normal file
View File

@ -0,0 +1,3 @@
export * from './history';
export * from './store';
export * from './auth-header';

17
src/_helpers/store.js Normal file
View File

@ -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,
)),
)

9
src/_reducers/index.js Normal file
View File

@ -0,0 +1,9 @@
import { combineReducers } from 'redux';
import { user } from './user.reducer';
const rootReducers = combineReducers({
user,
});
export default rootReducers;

View File

@ -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 {};
}
}

View File

@ -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.