<commit_before>import objectPath from 'object-path'

import {setErrorMessage} from './status'
import {secureFetch} from '../../common/actions'

export function checkingExistingLogin (loginProps) {
  return {
    type: 'CHECKING_EXISTING_LOGIN',
    loginProps
  }
}

export function noExistingLogin () {
  return {
    type: 'NO_EXISTING_LOGIN'
  }
}

export function userLoggedIn (token, profile) {
  return {
    type: 'USER_LOGGED_IN',
    token,
    profile
  }
}

export function revokeToken () {
  return {
    type: 'REVOKE_USER_TOKEN'
  }
}

export function checkExistingLogin (loginProps) {
  return function (dispatch, getState) {
    dispatch(checkingExistingLogin(loginProps))
    var login = getState().user.auth0.checkExistingLogin(loginProps)
    if (login) {
      return login.then((userTokenAndProfile) => {
        if (userTokenAndProfile) {
          dispatch(userLoggedIn(userTokenAndProfile.token, userTokenAndProfile.profile))
        } else {
          console.log('error checking token')
        }
      })
    } else {
      // no login found
      dispatch(noExistingLogin())
      // return empty promise
      return new Promise((resolve) => { resolve(null) })
    }
  }
}

// server call
export function fetchUser (user) {
  return function (dispatch, getState) {
    const url = '/api/manager/secure/user/' + user
    return dispatch(secureFetch(url))
      .then(response => response.json())
      .then(user => {
        return user
      })
  }
}

function updatingUserMetadata (profile, props, payload) {
  return {
    type: 'UPDATING_USER_METADATA',
    profile,
    props,
    payload
  }
}

export function updateUserMetadata (profile, props) {
  return function (dispatch, getState) {
    const CLIENT_ID = process.env.AUTH0_CLIENT_ID
    let userMetadata

    if ( // User metadata already has DataTools entry for this CLIENT_ID
      profile.user_metadata &&
      profile.user_metadata.datatools &&
      profile.user_metadata.datatools.findIndex(d => d.client_id === CLIENT_ID) !== -1
    ) {
      userMetadata = profile.user_metadata
    } else if ( // User metadata exists but does not have a DT entry for this CLIENT_ID
      profile.user_metadata &&
      profile.user_metadata.datatools
    ) {
      userMetadata.datatools.push({ client_id: CLIENT_ID })
    } else { // User metadata empty or does not exist
      userMetadata = {
        lang: 'en',
        datatools: [{ client_id: CLIENT_ID }]
      }
    }

    const clientIndex = userMetadata.datatools.findIndex(d => d.client_id === CLIENT_ID)
    for (var key in props) {
      objectPath.set(userMetadata, `datatools.${clientIndex}.${key}`, props[key])
    }
    const payload = {user_metadata: userMetadata}
    dispatch(updatingUserMetadata(profile, props, payload))
    const url = `https://${process.env.AUTH0_DOMAIN}/api/v2/users/${profile.user_id}`
    return dispatch(secureFetch(url, 'PATCH', payload))
      .then(response => response.json())
      .then(user => {
        if (user.user_id === getState().user.profile.user_id) {
          dispatch(checkExistingLogin())
        }
        return user
      })
  }
}
<commit_msg>fix(user): Fix bug with saving UI preferences to user metadata<commit_after>import objectPath from 'object-path'

import {setErrorMessage} from './status'
import {secureFetch} from '../../common/actions'

export function checkingExistingLogin (loginProps) {
  return {
    type: 'CHECKING_EXISTING_LOGIN',
    loginProps
  }
}

export function noExistingLogin () {
  return {
    type: 'NO_EXISTING_LOGIN'
  }
}

export function userLoggedIn (token, profile) {
  return {
    type: 'USER_LOGGED_IN',
    token,
    profile
  }
}

export function revokeToken () {
  return {
    type: 'REVOKE_USER_TOKEN'
  }
}

export function checkExistingLogin (loginProps) {
  return function (dispatch, getState) {
    dispatch(checkingExistingLogin(loginProps))
    var login = getState().user.auth0.checkExistingLogin(loginProps)
    if (login) {
      return login.then((userTokenAndProfile) => {
        if (userTokenAndProfile) {
          dispatch(userLoggedIn(userTokenAndProfile.token, userTokenAndProfile.profile))
        } else {
          console.log('error checking token')
        }
      })
    } else {
      // no login found
      dispatch(noExistingLogin())
      // return empty promise
      return new Promise((resolve) => { resolve(null) })
    }
  }
}

// server call
export function fetchUser (user) {
  return function (dispatch, getState) {
    const url = '/api/manager/secure/user/' + user
    return dispatch(secureFetch(url))
      .then(response => response.json())
      .then(user => {
        return user
      })
  }
}

function updatingUserMetadata (profile, props, payload) {
  return {
    type: 'UPDATING_USER_METADATA',
    profile,
    props,
    payload
  }
}

export function updateUserMetadata (profile, props) {
  return function (dispatch, getState) {
    const {user_metadata: userMetadata} = profile
    const CLIENT_ID = process.env.AUTH0_CLIENT_ID
    const hasDatatoolsEntry = userMetadata && userMetadata.datatools
    const updatedMetadata = hasDatatoolsEntry
      ? userMetadata
      : { lang: 'en', datatools: [] }
    if (updatedMetadata.datatools.findIndex(d => d.client_id === CLIENT_ID) === -1) {
      // User metadata exists but does not have a DT entry for this CLIENT_ID
      updatedMetadata.datatools.push({ client_id: CLIENT_ID })
    }
    const clientIndex = updatedMetadata.datatools.findIndex(d => d.client_id === CLIENT_ID)
    for (var key in props) {
      objectPath.set(updatedMetadata, `datatools.${clientIndex}.${key}`, props[key])
    }
    const payload = {user_metadata: updatedMetadata}
    dispatch(updatingUserMetadata(profile, props, payload))
    const url = `https://${process.env.AUTH0_DOMAIN}/api/v2/users/${profile.user_id}`
    return dispatch(secureFetch(url, 'PATCH', payload))
      .then(response => response.json())
      .then(user => {
        if (user.user_id === getState().user.profile.user_id) {
          dispatch(checkExistingLogin())
        }
        return user
      })
  }
}

{'fraction_non_alphanumeric': 0.11750285994443536, 'fraction_numerical': 0.0016342539630658604, 'mean_word_length': 3.0664451827242525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '1047238', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1869, 'n_words': 415}