diff --git a/src/actions.js b/src/actions.js index d909ca1..d8ca00c 100644 --- a/src/actions.js +++ b/src/actions.js @@ -9,125 +9,125 @@ const USER_DATA_SUCCESS = 'USER_DATA_SUCCESS'; const USER_DATA_FAILURE = 'USER_DATA_FAILURE'; function loginRequest() { - return { - type: LOGIN_REQUEST - } + return { + type: LOGIN_REQUEST + } } function loginSuccess(username, token) { - return { - type: LOGIN_SUCCESS, - username, - token - } + return { + type: LOGIN_SUCCESS, + username, + token + } } function loginFailure(loginError) { - return { - type: LOGIN_FAILURE, - loginError - } + return { + type: LOGIN_FAILURE, + loginError + } } function logout() { - return { - type: LOGOUT - } + return { + type: LOGOUT + } } function userDataRequest() { - return { - type: USER_DATA_REQUEST - } + return { + type: USER_DATA_REQUEST + } } function userDataSuccess(data) { - return { - type: USER_DATA_SUCCESS, - data - } + return { + type: USER_DATA_SUCCESS, + data + } } -function userDataFailure(errorMessage) { - return { - type: USER_DATA_FAILURE, - errorMessage - } +function userDataFailure(userError) { + return { + type: USER_DATA_FAILURE, + userError + } } export { - LOGIN_REQUEST, loginRequest, - LOGIN_SUCCESS, loginSuccess, - LOGIN_FAILURE, loginFailure, - LOGOUT, logout, - USER_DATA_REQUEST, userDataRequest, - USER_DATA_SUCCESS, userDataSuccess, - USER_DATA_FAILURE, userDataFailure, + LOGIN_REQUEST, loginRequest, + LOGIN_SUCCESS, loginSuccess, + LOGIN_FAILURE, loginFailure, + LOGOUT, logout, + USER_DATA_REQUEST, userDataRequest, + USER_DATA_SUCCESS, userDataSuccess, + USER_DATA_FAILURE, userDataFailure, }; export function requestLogin(username, password) { - return (dispatch) => { - // We are now logging in - dispatch(loginRequest()); - const apiURL = Config.DUSKEN_API_URL; - return fetch(`${apiURL}/auth/obtain-token/`, { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - 'username': username, - 'password': password - }) - }) - .then(response => response.json().then(json => ({ json, response }))) - .then(({json, response}) => { - if (response.ok === false) { - return Promise.reject(json) - } - return json - }) - .then( - data => { - dispatch(loginSuccess(username, data.token)) - }, - data => { - console.log(data); - dispatch(loginFailure(data || {'non_field_errors': ['Kunne ikke logge inn, prøv igjen']})) - } - ) - } + return (dispatch) => { + // We are now logging in + dispatch(loginRequest()); + const apiURL = Config.DUSKEN_API_URL; + return fetch(`${apiURL}/auth/obtain-token/`, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + 'username': username, + 'password': password + }) + }) + .then(response => response.json().then(json => ({ json, response }))) + .then(({json, response}) => { + if (response.ok === false) { + return Promise.reject(json) + } + return json + }) + .then( + data => { + dispatch(loginSuccess(username, data.token)) + }, + data => { + console.log(data); + dispatch(loginFailure(data || {'non_field_errors': ['Kunne ikke logge inn, prøv igjen']})) + } + ) + } } export function requestUserData(token) { - return (dispatch) => { - // We are now fetching user data - dispatch(userDataRequest()); + return (dispatch) => { + // We are now fetching user data + dispatch(userDataRequest()); - const apiURL = Config.DUSKEN_API_URL; - return fetch(`${apiURL}/api/me/`, { - method: 'GET', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - 'Authorization': 'Token ' + token - } - }) - .then(response => response.json().then(json => ({ json, response }))) - .then(({json, response}) => { - if (response.ok === false) { - return Promise.reject(json) - } - return json - }) - .then( - data => { - dispatch(userDataSuccess(data)) - }, - data => { - dispatch(userDataFailure('Det funka ikke :-(')) - } - ) - } + const apiURL = Config.DUSKEN_API_URL; + return fetch(`${apiURL}/api/me/`, { + method: 'GET', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Authorization': 'Token ' + token + } + }) + .then(response => response.json().then(json => ({ json, response }))) + .then(({json, response}) => { + if (response.ok === false) { + return Promise.reject(json) + } + return json + }) + .then( + data => { + dispatch(userDataSuccess(data)) + }, + data => { + dispatch(userDataFailure('Det funka ikke :-(')) + } + ) + } } diff --git a/src/reducers.js b/src/reducers.js index 9a7f84a..22b4cb2 100644 --- a/src/reducers.js +++ b/src/reducers.js @@ -1,64 +1,65 @@ import { - LOGIN_REQUEST, - LOGIN_SUCCESS, - LOGIN_FAILURE, - LOGOUT, - USER_DATA_REQUEST, - USER_DATA_SUCCESS, - USER_DATA_FAILURE, + LOGIN_REQUEST, + LOGIN_SUCCESS, + LOGIN_FAILURE, + LOGOUT, + USER_DATA_REQUEST, + USER_DATA_SUCCESS, + USER_DATA_FAILURE, } from './actions'; const initialState = { - isAuthenticated: false, - isLoggingIn: false, - isFetchingUserData: false, - userToken: null, - user: null, - loginError: null, + isAuthenticated: false, + isLoggingIn: false, + isFetchingUserData: false, + userToken: null, + user: null, + loginError: null, + userError: null, }; export default function duskenApp(state = initialState, action) { - switch (action.type) { - case LOGIN_REQUEST: - return Object.assign({}, state, { - isLoggingIn: true, - loginError: null, - }); + switch (action.type) { + case LOGIN_REQUEST: + return Object.assign({}, state, { + isLoggingIn: true, + loginError: null, + }); - case LOGIN_FAILURE: - return Object.assign({}, state, { - isLoggingIn: false, - loginError: action.loginError - }); + case LOGIN_FAILURE: + return Object.assign({}, state, { + isLoggingIn: false, + loginError: action.loginError + }); - case LOGIN_SUCCESS: - return Object.assign({}, state, { - isLoggingIn: false, - isAuthenticated: true, - userToken: action.token - }); + case LOGIN_SUCCESS: + return Object.assign({}, state, { + isLoggingIn: false, + isAuthenticated: true, + userToken: action.token + }); - case LOGOUT: - return initialState; + case LOGOUT: + return initialState; - case USER_DATA_REQUEST: - return Object.assign({}, state, { - isFetchingUserData: true - }); + case USER_DATA_REQUEST: + return Object.assign({}, state, { + isFetchingUserData: true + }); - case USER_DATA_FAILURE: - return Object.assign({}, state, { - isFetchingUserData: false, - errorMessage: action.errorMessage - }); + case USER_DATA_FAILURE: + return Object.assign({}, state, { + isFetchingUserData: false, + userError: action.userError + }); - case USER_DATA_SUCCESS: - return Object.assign({}, state, { - isFetchingUserData: false, - user: action.data - }); + case USER_DATA_SUCCESS: + return Object.assign({}, state, { + isFetchingUserData: false, + user: action.data + }); - default: - return state - } + default: + return state + } }