/* @flow */
import React, { PureComponent } from 'react';
import { StyleSheet } from 'react-native';

import type { Actions, Auth } from '../types';
import connectWithActions from '../connectWithActions';
import { ZulipButton } from '../common';
import { unregisterPush } from '../api';
import { getAuth, getAccounts, getPushToken } from '../selectors';
import { logErrorRemotely } from '../utils/logging';

const styles = StyleSheet.create({
  logoutButton: {
    flex: 1,
    margin: 8,
  },
});

type Props = {
  accounts: any[],
  auth: Auth,
  pushToken: string,
  actions: Actions,
};

class LogoutButton extends PureComponent<Props> {
  props: Props;

  shutdownPUSH = async () => {
    const { auth, actions, pushToken } = this.props;
    if (pushToken !== '') {
      try {
        await unregisterPush(auth, pushToken);
      } catch (e) {
        logErrorRemotely(e, 'failed to unregister Push token');
      }
      actions.deleteTokenPush();
    }
  };

  logout = () => {
    const { accounts, actions } = this.props;
    this.shutdownPUSH();
    actions.logout(accounts);
  };

  render() {
    return (
      <ZulipButton style={styles.logoutButton} secondary text="Logout" onPress={this.logout} />
    );
  }
}

export default connectWithActions(state => ({
  auth: getAuth(state),
  accounts: getAccounts(state),
  pushToken: getPushToken(state),
}))(LogoutButton);

/* @flow */
import React, { PureComponent } from 'react';
import { StyleSheet } from 'react-native';

import type { Actions, Auth } from '../types';
import connectWithActions from '../connectWithActions';
import { ZulipButton } from '../common';
import { unregisterPush } from '../api';
import { getAuth, getPushToken } from '../selectors';
import { logErrorRemotely } from '../utils/logging';

const styles = StyleSheet.create({
  logoutButton: {
    flex: 1,
    margin: 8,
  },
});

type Props = {
  auth: Auth,
  pushToken: string,
  actions: Actions,
};

class LogoutButton extends PureComponent<Props> {
  props: Props;

  shutdownPUSH = async () => {
    const { auth, actions, pushToken } = this.props;
    if (pushToken !== '') {
      try {
        await unregisterPush(auth, pushToken);
      } catch (e) {
        logErrorRemotely(e, 'failed to unregister Push token');
      }
      actions.deleteTokenPush();
    }
  };

  logout = () => {
    const { actions } = this.props;
    this.shutdownPUSH();
    actions.logout();
  };

  render() {
    return (
      <ZulipButton style={styles.logoutButton} secondary text="Logout" onPress={this.logout} />
    );
  }
}

export default connectWithActions(state => ({
  auth: getAuth(state),
  pushToken: getPushToken(state),
}))(LogoutButton);

Clean up logout method call.
logout: Clean up logout method call.

This was a silent type error -- we were passing an argument to a
function that doesn't expect any.  Make the code less confused.

[greg: revised commit message]
