lbry-desktop/ui/js/actions/user.js

235 lines
5.6 KiB
JavaScript
Raw Normal View History

2017-06-07 20:56:52 -04:00
import * as types from "constants/action_types";
2017-08-02 18:55:58 -04:00
import * as modals from "constants/modal_types";
2017-06-07 20:56:52 -04:00
import lbryio from "lbryio";
2017-08-17 23:31:44 -04:00
import { doOpenModal, doShowSnackBar } from "actions/app";
2017-07-24 18:59:26 -04:00
import { doRewardList, doClaimRewardType } from "actions/rewards";
2017-07-18 19:00:13 -04:00
import { selectEmailToVerify, selectUser } from "selectors/user";
2017-07-24 18:59:26 -04:00
import rewards from "rewards";
2017-06-01 12:20:12 -04:00
export function doAuthenticate() {
return function(dispatch, getState) {
dispatch({
type: types.AUTHENTICATION_STARTED,
2017-06-07 20:56:52 -04:00
});
lbryio
.authenticate()
.then(user => {
dispatch({
type: types.AUTHENTICATION_SUCCESS,
data: { user },
});
dispatch(doRewardList());
2017-08-20 17:42:00 -04:00
dispatch(doFetchInviteStatus());
2017-06-01 12:20:12 -04:00
})
2017-06-07 20:56:52 -04:00
.catch(error => {
2017-08-02 18:55:58 -04:00
dispatch(doOpenModal(modals.AUTHENTICATION_FAILURE));
2017-06-07 20:56:52 -04:00
dispatch({
type: types.AUTHENTICATION_FAILURE,
data: { error },
});
});
};
}
2017-06-08 17:15:34 -04:00
export function doUserFetch() {
return function(dispatch, getState) {
dispatch({
type: types.USER_FETCH_STARTED,
});
lbryio
.getCurrentUser()
.then(user => {
2017-06-21 20:09:30 -04:00
dispatch(doRewardList());
2017-06-08 17:15:34 -04:00
dispatch({
type: types.USER_FETCH_SUCCESS,
data: { user },
});
})
.catch(error => {
2017-06-08 17:15:34 -04:00
dispatch({
type: types.USER_FETCH_FAILURE,
data: { error },
});
});
2017-06-08 17:15:34 -04:00
};
}
export function doUserEmailNew(email) {
return function(dispatch, getState) {
dispatch({
type: types.USER_EMAIL_NEW_STARTED,
2017-06-07 20:56:52 -04:00
email: email,
});
lbryio
2017-06-21 08:42:22 -04:00
.call(
"user_email",
"new",
{ email: email, send_verification_email: true },
"post"
)
.catch(error => {
if (error.xhr && error.xhr.status == 409) {
2017-06-21 08:42:22 -04:00
return lbryio.call(
"user_email",
"resend_token",
{ email: email, only_if_expired: true },
"post"
);
}
2017-06-21 08:42:22 -04:00
throw error;
})
.then(() => {
dispatch({
2017-06-07 20:56:52 -04:00
type: types.USER_EMAIL_NEW_SUCCESS,
data: { email },
});
2017-06-08 17:15:34 -04:00
dispatch(doUserFetch());
})
.catch(error => {
dispatch({
type: types.USER_EMAIL_NEW_FAILURE,
2017-06-23 13:47:07 -04:00
data: { error },
});
});
2017-06-07 20:56:52 -04:00
};
}
2017-06-07 20:56:52 -04:00
export function doUserEmailVerify(verificationToken) {
2017-06-02 19:09:52 -04:00
return function(dispatch, getState) {
2017-06-07 20:56:52 -04:00
const email = selectEmailToVerify(getState());
2017-06-23 13:47:07 -04:00
verificationToken = verificationToken.toString().trim();
2017-06-07 20:56:52 -04:00
2017-06-02 19:09:52 -04:00
dispatch({
type: types.USER_EMAIL_VERIFY_STARTED,
2017-06-07 20:56:52 -04:00
code: verificationToken,
});
2017-06-02 19:09:52 -04:00
2017-06-07 20:56:52 -04:00
lbryio
2017-06-21 08:42:22 -04:00
.call(
"user_email",
"confirm",
{ verification_token: verificationToken, email: email },
"post"
)
2017-06-07 20:56:52 -04:00
.then(userEmail => {
if (userEmail.is_verified) {
dispatch({
type: types.USER_EMAIL_VERIFY_SUCCESS,
data: { email },
});
2017-06-08 17:15:34 -04:00
dispatch(doUserFetch());
2017-06-07 20:56:52 -04:00
} else {
2017-06-23 13:47:07 -04:00
throw new Error("Your email is still not verified."); //shouldn't happen
2017-06-07 20:56:52 -04:00
}
2017-06-23 13:47:07 -04:00
})
.catch(error => {
dispatch({
type: types.USER_EMAIL_VERIFY_FAILURE,
data: { error },
});
});
2017-06-07 20:56:52 -04:00
};
}
2017-07-18 19:00:13 -04:00
export function doUserIdentityVerify(stripeToken) {
return function(dispatch, getState) {
dispatch({
type: types.USER_IDENTITY_VERIFY_STARTED,
token: stripeToken,
});
lbryio
.call("user", "verify_identity", { stripe_token: stripeToken }, "post")
.then(user => {
if (user.is_identity_verified) {
dispatch({
type: types.USER_IDENTITY_VERIFY_SUCCESS,
data: { user },
});
2017-07-24 18:59:26 -04:00
dispatch(doClaimRewardType(rewards.TYPE_NEW_USER));
2017-07-18 19:00:13 -04:00
} else {
throw new Error(
"Your identity is still not verified. This should not happen."
); //shouldn't happen
}
})
.catch(error => {
dispatch({
type: types.USER_IDENTITY_VERIFY_FAILURE,
data: { error: error.toString() },
});
2017-07-18 19:00:13 -04:00
});
};
}
2017-07-21 17:45:37 -04:00
2017-07-21 13:23:39 +07:00
export function doFetchAccessToken() {
return function(dispatch, getState) {
const success = token =>
dispatch({
type: types.FETCH_ACCESS_TOKEN_SUCCESS,
data: { token },
});
lbryio.getAuthToken().then(success);
};
}
2017-08-17 23:31:44 -04:00
export function doFetchInviteStatus() {
return function(dispatch, getState) {
dispatch({
type: types.USER_INVITE_STATUS_FETCH_STARTED,
});
lbryio
.call("user", "invite_status")
.then(status => {
dispatch({
type: types.USER_INVITE_STATUS_FETCH_SUCCESS,
data: {
2017-08-25 15:51:54 -04:00
invitesRemaining: status.invites_remaining
? status.invites_remaining
: 0,
2017-08-17 23:31:44 -04:00
invitees: status.invitees,
},
});
})
.catch(error => {
dispatch({
type: types.USER_INVITE_STATUS_FETCH_FAILURE,
data: { error },
});
});
};
}
export function doUserInviteNew(email) {
return function(dispatch, getState) {
dispatch({
type: types.USER_INVITE_NEW_STARTED,
});
lbryio
.call("user", "invite", { email: email }, "post")
.then(invite => {
dispatch({
type: types.USER_INVITE_NEW_SUCCESS,
data: { email },
});
dispatch(
doShowSnackBar({
message: __("Invite sent to %s", email),
})
);
dispatch(doFetchInviteStatus());
})
.catch(error => {
dispatch({
type: types.USER_INVITE_NEW_FAILURE,
data: { error },
});
});
};
}