diff --git a/client/app/components/redux/reducer.jsx b/client/app/components/redux/reducer.jsx new file mode 100644 index 0000000..18317d7 --- /dev/null +++ b/client/app/components/redux/reducer.jsx @@ -0,0 +1,108 @@ + +import { setCookie } from '../Cookie.jsx'; + +const SET_LOGIN_PENDING = 'SET_LOGIN_PENDING'; +const SET_LOGIN_SUCCESS = 'SET_LOGIN_SUCCESS'; +const SET_LOGIN_ERROR = 'SET_LOGIN_ERROR'; + +export function login(email, password) { + return dispatch => { + dispatch(setLoginPending(true)); + dispatch(setLoginSuccess(false)); + dispatch(setLoginError(null)); + + callLoginApi(email, password, error => { + dispatch(setLoginPending(false)); + if (!error) { + dispatch(setLoginSuccess(true)); + } else { + dispatch(setLoginError(error)); + } + }); + } +} + +function setLoginPending(isLoginPending) { + return { + type: SET_LOGIN_PENDING, + isLoginPending + }; +} + +function setLoginSuccess(isLoginSuccess) { + return { + type: SET_LOGIN_SUCCESS, + isLoginSuccess + }; +} + +function setLoginError(loginError) { + return { + type: SET_LOGIN_ERROR, + loginError + } +} + + +function callLoginApi(email, password, callback) { + + Promise.resolve() + fetch('https://apollo.kevinmidboe.com/api/v1/user/login', { + method: 'POST', + headers: { + 'Content-type': 'application/json' + }, + body: JSON.stringify({ + username: email, + password: password, + }) + }) + .then(response => { + switch (response.status) { + case 200: + response.json() + .then((data) => { + if (data.success === true) { + let token = data.token; + setCookie('token', token, 10); + setCookie('logged_in', true, 10); + + window.location.reload(); + } + return callback(null); + }) + + case 401: + return callback(new Error(response.statusText)); + } + }) + .catch(error => { + return callback(new Error('Invalid email and password')); + }); +} + +export default function reducer(state = { + isLoginSuccess: false, + isLoginPending: false, + loginError: null +}, action) { + switch (action.type) { + case SET_LOGIN_PENDING: + return Object.assign({}, state, { + isLoginPending: action.isLoginPending + }); + + case SET_LOGIN_SUCCESS: + return Object.assign({}, state, { + isLoginSuccess: action.isLoginSuccess + }); + + case SET_LOGIN_ERROR: + return Object.assign({}, state, { + loginError: action.loginError + }); + + default: + return state; + } +} \ No newline at end of file diff --git a/client/app/components/redux/store.jsx b/client/app/components/redux/store.jsx new file mode 100644 index 0000000..6af9d92 --- /dev/null +++ b/client/app/components/redux/store.jsx @@ -0,0 +1,7 @@ +import { createStore, applyMiddleware } from 'redux'; +import thunk from 'redux-thunk'; +import logger from 'redux-logger'; +import reducer from './reducer.jsx'; + +const store = createStore(reducer, {}, applyMiddleware(thunk, logger)); +export default store; \ No newline at end of file