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