<commit_before>import { CART } from "../../utilities/events-utilities";

export const types = {
  INITIALIZED: "CART/INITIALIZED",
  ADD: "CART/ADD",
  UPDATED: "CART/UPDATED",
  CART_EMPTY: "CART/EMPTY",
  CART_EMPTY_SUCCESS: "CART/EMPTY_SUCCESS",
  REMOVE_SAMPLE: "CART/REMOVE_SAMPLE",
  REMOVE_PROJECT: "CART/REMOVE_PROJECT",
  LOAD_CART: "CART/LOAD_CART",
  CART_LOADED: "CART/CART_LOADED",
  APPLY_FILTER: "CART/FILTER"
};

const initialState = {
  count: 0,
  initialized: false,
  loaded: false,
  filter: "",
  samples: [],
  filteredSamples: []
};

function filterSamples(samples = [], filter = "") {
  return filter.length > 0
    ? samples.filter(s => s.label.toLowerCase().includes(filter))
    : samples;
}

function removeSample(samples, filter, projectId, sampleId) {
  const index = samples.findIndex(
    sample => sample.project.id === projectId && sample.id === sampleId
  );
  samples.splice(index, 1);
  return {
    samples,
    filteredSamples: filterSamples(samples, filter)
  };
}

function removeProject(samples, filter, projectId) {
  const filtered = samples.filter(s => s.project.id !== projectId);
  return {
    samples: filtered,
    filteredSamples: filterSamples(filtered, filter)
  };
}

export const reducer = (state = initialState, action = {}) => {
  switch (action.type) {
    case types.INITIALIZED:
      return { ...state, ...{ count: action.count, initialized: true } };
    case types.UPDATED:
      /*
      Since the cart is not currently a react component, setting the state does
      nothing.  We are going to use a CustomEvent so that we can communicate with
      the current AngularJS controller for the navigation.
       */
      document.dispatchEvent(
        new CustomEvent(CART.UPDATED, {<commit_msg>Fixed issue with removing single sample not updating.<commit_after>import { CART } from "../../utilities/events-utilities";

export const types = {
  INITIALIZED: "CART/INITIALIZED",
  ADD: "CART/ADD",
  UPDATED: "CART/UPDATED",
  CART_EMPTY: "CART/EMPTY",
  CART_EMPTY_SUCCESS: "CART/EMPTY_SUCCESS",
  REMOVE_SAMPLE: "CART/REMOVE_SAMPLE",
  REMOVE_PROJECT: "CART/REMOVE_PROJECT",
  LOAD_CART: "CART/LOAD_CART",
  CART_LOADED: "CART/CART_LOADED",
  APPLY_FILTER: "CART/FILTER"
};

const initialState = {
  count: 0,
  initialized: false,
  loaded: false,
  filter: "",
  samples: [],
  filteredSamples: []
};

function filterSamples(samples = [], filter = "") {
  return filter.length > 0
    ? samples.filter(s => s.label.toLowerCase().includes(filter))
    : samples;
}

function removeSample(samples, filter, projectId, sampleId) {
  const updatedSamples = [...samples];
  const index = updatedSamples.findIndex(
    sample => sample.project.id === projectId && sample.id === sampleId
  );
  updatedSamples.splice(index, 1);
  return {
    samples: updatedSamples,
    filteredSamples: filterSamples(updatedSamples, filter)
  };
}

function removeProject(samples, filter, projectId) {
  const filtered = samples.filter(s => s.project.id !== projectId);
  return {
    samples: filtered,
    filteredSamples: filterSamples(filtered, filter)
  };
}

export const reducer = (state = initialState, action = {}) => {
  switch (action.type) {
    case types.INITIALIZED:
      return { ...state, ...{ count: action.count, initialized: true } };
    case types.UPDATED:
      /*
      Since the cart is not currently a react component, setting the state does
      nothing.  We are going to use a CustomEvent so that we can communicate with
      the current AngularJS controller for the navigation.
       */
      document.dispatchEvent(
        new CustomEvent(CART.UPDATED, {
{'fraction_non_alphanumeric': 0.12960924600990645, 'fraction_numerical': 0.001651073197578426, 'mean_word_length': 3.654289372599232, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4887008', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1163, 'n_words': 285}