// @flow import moment from 'moment'; import * as React from 'react'; import { Component, Text, View, App, Types } from 'reactxp'; import { Button, RedButton, GreenButton, Label } from './styled'; import { Layout, Container } from './Layout'; import styles from './AccountStyles'; import Img from './Img'; import { formatAccount } from '../lib/formatters'; import type { AccountToken } from '../lib/daemon-rpc'; export type AccountProps = { accountToken: AccountToken, accountExpiry: string, updateAccountExpiry: () => Promise, onLogout: () => void, onClose: () => void, onBuyMore: () => void, }; export type AccountState = { isRefreshingExpiry: boolean, }; export default class Account extends Component { state = { isRefreshingExpiry: false, }; _activationStateToken: ?Types.SubscriptionToken; _isMounted = false; componentDidMount() { this._isMounted = true; this._refreshAccountExpiry(); this._activationStateToken = App.activationStateChangedEvent.subscribe((activationState) => { if (activationState === Types.AppActivationState.Active) { this._refreshAccountExpiry(); } }); } componentWillUnmount() { this._isMounted = false; const activationStateToken = this._activationStateToken; if (activationStateToken) { activationStateToken.unsubscribe(); this._activationStateToken = null; } } render() { const expiry = moment(this.props.accountExpiry); const formattedAccountToken = formatAccount(this.props.accountToken || ''); const formattedExpiry = expiry.format('hA, D MMMM YYYY').toUpperCase(); const isOutOfTime = expiry.isSameOrBefore(moment()); return ( Account Account ID {formattedAccountToken} Paid until {isOutOfTime ? ( OUT OF TIME ) : ( {formattedExpiry} )} Log out ); } async _refreshAccountExpiry() { this.setState({ isRefreshingExpiry: true }); try { await this.props.updateAccountExpiry(); } catch (e) { // TODO: Report the error to user } if (this._isMounted) { this.setState({ isRefreshingExpiry: false }); } } }