2017-08-31 08:21:39 +08:00
import {AccountStore, Actions, IdentityStore} from 'nylas-exports';
2016-08-31 05:52:19 +08:00
import {ipcRenderer} from 'electron';
2016-05-28 05:05:27 +08:00
import NylasStore from 'nylas-store';
2016-10-18 08:59:33 +08:00
import OnboardingActions from './onboarding-actions';
2016-05-28 05:05:27 +08:00
function accountTypeForProvider(provider) {
if (provider === 'eas') {
return 'exchange';
}
if (provider === 'custom') {
return 'imap';
}
return provider;
}
class OnboardingStore extends NylasStore {
constructor() {
super();
this.listenTo(OnboardingActions.moveToPreviousPage, this._onMoveToPreviousPage)
this.listenTo(OnboardingActions.moveToPage, this._onMoveToPage)
this.listenTo(OnboardingActions.accountJSONReceived, this._onAccountJSONReceived)
2017-08-01 11:20:01 +08:00
this.listenTo(OnboardingActions.identityJSONReceived, this._onIdentityJSONReceived)
2016-05-28 05:05:27 +08:00
this.listenTo(OnboardingActions.setAccountInfo, this._onSetAccountInfo);
this.listenTo(OnboardingActions.setAccountType, this._onSetAccountType);
2017-01-26 07:06:47 +08:00
ipcRenderer.on('set-account-type', (e, type) => {
if (type) {
this._onSetAccountType(type)
} else {
this._pageStack = ['account-choose']
this.trigger()
}
})
2016-05-28 05:05:27 +08:00
2017-01-12 05:54:30 +08:00
const {existingAccount, addingAccount, accountType} = NylasEnv.getWindowProps();
2016-05-28 05:05:27 +08:00
2016-10-21 07:34:59 +08:00
const hasAccounts = (AccountStore.accounts().length > 0)
2016-05-28 05:05:27 +08:00
const identity = IdentityStore.identity();
2016-10-21 07:34:59 +08:00
2016-05-28 05:05:27 +08:00
if (identity) {
this._accountInfo = {
2017-08-31 06:24:30 +08:00
name: `${identity.firstName || ""} ${identity.lastName || ""}`,
2016-05-28 05:05:27 +08:00
};
} else {
this._accountInfo = {};
}
if (existingAccount) {
2016-07-12 09:07:45 +08:00
// Used when re-adding an account after re-connecting
2017-01-12 05:54:30 +08:00
const existingAccountType = accountTypeForProvider(existingAccount.provider);
2016-05-28 05:05:27 +08:00
this._pageStack = ['account-choose']
this._accountInfo = {
name: existingAccount.name,
email: existingAccount.emailAddress,
};
2017-01-12 05:54:30 +08:00
this._onSetAccountType(existingAccountType);
2016-05-28 05:05:27 +08:00
} else if (addingAccount) {
2016-07-12 09:07:45 +08:00
// Adding a new, unknown account
this._pageStack = ['account-choose'];
2017-01-12 05:54:30 +08:00
if (accountType) {
this._onSetAccountType(accountType);
}
2016-07-12 09:07:45 +08:00
} else if (identity) {
// Should only happen if config was edited to remove all accounts,
// but don't want to re-login to Nylas account. Very useful when
// switching environments.
2016-05-28 05:05:27 +08:00
this._pageStack = ['account-choose'];
2016-10-21 07:34:59 +08:00
} else if (hasAccounts) {
// Should only happen when the user has "signed out" of their Nylas ID,
// but already has accounts synced. Or is upgrading from a very old build.
// We used to show "Welcome Back", but now just jump to sign in.
this._pageStack = ['authenticate'];
2016-05-28 05:05:27 +08:00
} else {
2016-07-12 09:07:45 +08:00
// Standard new user onboarding flow.
2016-05-28 05:05:27 +08:00
this._pageStack = ['welcome'];
}
}
_onOnboardingComplete = () => {
// When account JSON is received, we want to notify external services
// that it succeeded. Unfortunately in this case we're likely to
// close the window before those requests can be made. We add a short
// delay here to ensure that any pending requests have a chance to
// clear before the window closes.
setTimeout(() => {
ipcRenderer.send('account-setup-successful');
}, 100);
}
_onSetAccountType = (type) => {
let nextPage = "account-settings";
if (type === 'gmail') {
nextPage = "account-settings-gmail";
} else if (type === 'exchange') {
nextPage = "account-settings-exchange";
}
2016-10-08 01:16:20 +08:00
Actions.recordUserEvent('Selected Account Type', {
provider: type,
});
2017-01-11 07:03:09 +08:00
// Don't carry over any type-specific account information
const {email, name, password} = this._accountInfo;
this._onSetAccountInfo({email, name, password, type});
2016-05-28 05:05:27 +08:00
this._onMoveToPage(nextPage);
}
_onSetAccountInfo = (info) => {
this._accountInfo = info;
this.trigger();
}
_onMoveToPreviousPage = () => {
this._pageStack.pop();
this.trigger();
}
_onMoveToPage = (page) => {
this._pageStack.push(page)
this.trigger();
}
2017-08-01 11:20:01 +08:00
_onIdentityJSONReceived = async (json) => {
2016-05-28 05:05:27 +08:00
const isFirstAccount = AccountStore.accounts().length === 0;
feat(usage): Add a FeatureUsageStore and move Identity to the DB
Summary:
This is a WIP
Depends on D3799 on billing.nylas.com
This adds a `FeatureUsageStore` which determines whether a feature can be
used or not. It also allows us to record "using" a feature.
Feature Usage is ultimately backed by the Nylas Identity and cached
locally in the Identity object. Since feature usage is attached to the
Nylas Identity, we move the whole Identity object (except for the ID) to
the database.
This includes a migration (with tests!) to move the Nylas Identity from
the config into the Database. We still, however, need the Nylas ID to stay
in the config so it can be synchronously accessed by the /browser process
on bootup when determining what windows to show. It's also convenient to
know what the Nylas ID is by looking at the config. There's logic (with
tests!) to make sure these stay in sync. If you delete the Nylas ID from
the config, it'll be the same as logging you out.
The schema for the feature usage can be found in more detail on D3799. By
the time it reaches Nylas Mail, the Nylas ID object has a `feature_usage`
attribute that has each feature (keyed by the feature name) and
information about the plans attached to it. The schema Nylas Mail sees
looks like:
```
"feature_usage": {
"snooze": {
quota: 10,
peroid: 'monthly',
used_in_period: 8,
feature_limit_name: 'Snooze Group A',
},
}
```
See D3799 for more info about how these are generated.
One final change that's in here is how Stores are loaded. Most of our
core stores are loaded at require time, but now things like the
IdentityStore need to do asynchronous things on activation. In reality
most of our stores do this and it's a miracle it hasn't caused more
problems! Now when stores activate we optionally look for an `activate`
method and `await` for it. This was necessary so downstream classes (like
the Onboarding Store), see a fully initialized IdentityStore by the time
it's time to use them
Test Plan: New tests!
Reviewers: khamidou, juan, halla
Reviewed By: juan
Differential Revision: https://phab.nylas.com/D3808
2017-02-04 07:31:31 +08:00
await IdentityStore.saveIdentity(json);
2016-05-28 05:05:27 +08:00
setTimeout(() => {
if (isFirstAccount) {
this._onSetAccountInfo(Object.assign({}, this._accountInfo, {
2017-08-01 11:20:01 +08:00
name: `${json.firstName || ""} ${json.lastName || ""}`,
email: json.emailAddress,
2016-05-28 05:05:27 +08:00
}));
OnboardingActions.moveToPage('account-choose');
} else {
this._onOnboardingComplete();
}
}, 1000);
}
2017-09-11 03:04:22 +08:00
_onAccountJSONReceived = async (json) => {
2016-05-28 05:05:27 +08:00
try {
const isFirstAccount = AccountStore.accounts().length === 0;
2017-09-11 03:04:22 +08:00
AccountStore.addAccountFromJSON(json);
2016-05-28 05:05:27 +08:00
2016-06-08 03:53:05 +08:00
Actions.recordUserEvent('Email Account Auth Succeeded', {
2017-08-09 01:00:07 +08:00
provider: json.provider,
2016-05-28 05:05:27 +08:00
});
2017-08-09 01:00:07 +08:00
2016-05-28 05:05:27 +08:00
ipcRenderer.send('new-account-added');
NylasEnv.displayWindow();
if (isFirstAccount) {
this._onMoveToPage('initial-preferences');
2016-06-08 03:53:05 +08:00
Actions.recordUserEvent('First Account Linked', {
2017-08-09 01:00:07 +08:00
provider: json.provider,
2016-06-08 03:53:05 +08:00
});
2016-05-28 05:05:27 +08:00
} else {
2017-08-31 08:21:39 +08:00
// let them see the "success" screen for a moment
// before the window is closed.
setTimeout(() => {
this._onOnboardingComplete();
}, 2000);
2016-05-28 05:05:27 +08:00
}
} catch (e) {
NylasEnv.reportError(e);
2017-09-06 04:37:40 +08:00
NylasEnv.showErrorDialog("Unable to Connect Account", "Sorry, something went wrong on the Nylas server. Please try again. If you're still having issues, contact us at support@getmailspring.com.");
2016-05-28 05:05:27 +08:00
}
}
page() {
return this._pageStack[this._pageStack.length - 1];
}
pageDepth() {
return this._pageStack.length;
}
accountInfo() {
return this._accountInfo;
}
}
export default new OnboardingStore();