<commit_before>import update from 'immutability-helper';
import { createAction } from 'redux-actions';

export const ADD_PHASE = 'addPhaseAction';
export const EDIT_PHASE = 'editPhaseAction';
export const EDIT_PHASE_ATTRIBUTE = 'editPhaseAttributeAction';
export const REMOVE_PHASE = 'removePhaseAction';
export const SET_PHASE_VISIBILITY = 'setPhaseVisibilityAction';
export const SET_PHASES_VISIBILITY = 'setPhasesVisibilityAction';

export function addPhase (typeSpecifier, parent) {
  const phaseId = Math.random().toString(36).replace(/[^a-z]+/g, '');
  const newPhase = {
    id: phaseId,
    function: parent,
    actions: [],
    attributes: { TypeSpecifier: typeSpecifier },
    is_open: false
  };

  return createAction(ADD_PHASE)(newPhase);
}

export function editPhase (editedPhase) {
  return createAction(EDIT_PHASE)(editedPhase);
}

export function editPhaseAttribute (editedPhaseAttribute) {
  return createAction(EDIT_PHASE_ATTRIBUTE)(editedPhaseAttribute);
}

export function removePhase (phaseToRemove) {
  return createAction(REMOVE_PHASE)(phaseToRemove);
}

export function setPhaseVisibility (phase, visibility) {
  return createAction(SET_PHASE_VISIBILITY)({ phase, visibility });
}

export function setPhasesVisibility (phases, value) {
  const allPhasesOpen = {};
  for (const key in phases) {
    if (phases.hasOwnProperty(key)) {
      allPhasesOpen[key] = update(phases[key], {
        is_open: {
          $set: value
        }
      });
    }
  }
  return createAction(SET_PHASES_VISIBILITY)(allPhasesOpen);
}

export const addPhaseAction = (state, { payload }) => {
  return update(state, {
    phases: {
      [payload.id]: {
        $set: payload
      }
    }
  });
};

export const editPhaseAction = (state, { payload }) => {
  return update(state, {
    phases: {
      [payload.id]: {
        attributes: {
          $set: payload.attributes
        }
      }
    }
  });
};

export const editPhaseAttributeAction = (state, { payload }) => {
  if (payload.typeSpecifier) {
    return update(state, {
      phases: {
        [payload.phaseId]: {
          attributes: {
            TypeSpecifier: {
              $set: payload.typeSpecifier
            }
          }
        }
      }
    });
  } else if (payload.type) {
    return update(state, {
      phases: {
        [payload.phaseId]: {
          attributes: {
            PhaseType: {
              $set: payload.type
            }
          }
        }
      }
    });
  } else {
    return update(state, {
      phases: {
        [payload.phaseId]: {
          attributes: {
            [payload.attributeIndex]: {
              $set: payload.attribute
            }
          }
        }
      }
    });
  }
};

export const removePhaseAction = (state, { payload }) => {
  const phasesCopy = Object.assign({}, state.phases);
  delete phasesCopy[payload];

  return update(state, {
    phases: {
      $set: phasesCopy
    }
  });
};

export const setPhaseVisibilityAction = (state, { payload }) => {
  return update(state, {
    phases: {
      [payload.phase]: {
        is_open: {
          $set: payload.visibility
        }
      }
    }
  });
};

export const setPhasesVisibilityAction = (state, { payload }) => {
  return update(state, {
    phases: {
      $set: payload
    }
  });
};
<commit_msg>Create reducer function for editing Phase with EditorForm<commit_after>import update from 'immutability-helper';
import { createAction } from 'redux-actions';

export const ADD_PHASE = 'addPhaseAction';
export const EDIT_PHASE = 'editPhaseAction';
export const EDIT_PHASE_ATTRIBUTE = 'editPhaseAttributeAction';
export const REMOVE_PHASE = 'removePhaseAction';
export const SET_PHASE_VISIBILITY = 'setPhaseVisibilityAction';
export const SET_PHASES_VISIBILITY = 'setPhasesVisibilityAction';

export function addPhase (typeSpecifier, parent) {
  const phaseId = Math.random().toString(36).replace(/[^a-z]+/g, '');
  const newPhase = {
    id: phaseId,
    function: parent,
    actions: [],
    attributes: { TypeSpecifier: typeSpecifier },
    is_open: false
  };

  return createAction(ADD_PHASE)(newPhase);
}

export function editPhase (attributes, phaseId) {
  let editedAttributes = {};

  for (const key in attributes) {
    if (attributes.hasOwnProperty(key)) {
      if (attributes[key].checked === true) {
        editedAttributes[key] = attributes[key].value;
      }
    }
  }

  const editedPhase = Object.assign({}, {
    attributes: editedAttributes
  });

  return createAction(EDIT_PHASE)({ editedPhase, phaseId });
}

export function editPhaseAttribute (editedPhaseAttribute) {
  return createAction(EDIT_PHASE_ATTRIBUTE)(editedPhaseAttribute);
}

export function removePhase (phaseToRemove) {
  return createAction(REMOVE_PHASE)(phaseToRemove);
}

export function setPhaseVisibility (phase, visibility) {
  return createAction(SET_PHASE_VISIBILITY)({ phase, visibility });
}

export function setPhasesVisibility (phases, value) {
  const allPhasesOpen = {};
  for (const key in phases) {
    if (phases.hasOwnProperty(key)) {
      allPhasesOpen[key] = update(phases[key], {
        is_open: {
          $set: value
        }
      });
    }
  }
  return createAction(SET_PHASES_VISIBILITY)(allPhasesOpen);
}

export const addPhaseAction = (state, { payload }) => {
  return update(state, {
    phases: {
      [payload.id]: {
        $set: payload
      }
    }
  });
};

export const editPhaseAction = (state, { payload }) => {
  return update(state, {
    phases: {
      [payload.phaseId]: {
        attributes: {
          $set: payload.editedPhase.attributes
        }
      }
    }
  });
};

export const editPhaseAttributeAction = (state, { payload }) => {
  if (payload.typeSpecifier) {
    return update(state, {
      phases: {
        [payload.phaseId]: {
          attributes: {
            TypeSpecifier: {
              $set: payload.typeSpecifier
            }
          }
        }
      }
    });
  } else if (payload.type) {
    return update(state, {
      phases: {
        [payload.phaseId]: {
          attributes: {
            PhaseType: {
              $set: payload.type
            }
          }
        }
      }
    });
  } else {
    return update(state, {
      phases: {
        [payload.phaseId]: {
          attributes: {
            [payload.attributeIndex]: {
              $set: payload.attribute
            }
          }
        }
      }
    });
  }
};

export const removePhaseAction = (state, { payload }) => {
  const phasesCopy = Object.assign({}, state.phases);
  delete phasesCopy[payload];

  return update(state, {
    phases: {
      $set: phasesCopy
    }
  });
};

export const setPhaseVisibilityAction = (state, { payload }) => {
  return update(state, {
    phases: {
      [payload.phase]: {
        is_open: {
          $set: payload.visibility
        }
      }
    }
  });
};

export const setPhasesVisibilityAction = (state, { payload }) => {
  return update(state, {
    phases: {
      $set: payload
    }
  });
};

{'fraction_non_alphanumeric': 0.12492853058890795, 'fraction_numerical': 0.0005717552887364208, 'mean_word_length': 2.5698979591836735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6326898', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2102, 'n_words': 425}