<commit_before>import { read, getEntity, swap, updateEntity } from '../store/index';
import { closeLock } from '../lock/actions';
import webApi from '../lock/web_api';
import * as c from '../cred/index';
import * as cc from '../cred/country_codes';
import * as l from '../lock/index';
import * as m from './index';

export function changePhoneNumber(id, phoneNumber) {
  swap(updateEntity, "lock", id, c.setPhoneNumber, phoneNumber);
}

export function changePhoneLocation(id, location) {
  swap(updateEntity, "lock", id, lock => {
    lock = m.setSelectingLocation(lock, false);
    lock = c.setPhoneLocation(lock, location);
    return lock;
  });
}

export function setDefaultLocation(id, str) {
  const result = cc.find(str);
  if (!result) {
    throw new Error(`Unable to set the default location, can't find any country with the code "${str}".`);
  }

  swap(updateEntity, "lock", id, c.setPhoneLocation, result.get(0));
}

export function changeEmail(id, email) {
  swap(updateEntity, "lock", id, c.setEmail, email);
}

export function changeVcode(id, vcode) {
  swap(updateEntity, "lock", id, c.setVcode, vcode)
}

export function selectPhoneLocation(id) {
  swap(updateEntity, "lock", id, m.setSelectingLocation, true);
}

export function cancelSelectPhoneLocation(id) {
  swap(updateEntity, "lock", id, m.setSelectingLocation, false);
}

export function requestPasswordlessEmail(id) {
  // TODO: abstract this submit thing.
  swap(updateEntity, "lock", id, lock => {
    if (c.validEmail(lock)) {
      return l.setSubmitting(lock, true);
    } else {
      return c.setShowInvalidEmail(lock);
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    const options = {email: c.email(lock), send: m.send(lock)};
    webApi.startPasswordless(id, options, error => {
      if (error) {
        requestPasswordlessEmailError(id, error);
      } else {
        requestPasswordlessEmailSuccess(id);
      }
    });
  }
}

export function requestPasswordlessEmailSuccess(id) {
  swap(updateEntity, "lock", id, lock => {
    return m.setPasswordlessStarted(l.setSubmitting(lock, false), true);
  });
  const lock = read(getEntity, "lock", id);
  if (m.send(lock) === "link") {
    l.invokeDoneCallback(lock, null, c.email(lock));
  }
}

export function requestPasswordlessEmailError(id, error) {
  const fallbackDescription = "We're sorry, something went wrong when sending the email.";
  swap(updateEntity, "lock", id, l.setSubmitting, false, error.description || fallbackDescription);
  const lock = read(getEntity, "lock", id);
  if (m.send(lock) === "link") {
    l.invokeDoneCallback(lock, error);
  }
}

function fixArgentinianPhoneNumber(lock) {
  const phoneNumber = c.phoneNumber(lock);
  const phoneIsoCode = c.phoneIsoCode(lock);

  return phoneIsoCode === "AR" && phoneNumber[0] === "9" ?
    c.setPhoneNumber(lock, phoneNumber.substr(1, phoneNumber.length -1)) : lock;
}

export function sendSMS(id) {
  // TODO: abstract this submit thing.
  swap(updateEntity, "lock", id, lock => {

    if (c.validPhoneNumber(lock)) {
      lock = fixArgentinianPhoneNumber(lock);
      return l.setSubmitting(lock, true);
    } else {
      return c.setShowInvalidPhoneNumber(lock, true);
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    const options = {phoneNumber: c.fullPhoneNumber(lock)};
    webApi.startPasswordless(id, options, error => {
      if (error) {
        sendSMSError(id, error);
      } else {
        sendSMSSuccess(id);
      }
    });
  }
}

export function sendSMSSuccess(id) {
  swap(updateEntity, "lock", id, lock => {
    lock = l.setSubmitting(lock, false);
    lock = m.setPasswordlessStarted(lock, true);
    return lock;
  });
}

export function sendSMSError(id, error) {
  const fallbackDescription = "We're sorry, something went wrong when sending the SMS.";
  swap(updateEntity, "lock", id, l.setSubmitting, false, error.description || fallbackDescription);
}

export function resendEmail(id) {
  swap(updateEntity, "lock", id, m.resend);

  const lock = read(getEntity, "lock", id);
  const options = {email: c.email(lock), send: m.send(lock)};
  webApi.startPasswordless(id, options, error => {
    if (error) {
      resendEmailError(id, error);
    } else {
      resendEmailSuccess(id);
    }
  });
}

export function resendEmailSuccess(id) {
  swap(updateEntity, "lock", id, m.setResendSuccess);
  const lock = read(getEntity, "lock", id);
  l.invokeDoneCallback(lock, null, c.email(lock));
}

export function resendEmailError(id, error) {
  swap(updateEntity, "lock", id, m.setResendFailed);
  const lock = read(getEntity, "lock", id);
  l.invokeDoneCallback(lock, error);
}

export function signIn(id) {
  // TODO: abstract this submit thing
  swap(updateEntity, "lock", id, lock => {
    if (c.validVcode(lock)) {
      return l.setSubmitting(lock, true);
    } else {
      return c.setShowInvalidVcode(lock);
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    const isSMS = m.send(lock) === "sms";
    const options = {
      connection: isSMS ? "sms" : "email",
      username: isSMS ? c.fullPhoneNumber(lock) : c.email(lock),
      password: <PASSWORD>code(lock),
      sso: false
    };
    webApi.signIn(id, options, (error, ...args) => {
      if (error) {
        signInError(id, error);
      } else {
        signInSuccess(id, ...args);
      }
    });
  }
}

function signInSuccess(id, ...args) {
  const lock = read(getEntity, "lock", id);
  const autoclose = l.ui.autoclose(lock);

  if (!autoclose) {
    swap(updateEntity, "lock", id, lock => m.setSignedIn(l.setSubmitting(lock, false), true));
    l.invokeDoneCallback(lock, null, ...args);
  } else {
    closeLock(id, m.reset, lock => l.invokeDoneCallback(lock, null, ...args));
  }
}

function signInError(id, error) {
  swap(updateEntity, "lock", id, l.setSubmitting, false, error.description);

  const lock = read(getEntity, "lock", id);
  l.invokeDoneCallback(lock, error);
}

export function reset(id, opts = {}) {
  swap(updateEntity, "lock", id, m.reset, opts);
}

export function close(id) {
  closeLock(id, m.reset);
}

export function back(id, resetOpts = {}) {
  reset(id, resetOpts);
}
<commit_msg>Remove magic fix for argentinian phone numbers<commit_after>import { read, getEntity, swap, updateEntity } from '../store/index';
import { closeLock } from '../lock/actions';
import webApi from '../lock/web_api';
import * as c from '../cred/index';
import * as cc from '../cred/country_codes';
import * as l from '../lock/index';
import * as m from './index';

export function changePhoneNumber(id, phoneNumber) {
  swap(updateEntity, "lock", id, c.setPhoneNumber, phoneNumber);
}

export function changePhoneLocation(id, location) {
  swap(updateEntity, "lock", id, lock => {
    lock = m.setSelectingLocation(lock, false);
    lock = c.setPhoneLocation(lock, location);
    return lock;
  });
}

export function setDefaultLocation(id, str) {
  const result = cc.find(str);
  if (!result) {
    throw new Error(`Unable to set the default location, can't find any country with the code "${str}".`);
  }

  swap(updateEntity, "lock", id, c.setPhoneLocation, result.get(0));
}

export function changeEmail(id, email) {
  swap(updateEntity, "lock", id, c.setEmail, email);
}

export function changeVcode(id, vcode) {
  swap(updateEntity, "lock", id, c.setVcode, vcode)
}

export function selectPhoneLocation(id) {
  swap(updateEntity, "lock", id, m.setSelectingLocation, true);
}

export function cancelSelectPhoneLocation(id) {
  swap(updateEntity, "lock", id, m.setSelectingLocation, false);
}

export function requestPasswordlessEmail(id) {
  // TODO: abstract this submit thing.
  swap(updateEntity, "lock", id, lock => {
    if (c.validEmail(lock)) {
      return l.setSubmitting(lock, true);
    } else {
      return c.setShowInvalidEmail(lock);
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    const options = {email: c.email(lock), send: m.send(lock)};
    webApi.startPasswordless(id, options, error => {
      if (error) {
        requestPasswordlessEmailError(id, error);
      } else {
        requestPasswordlessEmailSuccess(id);
      }
    });
  }
}

export function requestPasswordlessEmailSuccess(id) {
  swap(updateEntity, "lock", id, lock => {
    return m.setPasswordlessStarted(l.setSubmitting(lock, false), true);
  });
  const lock = read(getEntity, "lock", id);
  if (m.send(lock) === "link") {
    l.invokeDoneCallback(lock, null, c.email(lock));
  }
}

export function requestPasswordlessEmailError(id, error) {
  const fallbackDescription = "We're sorry, something went wrong when sending the email.";
  swap(updateEntity, "lock", id, l.setSubmitting, false, error.description || fallbackDescription);
  const lock = read(getEntity, "lock", id);
  if (m.send(lock) === "link") {
    l.invokeDoneCallback(lock, error);
  }
}

export function sendSMS(id) {
  // TODO: abstract this submit thing.
  swap(updateEntity, "lock", id, lock => {

    if (c.validPhoneNumber(lock)) {
      return l.setSubmitting(lock, true);
    } else {
      return c.setShowInvalidPhoneNumber(lock, true);
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    const options = {phoneNumber: c.fullPhoneNumber(lock)};
    webApi.startPasswordless(id, options, error => {
      if (error) {
        sendSMSError(id, error);
      } else {
        sendSMSSuccess(id);
      }
    });
  }
}

export function sendSMSSuccess(id) {
  swap(updateEntity, "lock", id, lock => {
    lock = l.setSubmitting(lock, false);
    lock = m.setPasswordlessStarted(lock, true);
    return lock;
  });
}

export function sendSMSError(id, error) {
  const fallbackDescription = "We're sorry, something went wrong when sending the SMS.";
  swap(updateEntity, "lock", id, l.setSubmitting, false, error.description || fallbackDescription);
}

export function resendEmail(id) {
  swap(updateEntity, "lock", id, m.resend);

  const lock = read(getEntity, "lock", id);
  const options = {email: c.email(lock), send: m.send(lock)};
  webApi.startPasswordless(id, options, error => {
    if (error) {
      resendEmailError(id, error);
    } else {
      resendEmailSuccess(id);
    }
  });
}

export function resendEmailSuccess(id) {
  swap(updateEntity, "lock", id, m.setResendSuccess);
  const lock = read(getEntity, "lock", id);
  l.invokeDoneCallback(lock, null, c.email(lock));
}

export function resendEmailError(id, error) {
  swap(updateEntity, "lock", id, m.setResendFailed);
  const lock = read(getEntity, "lock", id);
  l.invokeDoneCallback(lock, error);
}

export function signIn(id) {
  // TODO: abstract this submit thing
  swap(updateEntity, "lock", id, lock => {
    if (c.validVcode(lock)) {
      return l.setSubmitting(lock, true);
    } else {
      return c.setShowInvalidVcode(lock);
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    const isSMS = m.send(lock) === "sms";
    const options = {
      connection: isSMS ? "sms" : "email",
      username: isSMS ? c.fullPhoneNumber(lock) : c.email(lock),
      password: <PASSWORD>(lock),
      sso: false
    };
    webApi.signIn(id, options, (error, ...args) => {
      if (error) {
        signInError(id, error);
      } else {
        signInSuccess(id, ...args);
      }
    });
  }
}

function signInSuccess(id, ...args) {
  const lock = read(getEntity, "lock", id);
  const autoclose = l.ui.autoclose(lock);

  if (!autoclose) {
    swap(updateEntity, "lock", id, lock => m.setSignedIn(l.setSubmitting(lock, false), true));
    l.invokeDoneCallback(lock, null, ...args);
  } else {
    closeLock(id, m.reset, lock => l.invokeDoneCallback(lock, null, ...args));
  }
}

function signInError(id, error) {
  swap(updateEntity, "lock", id, l.setSubmitting, false, error.description);

  const lock = read(getEntity, "lock", id);
  l.invokeDoneCallback(lock, error);
}

export function reset(id, opts = {}) {
  swap(updateEntity, "lock", id, m.reset, opts);
}

export function close(id) {
  closeLock(id, m.reset);
}

export function back(id, resetOpts = {}) {
  reset(id, resetOpts);
}

{'fraction_non_alphanumeric': 0.15421864740643468, 'fraction_numerical': 0.0004924491135915955, 'mean_word_length': 3.780306002353864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '93644', 'n_tokens_mistral': 4083, 'n_tokens_neox': 4023, 'n_words': 980}