lbry-desktop/ui/js/redux/reducers/user.js

198 lines
5 KiB
JavaScript
Raw Normal View History

2017-06-08 02:56:52 +02:00
import * as types from "constants/action_types";
2017-06-01 18:20:12 +02:00
2017-06-08 02:56:52 +02:00
const reducers = {};
2017-06-01 18:20:12 +02:00
const defaultState = {
authenticationIsPending: false,
2017-06-08 23:15:34 +02:00
userIsPending: false,
emailNewIsPending: false,
2017-06-08 02:56:52 +02:00
emailNewErrorMessage: "",
emailToVerify: "",
2017-08-18 05:31:44 +02:00
inviteNewErrorMessage: "",
inviteNewIsPending: false,
inviteStatusIsPending: false,
invitesRemaining: undefined,
invitees: undefined,
2017-06-08 02:56:52 +02:00
user: undefined,
};
2017-06-01 18:20:12 +02:00
reducers[types.AUTHENTICATION_STARTED] = function(state, action) {
return Object.assign({}, state, {
2017-06-08 02:56:52 +02:00
authenticationIsPending: true,
2017-06-08 23:15:34 +02:00
userIsPending: true,
user: defaultState.user,
2017-06-08 02:56:52 +02:00
});
};
2017-06-01 18:20:12 +02:00
reducers[types.AUTHENTICATION_SUCCESS] = function(state, action) {
return Object.assign({}, state, {
authenticationIsPending: false,
2017-06-08 23:15:34 +02:00
userIsPending: false,
2017-06-01 18:20:12 +02:00
user: action.data.user,
2017-06-08 02:56:52 +02:00
});
};
2017-06-01 18:20:12 +02:00
reducers[types.AUTHENTICATION_FAILURE] = function(state, action) {
return Object.assign({}, state, {
authenticationIsPending: false,
2017-06-08 23:15:34 +02: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 18:20:12 +02:00
user: null,
2017-06-08 02:56:52 +02:00
});
};
2017-06-01 18:20:12 +02:00
reducers[types.USER_EMAIL_NEW_STARTED] = function(state, action) {
return Object.assign({}, state, {
emailNewIsPending: true,
2017-06-08 02:56:52 +02:00
emailNewErrorMessage: "",
});
};
reducers[types.USER_EMAIL_NEW_SUCCESS] = function(state, action) {
2017-06-08 02:56:52 +02:00
let user = Object.assign({}, state.user);
2017-07-20 21:03:01 +02:00
user.primary_email = action.data.email;
return Object.assign({}, state, {
2017-06-08 02:56:52 +02:00
emailToVerify: action.data.email,
emailNewIsPending: false,
2017-06-08 02:56:52 +02:00
user: user,
});
};
reducers[types.USER_EMAIL_NEW_EXISTS] = function(state, action) {
2017-06-08 02:56:52 +02:00
let user = Object.assign({}, state.user);
return Object.assign({}, state, {
2017-06-08 02:56:52 +02:00
emailToVerify: action.data.email,
emailNewIsPending: false,
2017-06-08 02:56:52 +02:00
});
};
reducers[types.USER_EMAIL_NEW_FAILURE] = function(state, action) {
return Object.assign({}, state, {
emailNewIsPending: false,
2017-06-08 02:56:52 +02:00
emailNewErrorMessage: action.data.error,
});
};
2017-06-03 01:09:52 +02:00
reducers[types.USER_EMAIL_VERIFY_STARTED] = function(state, action) {
return Object.assign({}, state, {
emailVerifyIsPending: true,
2017-06-08 02:56:52 +02:00
emailVerifyErrorMessage: "",
});
};
2017-06-03 01:09:52 +02:00
reducers[types.USER_EMAIL_VERIFY_SUCCESS] = function(state, action) {
2017-06-08 02:56:52 +02:00
let user = Object.assign({}, state.user);
2017-07-20 21:03:01 +02:00
user.primary_email = action.data.email;
2017-06-03 01:09:52 +02:00
return Object.assign({}, state, {
2017-06-08 02:56:52 +02:00
emailToVerify: "",
2017-06-03 01:09:52 +02:00
emailVerifyIsPending: false,
2017-06-08 02:56:52 +02:00
user: user,
});
};
2017-06-03 01:09:52 +02:00
reducers[types.USER_EMAIL_VERIFY_FAILURE] = function(state, action) {
return Object.assign({}, state, {
emailVerifyIsPending: false,
2017-06-08 02:56:52 +02:00
emailVerifyErrorMessage: action.data.error,
});
};
2017-07-19 01:00:13 +02:00
reducers[types.USER_IDENTITY_VERIFY_STARTED] = function(state, action) {
return Object.assign({}, state, {
identityVerifyIsPending: true,
identityVerifyErrorMessage: "",
});
};
reducers[types.USER_IDENTITY_VERIFY_SUCCESS] = function(state, action) {
return Object.assign({}, state, {
identityVerifyIsPending: false,
identityVerifyErrorMessage: "",
user: action.data.user,
});
};
reducers[types.USER_IDENTITY_VERIFY_FAILURE] = function(state, action) {
return Object.assign({}, state, {
identityVerifyIsPending: false,
identityVerifyErrorMessage: action.data.error,
});
};
2017-07-21 08:23:39 +02:00
reducers[types.FETCH_ACCESS_TOKEN_SUCCESS] = function(state, action) {
const { token } = action.data;
return Object.assign({}, state, {
accessToken: token,
});
};
2017-08-18 05:31:44 +02:00
reducers[types.USER_INVITE_STATUS_FETCH_STARTED] = function(state, action) {
return Object.assign({}, state, {
inviteStatusIsPending: true,
});
};
reducers[types.USER_INVITE_STATUS_FETCH_SUCCESS] = function(state, action) {
return Object.assign({}, state, {
inviteStatusIsPending: false,
invitesRemaining: action.data.invitesRemaining,
invitees: action.data.invitees,
});
};
reducers[types.USER_INVITE_NEW_STARTED] = function(state, action) {
return Object.assign({}, state, {
inviteNewIsPending: true,
inviteNewErrorMessage: "",
});
};
reducers[types.USER_INVITE_NEW_SUCCESS] = function(state, action) {
return Object.assign({}, state, {
inviteNewIsPending: false,
inviteNewErrorMessage: "",
});
};
reducers[types.USER_INVITE_NEW_FAILURE] = function(state, action) {
return Object.assign({}, state, {
inviteNewIsPending: false,
inviteNewErrorMessage: action.data.error.message,
});
};
reducers[types.USER_INVITE_STATUS_FETCH_FAILURE] = function(state, action) {
return Object.assign({}, state, {
inviteStatusIsPending: false,
invitesRemaining: null,
invitees: null,
});
};
2017-06-01 18:20:12 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}