<commit_before>import { replace } from 'react-router-redux';
import { getIndicatorId, getTempResolution } from 'selectors/nexgddptool';
import {
  NEXGDDP_SET_MAP_ZOOM,
  NEXGDDP_SET_MAP_CENTER,
  NEXGDDP_SET_MARKER_MODE,
  NEXGDDP_SET_MARKER_POSITION,
  NEXGDDP_SET_MAP_MODE,
  NEXGDDP_SET_GRAPH_MODE,
  NEXGDDP_SET_SCENARIO_OPTIONS,
  NEXGDDP_SET_SCENARIO_SELECTION,
  NEXGDDP_SET_RANGE1_OPTIONS,
  NEXGDDP_SET_RANGE1_SELECTION,
  NEXGDDP_SET_RANGE2_OPTIONS,
  NEXGDDP_SET_RANGE2_SELECTION,
  NEXGDDP_SET_CHART_DATA,
  NEXGDDP_SET_CHART_LOADED,
  NEXGDDP_SET_CHART_ERROR,
  NEXGDDP_SET_BASEMAP,
  NEXGDDP_SET_LABELS,
  NEXGDDP_SET_BOUNDARIES,
  NEXGDDP_SET_DATASET,
  NEXGDDP_SET_INDICATOR_DATASET,
  NEXGDDP_RESET_STATE
} from '../constants';

export function updateUrl() {
  return (dispatch, getState) => {
    const state = getState().nexgddptool;
    const params = {
      zoom: state.map.zoom,
      lat: state.map.center
        ? state.map.center[0]
        : undefined,
      lng: state.map.center
        ? state.map.center[1]
        : undefined,
      markerLat: state.marker ? state.marker[0] : undefined,
      markerLng: state.marker ? state.marker[1] : undefined,
      mapMode: state.mapMode,
      graphMode: state.graphMode,
      scenario: state.scenario.selection
        ? state.scenario.selection.value
        : undefined,
      range1: state.range1.selection
        ? state.range1.selection.value
        : undefined,
      range2: state.range2.selection
        ? state.range2.selection.value
        : undefined,
      basemap: state.map.basemap !== 'default'
        ? state.map.basemap
        : undefined,
      labels: state.map.labels !== 'none'
        ? state.map.labels
        : undefined,
      boundaries: !!state.map.boundaries || undefined
    };

    const url = Object.keys(params)
      .filter(key => params[key] !== undefined)
      .map(key => `${key}=${encodeURIComponent(params[key])}`)
      .reduce(
        (res, chunk, index) => `${res}${index === 0 ? '?' : '&'}${chunk}`,
        location.pathname
      );

    dispatch(replace(url));
  };
}

export function setIndicatorDataset(dataset) {
  return dispatch => dispatch({
    type: NEXGDDP_SET_INDICATOR_DATASET,
    payload: dataset
  });
}

export function resetChartData() {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_CHART_LOADED,
      payload: false
    });

    dispatch({
      type: NEXGDDP_SET_CHART_ERROR,
      payload: false
    });

    dispatch({
      type: NEXGDDP_SET_CHART_DATA,
      payload: []
    });
  };
}

export function setMapZoom(zoom, changeUrl = true) {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_MAP_ZOOM,
      payload: zoom
    });

    if (changeUrl) dispatch(updateUrl());
  };
}

export function setMapCenter(center, changeUrl = true) {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_MAP_CENTER,
      payload: center
    });

    if (changeUrl) dispatch(updateUrl());
  };
}

export function setMarkerMode(mode) {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_MARKER_MODE,
      payload: mode
    });
  };
}

export function getChartData() {
  return (dispatch, getState) => {
    dispatch({
      type: NEXGDDP_SET_CHART_LOADED,
      payload: false
    });

    dispatch({
      type: NEXGDDP_SET_CHART_ERROR,
      payload: false
    });

    const state = getState();
    const lat = state.nexgddptool.marker[0];
    const lng = state.nexgddptool.marker[1];
    const indicatorId = getIndicatorId(state);
    const slug = state.nexgddptool.indicatorDataset.slug;

    return fetch(`${process.env.RW_API_URL}/query?sql=select ${indicatorId}_q25 as q25, ${indicatorId} as q50, ${indicatorId}_q75 as q75, year as date from ${slug}&lat=${lat}&lon=${lng}`)
      .then((res) => {
        if (res.ok) return res.json();
        throw new Error('Unable to fetch the data of the chart');
      })
      .then(json => json.data)
      .then(data => dispatch({
        type: NEXGDDP_SET_CHART_DATA,
        payload: data
      }))
      .catch((err) => {
        console.error(err);

        dispatch({
          type: NEXGDDP_SET_CHART_ERROR,
          payload: true
        });
      })
      .then(() => dispatch({
        type: NEXGDDP_SET_CHART_LOADED,
        payload: true
      }));
  };
}

export function setMarkerPosition(coordinates, changeUrl = true) {
  return (dispatch, getState) => {
    dispatch({
      type: NEXGDDP_SET_MARKER_POSITION,
      payload: coordinates
    });

    if (changeUrl) dispatch(updateUrl());

    // We load the data of the chart for this
    // location
    if (getState().nexgddptool.indicatorDataset) dispatch(getChartData());<commit_msg>Prevents an error from being thrown in the console<commit_after>import { replace } from 'react-router-redux';
import { getIndicatorId, getTempResolution } from 'selectors/nexgddptool';
import {
  NEXGDDP_SET_MAP_ZOOM,
  NEXGDDP_SET_MAP_CENTER,
  NEXGDDP_SET_MARKER_MODE,
  NEXGDDP_SET_MARKER_POSITION,
  NEXGDDP_SET_MAP_MODE,
  NEXGDDP_SET_GRAPH_MODE,
  NEXGDDP_SET_SCENARIO_OPTIONS,
  NEXGDDP_SET_SCENARIO_SELECTION,
  NEXGDDP_SET_RANGE1_OPTIONS,
  NEXGDDP_SET_RANGE1_SELECTION,
  NEXGDDP_SET_RANGE2_OPTIONS,
  NEXGDDP_SET_RANGE2_SELECTION,
  NEXGDDP_SET_CHART_DATA,
  NEXGDDP_SET_CHART_LOADED,
  NEXGDDP_SET_CHART_ERROR,
  NEXGDDP_SET_BASEMAP,
  NEXGDDP_SET_LABELS,
  NEXGDDP_SET_BOUNDARIES,
  NEXGDDP_SET_DATASET,
  NEXGDDP_SET_INDICATOR_DATASET,
  NEXGDDP_RESET_STATE
} from '../constants';

export function updateUrl() {
  return (dispatch, getState) => {
    const state = getState().nexgddptool;
    const params = {
      zoom: state.map.zoom,
      lat: state.map.center
        ? state.map.center[0]
        : undefined,
      lng: state.map.center
        ? state.map.center[1]
        : undefined,
      markerLat: state.marker ? state.marker[0] : undefined,
      markerLng: state.marker ? state.marker[1] : undefined,
      mapMode: state.mapMode,
      graphMode: state.graphMode,
      scenario: state.scenario.selection
        ? state.scenario.selection.value
        : undefined,
      range1: state.range1.selection
        ? state.range1.selection.value
        : undefined,
      range2: state.range2.selection
        ? state.range2.selection.value
        : undefined,
      basemap: state.map.basemap !== 'default'
        ? state.map.basemap
        : undefined,
      labels: state.map.labels !== 'none'
        ? state.map.labels
        : undefined,
      boundaries: !!state.map.boundaries || undefined
    };

    const url = Object.keys(params)
      .filter(key => params[key] !== undefined)
      .map(key => `${key}=${encodeURIComponent(params[key])}`)
      .reduce(
        (res, chunk, index) => `${res}${index === 0 ? '?' : '&'}${chunk}`,
        location.pathname
      );

    dispatch(replace(url));
  };
}

export function setIndicatorDataset(dataset) {
  return dispatch => dispatch({
    type: NEXGDDP_SET_INDICATOR_DATASET,
    payload: dataset
  });
}

export function resetChartData() {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_CHART_LOADED,
      payload: false
    });

    dispatch({
      type: NEXGDDP_SET_CHART_ERROR,
      payload: false
    });

    dispatch({
      type: NEXGDDP_SET_CHART_DATA,
      payload: []
    });
  };
}

export function setMapZoom(zoom, changeUrl = true) {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_MAP_ZOOM,
      payload: zoom
    });

    if (changeUrl) dispatch(updateUrl());
  };
}

export function setMapCenter(center, changeUrl = true) {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_MAP_CENTER,
      payload: center
    });

    if (changeUrl) dispatch(updateUrl());
  };
}

export function setMarkerMode(mode) {
  return (dispatch) => {
    dispatch({
      type: NEXGDDP_SET_MARKER_MODE,
      payload: mode
    });
  };
}

export function getChartData() {
  return (dispatch, getState) => {
    dispatch({
      type: NEXGDDP_SET_CHART_LOADED,
      payload: false
    });

    dispatch({
      type: NEXGDDP_SET_CHART_ERROR,
      payload: false
    });

    const state = getState();
    const lat = state.nexgddptool.marker[0];
    const lng = state.nexgddptool.marker[1];
    const indicatorId = getIndicatorId(state);
    const slug = state.nexgddptool.indicatorDataset.slug;

    return fetch(`${process.env.RW_API_URL}/query?sql=select ${indicatorId}_q25 as q25, ${indicatorId} as q50, ${indicatorId}_q75 as q75, year as date from ${slug}&lat=${lat}&lon=${lng}`)
      .then((res) => {
        if (res.ok) return res.json();
        throw new Error('Unable to fetch the data of the chart');
      })
      .then(json => json.data)
      .then(data => dispatch({
        type: NEXGDDP_SET_CHART_DATA,
        payload: data
      }))
      .catch((err) => {
        console.error(err);

        dispatch({
          type: NEXGDDP_SET_CHART_ERROR,
          payload: true
        });
      })
      .then(() => dispatch({
        type: NEXGDDP_SET_CHART_LOADED,
        payload: true
      }));
  };
}

export function setMarkerPosition(coordinates, changeUrl = true) {
  return (dispatch, getState) => {
    dispatch({
      type: NEXGDDP_SET_MARKER_POSITION,
      payload: coordinates
    });

    if (changeUrl) dispatch(updateUrl());

    // We load the data of the chart for this
    // location
    if (getState().nexgddptool.indicatorDataset && getState().nexgddptool.marker) {
      dispatch(getChartData());
    }
{'fraction_non_alphanumeric': 0.12207463630613535, 'fraction_numerical': 0.0056925996204933585, 'mean_word_length': 2.9121649484536083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '3378420', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3288, 'n_words': 586}