<commit_before>import { handleActions } from 'redux-actions';
import update from 'react-addons-update';
import * as actions from '../actions/dashboard';

export const initialState = {};

function createDashboard(state, { payload }) {
  const id = payload.id;
  return update(state, {
    [id]: { $set: payload },
  });
}

function editDashboard(state, { payload }) {
  const id = payload.id;
  return Object.assign({}, state, {
    [id]: payload,
  });
}

function saveDashboards(state, { payload }) {
  return payload.reduce((result, dashboard) => {
    const id = dashboard.id;
    if (state[id] == null) {
      return update(result, {
        [id]: { $set: update(dashboard, { $merge: { type: 'dashboard', status: 'OK' } }) },
      });
    }
    return result;
  }, state);
}

function saveDashboard(state, { payload }) {
  const id = payload.id;
  return Object.assign({}, state, {
    [id]: payload,
  });
}

function saveShareId(state, { payload }) {
  const id = payload.id;
  return {
    ...state,
    [id]: { ...state[id], ...payload },
  };
}

function saveShareProtection(state, { payload }) {
  const { shareId, data } = payload;
  const dashboardId = Object.keys(state).filter(id => state[id].shareId === shareId)[0];
  if (!dashboardId) return state;
  return {
    ...state,
    [dashboardId]: { ...state[dashboardId], ...data },
  };
}

function removeDashboard(state, { payload }) {
  const newState = Object.assign({}, state);
  delete newState[payload];
  return newState;
}


function removeVisualisationFromDashboardLayout(layout, visualisationId) {
  const newLayout = Object.assign({}, layout);
  delete newLayout[visualisationId];
  return newLayout;
}

function removeVisualisationFromDashboardEntities(entities, visualisationId) {
  const newEntities = Object.assign({}, entities);
  delete newEntities[visualisationId];
  return newEntities;
}

function removeVisualisationFromDashboard(dashboard, visualisationId) {
  return Object.assign({}, dashboard, {
    layout: removeVisualisationFromDashboardLayout(dashboard.layout, visualisationId),
    entities: removeVisualisationFromDashboardEntities(dashboard.entities, visualisationId),
  });
}

function containsVisualisation(dashboard, visualisationId) {
  return dashboard.entities[visualisationId] != null;
}

function removeVisualisation(state, { payload }) {
  const visualisationId = payload;
  const dashboards = Object.assign({}, state);
  Object.keys(dashboards).forEach((dashboardId) => {
    const dashboard = dashboards[dashboardId];
    if (containsVisualisation(dashboard, visualisationId)) {
      dashboards[dashboardId] = removeVisualisationFromDashboard(dashboard, visualisationId);<commit_msg>Deleting anything gives an error but works #1971<commit_after>import { handleActions } from 'redux-actions';
import update from 'react-addons-update';
import * as actions from '../actions/dashboard';
import { get } from 'lodash';

export const initialState = {};

function createDashboard(state, { payload }) {
  const id = payload.id;
  return update(state, {
    [id]: { $set: payload },
  });
}

function editDashboard(state, { payload }) {
  const id = payload.id;
  return Object.assign({}, state, {
    [id]: payload,
  });
}

function saveDashboards(state, { payload }) {
  return payload.reduce((result, dashboard) => {
    const id = dashboard.id;
    if (state[id] == null) {
      return update(result, {
        [id]: { $set: update(dashboard, { $merge: { type: 'dashboard', status: 'OK' } }) },
      });
    }
    return result;
  }, state);
}

function saveDashboard(state, { payload }) {
  const id = payload.id;
  return Object.assign({}, state, {
    [id]: payload,
  });
}

function saveShareId(state, { payload }) {
  const id = payload.id;
  return {
    ...state,
    [id]: { ...state[id], ...payload },
  };
}

function saveShareProtection(state, { payload }) {
  const { shareId, data } = payload;
  const dashboardId = Object.keys(state).filter(id => state[id].shareId === shareId)[0];
  if (!dashboardId) return state;
  return {
    ...state,
    [dashboardId]: { ...state[dashboardId], ...data },
  };
}

function removeDashboard(state, { payload }) {
  const newState = Object.assign({}, state);
  delete newState[payload];
  return newState;
}


function removeVisualisationFromDashboardLayout(layout, visualisationId) {
  const newLayout = Object.assign({}, layout);
  delete newLayout[visualisationId];
  return newLayout;
}

function removeVisualisationFromDashboardEntities(entities, visualisationId) {
  const newEntities = Object.assign({}, entities);
  delete newEntities[visualisationId];
  return newEntities;
}

function removeVisualisationFromDashboard(dashboard, visualisationId) {
  return Object.assign({}, dashboard, {
    layout: removeVisualisationFromDashboardLayout(dashboard.layout, visualisationId),
    entities: removeVisualisationFromDashboardEntities(dashboard.entities, visualisationId),
  });
}

function containsVisualisation(dashboard, visualisationId) {
  return get(dashboard, `entities[${visualisationId}]`) != null;
}

function removeVisualisation(state, { payload }) {
  const visualisationId = payload;
  const dashboards = Object.assign({}, state);
  Object.keys(dashboards).forEach((dashboardId) => {
    const dashboard = dashboards[dashboardId];
    if (containsVisualisation(dashboard, visualisationId)) {
      dashboards[dashboardId] = removeVisualisationFromDashboard(dashboard, visualisationId);
{'fraction_non_alphanumeric': 0.14188941779567923, 'fraction_numerical': 0.0010984987184181618, 'mean_word_length': 4.5181818181818185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4207256', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1650, 'n_words': 365}