2017-06-07 20:56:52 -04:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
import { getLocal } from "utils";
|
2017-06-01 12:20:12 -04:00
|
|
|
|
2017-06-07 20:56:52 -04:00
|
|
|
const reducers = {};
|
2017-06-01 12:20:12 -04:00
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
authenticationIsPending: false,
|
2017-06-08 17:15:34 -04:00
|
|
|
userIsPending: false,
|
2017-06-01 20:51:52 -04:00
|
|
|
emailNewIsPending: false,
|
2017-06-07 20:56:52 -04:00
|
|
|
emailNewErrorMessage: "",
|
|
|
|
emailNewDeclined: getLocal("user_email_declined", false),
|
|
|
|
emailToVerify: "",
|
|
|
|
user: undefined,
|
|
|
|
};
|
2017-06-01 12:20:12 -04:00
|
|
|
|
|
|
|
reducers[types.AUTHENTICATION_STARTED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
2017-06-07 20:56:52 -04:00
|
|
|
authenticationIsPending: true,
|
2017-06-08 17:15:34 -04:00
|
|
|
userIsPending: true,
|
|
|
|
user: defaultState.user,
|
2017-06-07 20:56:52 -04:00
|
|
|
});
|
|
|
|
};
|
2017-06-01 12:20:12 -04:00
|
|
|
|
|
|
|
reducers[types.AUTHENTICATION_SUCCESS] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
authenticationIsPending: false,
|
2017-06-08 17:15:34 -04:00
|
|
|
userIsPending: false,
|
2017-06-01 12:20:12 -04:00
|
|
|
user: action.data.user,
|
2017-06-07 20:56:52 -04:00
|
|
|
});
|
|
|
|
};
|
2017-06-01 12:20:12 -04:00
|
|
|
|
|
|
|
reducers[types.AUTHENTICATION_FAILURE] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
authenticationIsPending: false,
|
2017-06-08 17:15:34 -04:00
|
|
|
userIsPending: false,
|
|
|
|
user: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reducers[types.USER_FETCH_STARTED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
userIsPending: true,
|
|
|
|
user: defaultState.user,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reducers[types.USER_FETCH_SUCCESS] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
userIsPending: false,
|
|
|
|
user: action.data.user,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reducers[types.USER_FETCH_FAILURE] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
userIsPending: true,
|
2017-06-01 12:20:12 -04:00
|
|
|
user: null,
|
2017-06-07 20:56:52 -04:00
|
|
|
});
|
|
|
|
};
|
2017-06-01 12:20:12 -04:00
|
|
|
|
2017-06-01 20:51:52 -04:00
|
|
|
reducers[types.USER_EMAIL_DECLINE] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
2017-06-07 20:56:52 -04:00
|
|
|
emailNewDeclined: true,
|
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
|
|
|
reducers[types.USER_EMAIL_NEW_STARTED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
emailNewIsPending: true,
|
2017-06-07 20:56:52 -04:00
|
|
|
emailNewErrorMessage: "",
|
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
|
|
|
reducers[types.USER_EMAIL_NEW_SUCCESS] = function(state, action) {
|
2017-06-07 20:56:52 -04:00
|
|
|
let user = Object.assign({}, state.user);
|
|
|
|
user.has_email = true;
|
2017-06-01 20:51:52 -04:00
|
|
|
return Object.assign({}, state, {
|
2017-06-07 20:56:52 -04:00
|
|
|
emailToVerify: action.data.email,
|
2017-06-01 20:51:52 -04:00
|
|
|
emailNewIsPending: false,
|
2017-06-07 20:56:52 -04:00
|
|
|
user: user,
|
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
|
|
|
reducers[types.USER_EMAIL_NEW_EXISTS] = function(state, action) {
|
2017-06-07 20:56:52 -04:00
|
|
|
let user = Object.assign({}, state.user);
|
2017-06-01 20:51:52 -04:00
|
|
|
return Object.assign({}, state, {
|
2017-06-07 20:56:52 -04:00
|
|
|
emailToVerify: action.data.email,
|
2017-06-01 20:51:52 -04:00
|
|
|
emailNewIsPending: false,
|
2017-06-07 20:56:52 -04:00
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
|
|
|
reducers[types.USER_EMAIL_NEW_FAILURE] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
emailNewIsPending: false,
|
2017-06-07 20:56:52 -04:00
|
|
|
emailNewErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
2017-06-02 19:09:52 -04:00
|
|
|
reducers[types.USER_EMAIL_VERIFY_STARTED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
emailVerifyIsPending: true,
|
2017-06-07 20:56:52 -04:00
|
|
|
emailVerifyErrorMessage: "",
|
|
|
|
});
|
|
|
|
};
|
2017-06-02 19:09:52 -04:00
|
|
|
|
|
|
|
reducers[types.USER_EMAIL_VERIFY_SUCCESS] = function(state, action) {
|
2017-06-07 20:56:52 -04:00
|
|
|
let user = Object.assign({}, state.user);
|
|
|
|
user.has_email = true;
|
2017-06-02 19:09:52 -04:00
|
|
|
return Object.assign({}, state, {
|
2017-06-07 20:56:52 -04:00
|
|
|
emailToVerify: "",
|
2017-06-02 19:09:52 -04:00
|
|
|
emailVerifyIsPending: false,
|
2017-06-07 20:56:52 -04:00
|
|
|
user: user,
|
|
|
|
});
|
|
|
|
};
|
2017-06-02 19:09:52 -04:00
|
|
|
|
|
|
|
reducers[types.USER_EMAIL_VERIFY_FAILURE] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
emailVerifyIsPending: false,
|
2017-06-07 20:56:52 -04:00
|
|
|
emailVerifyErrorMessage: action.data.error,
|
|
|
|
});
|
|
|
|
};
|
2017-06-01 20:51:52 -04:00
|
|
|
|
2017-06-01 12:20:12 -04:00
|
|
|
export default function reducer(state = defaultState, action) {
|
|
|
|
const handler = reducers[action.type];
|
|
|
|
if (handler) return handler(state, action);
|
|
|
|
return state;
|
|
|
|
}
|