<commit_before>import {
  CREATE_GOTO, REMOVE_GOTO, EDIT_GOTO
} from '../actions/goTo'
import {
  LOAD_QUESTIONNAIRE_SUCCESS
} from '../actions/questionnaire'

const emptyGoTo = {
  description: '',
  expression: '',
  ifTrue: null, //ifTrue and ifFalse can be used to store a label, which can be
                //an empty string, so we use `null` as a default value
  ifFalse: null,
  ifTrueIsAName: false,
  ifFalseIsAName: false
}

export default function (state={}, action) {
  const { type, payload } = action
  switch (type) {
    case CREATE_GOTO:
      return {
        ...state,
        [payload.id]: {
          id: payload.id,
          ...emptyGoTo
        }
      }
    case EDIT_GOTO:
      //TODO inspect ifTrue and ifFalse to replace empty strings by `null`.
      //Anyway, we should decide how to handle components with no label in
      //goTo edition
      return {
        ...state,
        [payload.id]: {
          ...state[payload.id],
          ...payload.update
        }
      }
    case REMOVE_GOTO:
      const { [payload.id]: toRemove, ...toKeep } = state
      return toKeep
    case LOAD_QUESTIONNAIRE_SUCCESS:
      return payload.update.goToById
    default:
      return state
  }
}<commit_msg>Switch to function mapping for reducer syntax<commit_after>import {
  CREATE_GOTO, REMOVE_GOTO, EDIT_GOTO
} from '../actions/goTo'
import {
  LOAD_QUESTIONNAIRE_SUCCESS
} from '../actions/questionnaire'

const emptyGoTo = {
  description: '',
  expression: '',
  ifTrue: null, //ifTrue and ifFalse can be used to store a label, which can be
                //an empty string, so we use `null` as a default value
  ifFalse: null,
  ifTrueIsAName: false,
  ifFalseIsAName: false
}

const actionsHndlrs = {
  CREATE_GOTO: createGoTo,
  REMOVE_GOTO: removeGoTo,
  EDIT_GOTO: editGoTo,
  LOAD_QUESTIONNAIRE_SUCCESS: loadQuestionnaireSuccess
}


export default function (state={}, action) {
  if (!action) return state
  const { type, payload } = action
  const hndlr = actionsHndlrs[type]
  return hndlr ? hndlr(state, payload, action) : state
}


function removeGoTo(goTos, { id }) {
  const { [id]: toRemove, ...toKeep } = goTos
  return toKeep
}

function createGoTo(goTos, { id }) {
  return {
    ...goTos,
    [id]: {
      id,
      ...emptyGoTo
    }
  }
}

function editGoTo(goTos, { id, update }) {
  return {
    ...goTos,
    [id]: {
      ...goTos[id],
      ...update
    }
  }
}

function loadQuestionnaireSuccess(goTos, { update: { goToById }}) {
  return goToById
}
{'fraction_non_alphanumeric': 0.1156761069006781, 'fraction_numerical': 0.0, 'mean_word_length': 2.5224719101123596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3285752', 'n_tokens_mistral': 865, 'n_tokens_neox': 828, 'n_words': 230}