import * as React from 'react'; import { formatDate, hasExpired } from '../../shared/account-expiry'; import { AccountToken, IDevice } from '../../shared/daemon-rpc-types'; import { messages } from '../../shared/gettext'; import { AccountContainer, AccountFooter, AccountOutOfTime, AccountRow, AccountRowLabel, AccountRows, AccountRowValue, DeviceRowValue, StyledBuyCreditButton, StyledContainer, StyledRedeemVoucherButton, StyledSpinnerContainer, } from './AccountStyles'; import AccountTokenLabel from './AccountTokenLabel'; import * as AppButton from './AppButton'; import { AriaDescribed, AriaDescription, AriaDescriptionGroup } from './AriaGroup'; import ImageView from './ImageView'; import { BackAction } from './KeyboardNavigation'; import { Layout } from './Layout'; import { ModalAlert, ModalAlertType, ModalMessage } from './Modal'; import { NavigationBar, NavigationItems, TitleBarItem } from './NavigationBar'; import SettingsHeader, { HeaderTitle } from './SettingsHeader'; interface IProps { deviceName?: string; accountToken?: AccountToken; accountExpiry?: string; expiryLocale: string; isOffline: boolean; prepareLogout: () => void; cancelLogout: () => void; onLogout: () => void; onClose: () => void; onBuyMore: () => Promise; updateAccountData: () => void; getDevice: () => Promise; } interface IState { logoutDialogState: 'hidden' | 'checking-ports' | 'confirm'; } export default class Account extends React.Component { public state: IState = { logoutDialogState: 'hidden' }; public componentDidMount() { this.props.updateAccountData(); } public render() { return ( { // TRANSLATORS: Title label in navigation bar messages.pgettext('account-view', 'Account') } {messages.pgettext('account-view', 'Account')} {messages.pgettext('device-management', 'Device name')} {this.props.deviceName} {messages.pgettext('account-view', 'Account number')} {messages.pgettext('account-view', 'Paid until')} {messages.gettext('Buy more credit')} {messages.pgettext('account-view', 'Log out')} {this.renderLogoutDialog()} ); } private renderLogoutDialog() { const modalType = this.state.logoutDialogState === 'checking-ports' ? undefined : ModalAlertType.warning; const message = this.state.logoutDialogState === 'checking-ports' ? ( ) : ( { // TRANSLATORS: This is is a further explanation of what happens when logging out. messages.pgettext( 'device-management', 'The ports forwarded to this device will be deleted if you log out.', ) } ); const buttons = this.state.logoutDialogState === 'checking-ports' ? [] : [ { // TRANSLATORS: Confirmation button when logging out messages.pgettext('device-management', 'Log out anyway') } , {messages.gettext('Back')} , ]; return ( {message} ); } private onTryLogout = async () => { this.setState({ logoutDialogState: 'checking-ports' }); this.props.prepareLogout(); const device = await this.props.getDevice(); if (device === undefined) { this.onHideLogoutConfirmationDialog(); } else if (device.ports !== undefined && device.ports.length > 0) { this.setState({ logoutDialogState: 'confirm' }); } else { this.props.onLogout(); this.onHideLogoutConfirmationDialog(); } }; private cancelLogout = () => { this.props.cancelLogout(); this.onHideLogoutConfirmationDialog(); }; private onHideLogoutConfirmationDialog = () => { this.setState({ logoutDialogState: 'hidden' }); }; } function FormattedAccountExpiry(props: { expiry?: string; locale: string }) { if (props.expiry) { if (hasExpired(props.expiry)) { return ( {messages.pgettext('account-view', 'OUT OF TIME')} ); } else { return {formatDate(props.expiry, props.locale)}; } } else { return ( {messages.pgettext('account-view', 'Currently unavailable')} ); } }