import React from 'react';
import {Actions, IdentityStore} from 'nylas-exports';
import {RetinaImg} from 'nylas-component-kit';
import {shell} from 'electron';
class OpenIdentityPageButton extends React.Component {
static propTypes = {
path: React.PropTypes.string,
label: React.PropTypes.string,
source: React.PropTypes.string,
campaign: React.PropTypes.string,
img: React.PropTypes.string,
}
constructor(props) {
super(props);
this.state = {
loading: false,
};
}
_onClick = () => {
this.setState({loading: true});
IdentityStore.fetchSingleSignOnURL(this.props.path, {
source: this.props.source,
campaign: this.props.campaign,
content: this.props.label,
}).then((url) => {
this.setState({loading: false});
shell.openExternal(url);
});
}
render() {
if (this.state.loading) {
return (
{this.props.label}…
);
}
if (this.props.img) {
return (
{this.props.label}
)
}
return (
{this.props.label}
);
}
}
class PreferencesIdentity extends React.Component {
static displayName = 'PreferencesIdentity';
constructor() {
super();
this.state = this.getStateFromStores();
this.state.refreshing = false;
}
componentDidMount() {
this.unsubscribe = IdentityStore.listen(() => {
this.setState(this.getStateFromStores());
});
}
componentWillUnmount() {
this.unsubscribe();
}
getStateFromStores() {
return {
identity: IdentityStore.identity() || {},
subscriptionState: IdentityStore.subscriptionState(),
daysUntilSubscriptionRequired: IdentityStore.daysUntilSubscriptionRequired(),
};
}
_onRefresh = () => {
this.setState({refreshing: true});
IdentityStore.refreshIdentityAndAccounts().finally(() => {
this.setState({refreshing: false});
});
}
_renderPaymentRow() {
const {identity, daysUntilSubscriptionRequired, subscriptionState} = this.state
if (subscriptionState === IdentityStore.State.Trialing) {
let msg = "You have not upgraded to Nylas Pro.";
if (daysUntilSubscriptionRequired > 1) {
msg = `There are ${daysUntilSubscriptionRequired} days remaining in your trial of Nylas Pro.`;
} else if (daysUntilSubscriptionRequired === 1) {
msg = `There is one day remaining in your trial of Nylas Pro. Subscribe today!`;
}
return (
)
}
if (subscriptionState === IdentityStore.State.Lapsed) {
return (
Your subscription has been canceled or your billing information has expired.
We've paused your mailboxes! Renew your subscription to continue using N1.
)
}
return (
Your subscription is valid until {new Date(identity.valid_until * 1000).toLocaleDateString()}. Enjoy N1!
)
}
render() {
const {identity, refreshing} = this.state;
const {firstname, lastname, email} = identity;
let refresh = null;
if (refreshing) {
refresh = (
Refreshing...
)
} else {
refresh = (
Refresh
)
}
return (
Nylas ID:
{refresh}
{firstname} {lastname}
{email}
Actions.logoutNylasIdentity()}>Sign Out
{this._renderPaymentRow()}
);
}
}
export default PreferencesIdentity;