<commit_before>import { withAuthenticationRequired } from '@auth0/auth0-react'
import clone from 'clone'
import { Form, Formik } from 'formik'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import * as yup from 'yup'

import * as uiActions from '../../actions/ui'
import * as userActions from '../../actions/user'
import { RETURN_TO_CURRENT_ROUTE } from '../../util/ui'
import { isNewUser, isHome, isWork } from '../../util/user'
import DesktopNav from '../app/desktop-nav'
import AccountSetupFinishPane from './account-setup-finish-pane'
import ExistingAccountDisplay from './existing-account-display'
import FavoriteLocationsPane from './favorite-locations-pane'
import NewAccountWizard from './new-account-wizard'
import NotificationPrefsPane from './notification-prefs-pane'
import TermsOfUsePane from './terms-of-use-pane'
import VerifyEmailScreen from './verify-email-screen'
import withLoggedInUserSupport from './with-logged-in-user-support'

// The validation schema for the form fields.
const validationSchema = yup.object({
  email: yup.string().email(),
  hasConsentedToTerms: yup.boolean().oneOf([true], 'You must agree to the terms to continue.'),
  notificationChannel: yup.string().oneOf(['email', 'sms', 'none']),
  savedLocations: yup.array().of(yup.object({
    address: yup.string(),
    icon: yup.string(),
    type: yup.string()
  })),
  storeTripHistory: yup.boolean()
})

// Defaults for home and work
const BLANK_HOME = {
  address: '',
  icon: 'home',
  name: 'Home',
  type: 'home'
}
const BLANK_WORK = {
  address: '',
  icon: 'briefcase',
  name: 'Work',
  type: 'work'
}

/**
 * Makes a copy of the logged-in user data for the Formik initial state, with:
 * - the 'home' and 'work' locations at the top of the savedLocations list
 *   so they are always shown and shown at the top of the FavoriteLocationsPane.
 *   Note: In the returned value, savedLocations is always a valid array.
 * - initial values for phone number/code fields used by Formik.
 */
function cloneForFormik (userData) {
  const clonedUser = clone(userData)
  const { savedLocations = [] } = clonedUser

  const homeLocation = savedLocations.find(isHome) || BLANK_HOME
  const workLocation = savedLocations.find(isWork) || BLANK_WORK
  const reorderedLocations = [
    homeLocation,
    workLocation,
    ...savedLocations.filter(loc => loc !== homeLocation && loc !== workLocation)
  ]

  clonedUser.savedLocations = reorderedLocations
  return clonedUser
}

/**
 * This screen handles creating/updating OTP user account settings.
 */
class UserAccountScreen extends Component {
  constructor (props) {
    super(props)

    this.state = {
      // Capture whether user is a new user at this stage, and retain that value as long as this screen is active.
      // Reminder: When a new user progresses through the account steps,
      // isNewUser(loggedInUser) will change to false as the database gets updated.
      isNewUser: isNewUser(props.loggedInUser)
    }
  }
<commit_msg>refactor(UserAccountScreen): Force name for home and work locations.<commit_after>import { withAuthenticationRequired } from '@auth0/auth0-react'
import clone from 'clone'
import { Form, Formik } from 'formik'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import * as yup from 'yup'

import * as uiActions from '../../actions/ui'
import * as userActions from '../../actions/user'
import { RETURN_TO_CURRENT_ROUTE } from '../../util/ui'
import { isNewUser, isHome, isWork } from '../../util/user'
import DesktopNav from '../app/desktop-nav'
import AccountSetupFinishPane from './account-setup-finish-pane'
import ExistingAccountDisplay from './existing-account-display'
import FavoriteLocationsPane from './favorite-locations-pane'
import NewAccountWizard from './new-account-wizard'
import NotificationPrefsPane from './notification-prefs-pane'
import TermsOfUsePane from './terms-of-use-pane'
import VerifyEmailScreen from './verify-email-screen'
import withLoggedInUserSupport from './with-logged-in-user-support'

// The validation schema for the form fields.
const validationSchema = yup.object({
  email: yup.string().email(),
  hasConsentedToTerms: yup.boolean().oneOf([true], 'You must agree to the terms to continue.'),
  notificationChannel: yup.string().oneOf(['email', 'sms', 'none']),
  savedLocations: yup.array().of(yup.object({
    address: yup.string(),
    icon: yup.string(),
    type: yup.string()
  })),
  storeTripHistory: yup.boolean()
})

// Defaults for home and work
const BLANK_HOME = {
  address: '',
  icon: 'home',
  name: 'Home',
  type: 'home'
}
const BLANK_WORK = {
  address: '',
  icon: 'briefcase',
  name: 'Work',
  type: 'work'
}

/**
 * Makes a copy of the logged-in user data for the Formik initial state, with:
 * - the 'home' and 'work' locations at the top of the savedLocations list
 *   so they are always shown and shown at the top of the FavoriteLocationsPane.
 *   The name field is set to 'Home' and 'Work' regardless of the value that was persisted.
 *   Note: In the returned value, savedLocations is always a valid array.
 * - initial values for phone number/code fields used by Formik.
 */
function cloneForFormik (userData) {
  const clonedUser = clone(userData)
  const { savedLocations = [] } = clonedUser

  const homeLocation = savedLocations.find(isHome) || BLANK_HOME
  const workLocation = savedLocations.find(isWork) || BLANK_WORK

  homeLocation.name = BLANK_HOME.name
  workLocation.name = BLANK_WORK.name

  const reorderedLocations = [
    homeLocation,
    workLocation,
    ...savedLocations.filter(loc => loc !== homeLocation && loc !== workLocation)
  ]

  clonedUser.savedLocations = reorderedLocations
  return clonedUser
}

/**
 * This screen handles creating/updating OTP user account settings.
 */
class UserAccountScreen extends Component {
  constructor (props) {
    super(props)

    this.state = {
      // Capture whether user is a new user at this stage, and retain that value as long as this screen is active.
      // Reminder: When a new user progresses through the account steps,
      // isNewUser(loggedInUser) will change to false as the database gets updated.
      isNewUser: isNewUser(props.loggedInUser)
    }
  }

{'fraction_non_alphanumeric': 0.11770350231888693, 'fraction_numerical': 0.0006396929473852551, 'mean_word_length': 3.9477848101265822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '357973', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1859, 'n_words': 607}