import Ember from 'ember';
import layout from '../../templates/components/nypr-accounts/resend-button';
import fetch from 'fetch';
import { task, timeout } from 'ember-concurrency';
import { rejectUnsuccessfulResponses } from 'nypr-account-settings/utils/fetch-utils';

export default Ember.Component.extend({
  layout,
  tagName: '',
  target: null,
  email: null,
  resendAction: null,
  ready: true,
  success: false,
  error: false,
  autoReset: true,
  resetDelay: 10000,
  successMessage: 'Email resent',
  errorMessage: 'Email not resent. Try again later',
  actions: {
    resend(target, email) {
      let resendAction = () => fetch(`${target}?email=${email}`, {method: 'GET', mode: 'cors'})
      .then(rejectUnsuccessfulResponses);
      this.get('tryResend').perform(resendAction);
    }
  },
  tryResend: task(function * (resendAction) {
    try {
      yield resendAction();
      this.set('ready', false);
      this.set('success', true);
    } catch(e) {
      this.set('ready', false);
      this.set('error', true);
    } finally {
      if (this.get('autoReset')) {
        yield timeout(this.get('resetDelay'));
        this.reset();
      }
    }
  }).drop(),
  reset() {
    this.set('success', false);
    this.set('error', false);
    this.set('ready', true);
  }
});

import Ember from 'ember';
import layout from '../../templates/components/nypr-accounts/resend-button';
import fetch from 'fetch';
import { task, timeout } from 'ember-concurrency';
import { rejectUnsuccessfulResponses } from 'nypr-account-settings/utils/fetch-utils';

export default Ember.Component.extend({
  layout,
  tagName: '',
  target: null,
  email: null,
  resendAction: null,
  ready: true,
  success: false,
  error: false,
  autoReset: true,
  resetDelay: Ember.testing ? 10 : 10000,
  successMessage: 'Email resent',
  errorMessage: 'Email not resent. Try again later',
  actions: {
    resend(target, email) {
      let resendAction = () => fetch(`${target}?email=${email}`, {method: 'GET', mode: 'cors'})
      .then(rejectUnsuccessfulResponses);
      this.get('tryResend').perform(resendAction);
    }
  },
  tryResend: task(function * (resendAction) {
    try {
      yield resendAction();
      this.set('ready', false);
      this.set('success', true);
    } catch(e) {
      this.set('ready', false);
      this.set('error', true);
    } finally {
      if (this.get('autoReset')) {
        yield timeout(this.get('resetDelay'));
        this.reset();
      }
    }
  }).drop(),
  reset() {
    this.set('success', false);
    this.set('error', false);
    this.set('ready', true);
  }
});

Tweak resend button for acceptance testing
Tweak resend button for acceptance testing
