<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import isEmpty from 'lodash/isEmpty';
import 'isomorphic-fetch';

// Redux
import withRedux from 'next-redux-wrapper';
import { initStore } from 'store';
import { bindActionCreators } from 'redux';
import { getWidget } from 'redactions/widget';
import { setUser } from 'redactions/user';
import { setRouter } from 'redactions/routes';

// Components
import Page from 'components/app/layout/Page';
import EmbedLayout from 'components/app/layout/EmbedLayout';
import Spinner from 'components/ui/Spinner';

class EmbedTable extends Page {
  static getInitialProps({ asPath, pathname, query, req, store, isServer }) {
    const { user } = isServer ? req : store.getState();
    const url = { asPath, pathname, query };
    const referer = isServer ? req.headers.referer : location.href;
    store.dispatch(setUser(user));
    store.dispatch(setRouter(url));
    return { user, isServer, url, referer, isLoading: true };
  }

  isLoadedExternally() {
    return !/localhost|staging.resourcewatch.org/.test(this.props.referer);
  }

  constructor(props) {
    super(props);
    this.state = {
      isLoading: props.isLoading,
      tableData: []
    };
  }

  componentDidMount() {
    this.props.getWidget(this.props.url.query.id);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.widget && nextProps.widget.attributes) {
      const queryURL = nextProps.widget.attributes.widgetConfig.data[0].url;
      this.loadTableData(queryURL);
    }
  }

  loadTableData(query) {
    fetch(query)
      .then((response) => {
        this.setState({
          isLoading: false,
          tableData: response
        });
      })
      .catch((error) => {
        this.setState({ isLoading: false });
        console.error(error);
      });
  }

  render() {
    const { widget } = this.props;
    const { isLoading, tableData } = this.state;

    const header = tableData && tableData.length > 0 && Object.keys(tableData[0]);

    if (isEmpty(tableData)) {
      return (
        <EmbedLayout
          title={'Loading widget...'}
          description={''}
        >
          <Spinner isLoading className="-light" />
        </EmbedLayout>
      );
    }
    debugger;
    console.log('tableData', tableData);

    return (
      <EmbedLayout
        title={`${widget.attributes.name}`}
        description={`${widget.attributes.description || ''}`}
      >
        <div className="c-embed-widget">
          <div className="visualization">
            <Spinner isLoading={isLoading} className="-light" />
            <div className="widget-title">
              <h4>{widget.attributes.name}</h4>
            </div>
            <div className="widget-content">
              {tableData &&
                <table>
                  <thead>
                    <tr>
                      {header && header.map(val => (
                        <th
                          key={`header_${val}`}
                        >
                          {val}
                        </th>
                      ))<commit_msg>load table data<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import isEmpty from 'lodash/isEmpty';
import 'isomorphic-fetch';

// Redux
import withRedux from 'next-redux-wrapper';
import { initStore } from 'store';
import { bindActionCreators } from 'redux';
import { getWidget } from 'redactions/widget';
import { setUser } from 'redactions/user';
import { setRouter } from 'redactions/routes';

// Components
import Page from 'components/app/layout/Page';
import EmbedLayout from 'components/app/layout/EmbedLayout';
import Spinner from 'components/ui/Spinner';

class EmbedTable extends Page {
  static getInitialProps({ asPath, pathname, query, req, store, isServer }) {
    const { user } = isServer ? req : store.getState();
    const url = { asPath, pathname, query };
    const referer = isServer ? req.headers.referer : location.href;
    store.dispatch(setUser(user));
    store.dispatch(setRouter(url));
    return { user, isServer, url, referer, isLoading: true };
  }

  isLoadedExternally() {
    return !/localhost|staging.resourcewatch.org/.test(this.props.referer);
  }

  constructor(props) {
    super(props);
    this.state = {
      isLoading: props.isLoading,
      tableData: []
    };
  }

  componentDidMount() {
    this.props.getWidget(this.props.url.query.id);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.widget && nextProps.widget.attributes) {
      const queryURL = nextProps.widget.attributes.widgetConfig.data[0].url;
      this.loadTableData(queryURL);
    }
  }

  loadTableData(query) {
    fetch(query)
      .then(response => response.json())
      .then((response) => {
        this.setState({
          isLoading: false,
          tableData: response.data
        });
      })
      .catch((error) => {
        this.setState({ isLoading: false });
        console.error(error);
      });
  }

  render() {
    const { widget } = this.props;
    const { isLoading, tableData } = this.state;

    const header = tableData && tableData.length > 0 && Object.keys(tableData[0]);

    if (isEmpty(tableData)) {
      return (
        <EmbedLayout
          title={'Loading widget...'}
          description={''}
        >
          <Spinner isLoading className="-light" />
        </EmbedLayout>
      );
    }

    return (
      <EmbedLayout
        title={`${widget.attributes.name}`}
        description={`${widget.attributes.description || ''}`}
      >
        <div className="c-embed-widget">
          <div className="visualization">
            <Spinner isLoading={isLoading} className="-light" />
            <div className="widget-title">
              <h4>{widget.attributes.name}</h4>
            </div>
            <div className="widget-content">
              {tableData &&
                <table>
                  <thead>
                    <tr>
                      {header && header.map(val => (
                        <th
                          key={`header_${val}`}
                        >
                          {val}
                        </th>
                      ))
{'fraction_non_alphanumeric': 0.13588624979802877, 'fraction_numerical': 0.0016157699143641946, 'mean_word_length': 2.1694828469022016, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 46, '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': '2736012', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1756, 'n_words': 351}