mirror of
https://github.com/franjsco/tick3t
synced 2025-06-05 22:19:18 +02:00
feat: redux
This commit is contained in:
1
src/_actions/index.js
Normal file
1
src/_actions/index.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './user.actions';
|
37
src/_actions/user.actions.js
Normal file
37
src/_actions/user.actions.js
Normal 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
1
src/_constants/index.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './user.constants';
|
8
src/_constants/user.constants.js
Normal file
8
src/_constants/user.constants.js
Normal 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',
|
||||||
|
}
|
8
src/_helpers/auth-header.js
Normal file
8
src/_helpers/auth-header.js
Normal 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
3
src/_helpers/index.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export * from './history';
|
||||||
|
export * from './store';
|
||||||
|
export * from './auth-header';
|
17
src/_helpers/store.js
Normal file
17
src/_helpers/store.js
Normal 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
9
src/_reducers/index.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { combineReducers } from 'redux';
|
||||||
|
import { user } from './user.reducer';
|
||||||
|
|
||||||
|
|
||||||
|
const rootReducers = combineReducers({
|
||||||
|
user,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default rootReducers;
|
40
src/_reducers/user.reducer.js
Normal file
40
src/_reducers/user.reducer.js
Normal 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 {};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,10 +1,17 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
|
import { Provider } from 'react-redux';
|
||||||
|
|
||||||
|
import { store } from './_helpers'
|
||||||
import App from './App';
|
import App from './App';
|
||||||
import * as serviceWorker from './serviceWorker';
|
import * as serviceWorker from './serviceWorker';
|
||||||
|
|
||||||
require('dotenv').config();
|
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
|
// If you want your app to work offline and load faster, you can change
|
||||||
// unregister() to register() below. Note this comes with some pitfalls.
|
// unregister() to register() below. Note this comes with some pitfalls.
|
||||||
|
Reference in New Issue
Block a user