<commit_before>import React, {Component} from "react";
import Helmet from "react-helmet";
import {connect} from "react-redux";
import {browserHistory} from "react-router";
import d3plus, {tooltipBody} from "helpers/d3plus";
import {DICTIONARY} from "helpers/dictionary";
import {VARIABLES} from "helpers/formatters";
import Radio from "components/Radio";
import Selector from "components/Selector";
import {API} from "helpers/consts.js";
import axios from "axios";
import "./Map.css";

import header from "../helmet.js";

import {CanonComponent} from "datawheel-canon";
import {extent, mean} from "d3-array";
import {nest} from "d3-collection";
import {selectAll} from "d3-selection";
import {merge} from "d3plus-common";
import {Geomap} from "d3plus-react";
import {titleCase} from "d3plus-text";
import {dataFold as fold} from "d3plus-viz";

const queryDefaults = {
  column: "rainfall_awa_mm",
  geo: "adm0"
};

class Map extends Component {
  constructor(props) {
    super(props);
    const {column, geo} = {...queryDefaults, ...props.location.query};

    this.state = {
      first: true,
      loaded: false,
      column,
      geo,
      data: [],
      levels: {}
    };

    this.handleColumn = this.handleColumn.bind(this);
    this.handleGeo = this.handleGeo.bind(this);
  }

  componentDidMount() {
    this.fetchData();
  }

  fetchData(newVars = {}) {
    let {geo, column} = this.state;
    geo = newVars.geo ? newVars.geo : geo;
    column = newVars.column ? newVars.column : column;
    const mapParams = this.datasetPrep(geo, column);
    const dataset = this.dataset(column);
    const origColumn = column;

    let required = mapParams.variable === "geo" ? column : `${[mapParams.variable, mapParams.variableName].join(",")},${column}`;
    console.log(column, origColumn, dataset);
    if (column === "rainfall_awa_mm") required += ",start_year";
    if (dataset === "poverty" && (column.endsWith("ppp1") || column.endsWith("ppp2"))) {
      const povLevel = column.split("_").slice(-1)[0];
      const url = `${API}api/poverty?show=${geo}&poverty_level=${povLevel}`;
      axios.get(url).then(result => {
        const data = result.data.data;
        this.setState({geo, column, data, loaded: true}, () => this.handleUrl());
      });
    }
    else if (dataset === "dhs") {
      const condition = origColumn.split("_").slice(-1)[0];
      const url = `${API}api/health?show=${geo}&severity=severe&condition=${condition}`;
      axios.get(url).then(result => {
        const data = result.data.data;
        this.setState({geo, column, data, loaded: true}, () => this.handleUrl());
      });
    }
    else {
      const show = mapParams.variable;
      const url = `${API}api/join/?show=year,${show}&sumlevel=latest_by_geo,${geo}&required=${required},url_name&order=${column}&sort=desc&display_names=true`;

      axios.get(url).then(result => {
        const data = fold(result.data);
        this.setState({geo, column: origColumn, data, loaded: true}, () => this.handleUrl());
      });<commit_msg>tests harvested area through dedicated endpoint<commit_after>import React, {Component} from "react";
import Helmet from "react-helmet";
import {connect} from "react-redux";
import {browserHistory} from "react-router";
import d3plus, {tooltipBody} from "helpers/d3plus";
import {DICTIONARY} from "helpers/dictionary";
import {VARIABLES} from "helpers/formatters";
import Radio from "components/Radio";
import Selector from "components/Selector";
import {API} from "helpers/consts.js";
import axios from "axios";
import "./Map.css";

import header from "../helmet.js";

import {CanonComponent} from "datawheel-canon";
import {extent, mean} from "d3-array";
import {nest} from "d3-collection";
import {selectAll} from "d3-selection";
import {merge} from "d3plus-common";
import {Geomap} from "d3plus-react";
import {titleCase} from "d3plus-text";
import {dataFold as fold} from "d3plus-viz";

const queryDefaults = {
  column: "rainfall_awa_mm",
  geo: "adm0"
};

class Map extends Component {
  constructor(props) {
    super(props);
    const {column, geo} = {...queryDefaults, ...props.location.query};

    this.state = {
      first: true,
      loaded: false,
      column,
      geo,
      data: [],
      levels: {}
    };

    this.handleColumn = this.handleColumn.bind(this);
    this.handleGeo = this.handleGeo.bind(this);
  }

  componentDidMount() {
    this.fetchData();
  }

  fetchData(newVars = {}) {
    let {geo, column} = this.state;
    geo = newVars.geo ? newVars.geo : geo;
    column = newVars.column ? newVars.column : column;
    const mapParams = this.datasetPrep(geo, column);
    const dataset = this.dataset(column);
    const origColumn = column;

    let required = mapParams.variable === "geo" ? column : `${[mapParams.variable, mapParams.variableName].join(",")},${column}`;
    console.log(column, origColumn, dataset);
    if (column === "rainfall_awa_mm") required += ",start_year";
    if (dataset === "poverty" && (column.endsWith("ppp1") || column.endsWith("ppp2"))) {
      const povLevel = column.split("_").slice(-1)[0];
      const url = `${API}api/poverty?show=${geo}&poverty_level=${povLevel}`;
      axios.get(url).then(result => {
        const data = result.data.data;
        this.setState({geo, column, data, loaded: true}, () => this.handleUrl());
      });
    }
    else if (dataset === "dhs") {
      const condition = origColumn.split("_").slice(-1)[0];
      const url = `${API}api/health?show=${geo}&severity=severe&condition=${condition}`;
      axios.get(url).then(result => {
        const data = result.data.data;
        this.setState({geo, column, data, loaded: true}, () => this.handleUrl());
      });
    }
    else if (column === "harvested_area") {
      const url = `${API}api/harvested_area?show=${geo}`;
      axios.get(url).then(result => {
        const data = result.data.data;
        this.setState({geo, column, data, loaded: true}, () => this.handleUrl());
      });
    }
    else {
      const show = mapParams.variable;
      const url = `${API}api/join/?show=year,${show}&sumlevel=latest_by_geo,${geo}&required=${required},url_name&order=${column}&sort=desc&display_names=true`;

      axios.get(url).then(result => {
        const data = fold(result.data);
        this.setState({geo, column: origColumn, data, loaded: true}, () => this.handleUrl());
      });
{'fraction_non_alphanumeric': 0.1731826032344167, 'fraction_numerical': 0.005024336630554247, 'mean_word_length': 3.746646795827124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4366035', 'n_tokens_mistral': 2067, 'n_tokens_neox': 2066, 'n_words': 454}