<commit_before>import environment from './environment.js';
import { updateLoggedInStatus } from '../../login/actions';
import { updateProfileFields, profileLoadingFinished } from '../../user-profile/actions';

export function handleVerify(verifyUrl) {
  window.dataLayer.push({ event: 'verify-link-clicked' });
  if (verifyUrl) {
    window.dataLayer.push({ event: 'verify-link-opened' });
    const receiver = window.open(`${verifyUrl}&op=signin`, '_blank', 'resizable=yes,scrollbars=1,top=50,left=500,width=500,height=750');
    receiver.focus();
  }
}

export function getUserData(dispatch) {
  fetch(`${environment.API_URL}/v0/user`, {
    method: 'GET',
    headers: new Headers({
      Authorization: `Token token=${sessionStorage.userToken}`
    })
  }).then(response => {
    return response.json();
  }).then(json => {
    if (json.data) {
      const userData = json.data.attributes.profile;
      // sessionStorage coerces everything into String. this if-statement
      // is to prevent the firstname being set to the string 'Null'
      if (userData.first_name) {
        sessionStorage.setItem('userFirstName', userData.first_name);
      }
      dispatch(updateProfileFields({
        savedForms: json.data.attributes.in_progress_forms,
        prefillsAvailable: json.data.attributes.prefills_available,
        accountType: userData.loa.current,
        veteranStatus: json.data.attributes.veteran_status.status,
        email: userData.email,
        userFullName: {
          first: userData.first_name,
          middle: userData.middle_name,
          last: userData.last_name,
        },
        gender: userData.gender,
        dob: userData.birth_date,
        status: json.data.attributes.va_profile.status,
        services: json.data.attributes.services,
        healthTermsCurrent: json.data.attributes.health_terms_current,
      }));
      dispatch(updateLoggedInStatus(true));
    } else {
      dispatch(profileLoadingFinished());
    }
  });
}

export function addEvent(element, eventName, callback) {
  if (element.addEventListener) {
    element.addEventListener(eventName, callback, false);
  } else if (element.attachEvent) {
    element.attachEvent('on' + eventName, callback); // eslint-disable-line prefer-template
  }
}

export function getLoginUrl(onUpdateLoginUrl) {
  const loginUrlRequest = fetch(`${environment.API_URL}/v0/sessions/new?level=1`, {
    method: 'GET',
  }).then(response => {
    return response.json();
  }).then(json => {
    onUpdateLoginUrl(json.authenticate_via_get);<commit_msg>Pull veteran status into profile fetch<commit_after>import environment from './environment.js';
import { updateLoggedInStatus } from '../../login/actions';
import { updateProfileFields, profileLoadingFinished } from '../../user-profile/actions';

export function handleVerify(verifyUrl) {
  window.dataLayer.push({ event: 'verify-link-clicked' });
  if (verifyUrl) {
    window.dataLayer.push({ event: 'verify-link-opened' });
    const receiver = window.open(`${verifyUrl}&op=signin`, '_blank', 'resizable=yes,scrollbars=1,top=50,left=500,width=500,height=750');
    receiver.focus();
  }
}

export function getUserData(dispatch) {
  fetch(`${environment.API_URL}/v0/user`, {
    method: 'GET',
    headers: new Headers({
      Authorization: `Token token=${sessionStorage.userToken}`
    })
  }).then(response => {
    return response.json();
  }).then(json => {
    if (json.data) {
      const userData = json.data.attributes.profile;
      // sessionStorage coerces everything into String. this if-statement
      // is to prevent the firstname being set to the string 'Null'
      if (userData.first_name) {
        sessionStorage.setItem('userFirstName', userData.first_name);
      }
      dispatch(updateProfileFields({
        savedForms: json.data.attributes.in_progress_forms,
        prefillsAvailable: json.data.attributes.prefills_available,
        accountType: userData.loa.current,
        email: userData.email,
        userFullName: {
          first: userData.first_name,
          middle: userData.middle_name,
          last: userData.last_name,
        },
        gender: userData.gender,
        dob: userData.birth_date,
        status: json.data.attributes.va_profile.status,
        veteranStatus: json.data.attributes.veteran_status.status,
        isVeteran: json.data.attributes.veteran_status.is_veteran,
        services: json.data.attributes.services,
        healthTermsCurrent: json.data.attributes.health_terms_current,
      }));
      dispatch(updateLoggedInStatus(true));
    } else {
      dispatch(profileLoadingFinished());
    }
  });
}

export function addEvent(element, eventName, callback) {
  if (element.addEventListener) {
    element.addEventListener(eventName, callback, false);
  } else if (element.attachEvent) {
    element.attachEvent('on' + eventName, callback); // eslint-disable-line prefer-template
  }
}

export function getLoginUrl(onUpdateLoginUrl) {
  const loginUrlRequest = fetch(`${environment.API_URL}/v0/sessions/new?level=1`, {
    method: 'GET',
  }).then(response => {
    return response.json();
  }).then(json => {
    onUpdateLoginUrl(json.authenticate_via_get);
{'fraction_non_alphanumeric': 0.13238131995368582, 'fraction_numerical': 0.005789270551910459, 'mean_word_length': 3.5625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4808967', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1528, 'n_words': 276}