<commit_before>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2018
 */

import React, { useState, useEffect } from 'react';
import { View, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import globalStyles from '../globalStyles';
import { testData } from '../database/utilities/ReportDemoData';
import { ReportSideBar } from './ReportSideBar';
import { ReportChart } from './ReportChart';
import { createOrUpdateRecord } from '../sync/incomingSyncUtils';

export const DashboardPage = ({ database, settings }) => {
  const [reports, setReports] = useState(null);
  const [selectedItemIndex, setSelectedItemIndex] = useState(0);

  // Used for adding data within ./ReportDemoData to the database for
  // testing while syncing from mobile is not functional.
  database.write(() =>
    testData.forEach(report => {
      const recordCopy = { ...report };
      recordCopy.data = JSON.stringify(report.data);
      createOrUpdateRecord(database, settings, 'Report', recordCopy);
    })
  );

  useEffect(() => {
    setReports(
      database
        .objects('Report')
        .snapshot()
        .map((report, index) => ({
          id: report.reportID,
          index,
          title: report.title,
          type: report.type,
          date: report.date,
          data: JSON.parse(report.data),
        }))
    );
  });

  const onPressItem = id => {
    if (selectedItemIndex === id) return;
    setSelectedItemIndex(id);
  };

  const { pageContentContainer, container, pageTopSectionContainer } = globalStyles;
  const pageContainer = StyleSheet.flatten([pageTopSectionContainer, { paddingHorizontal: 0 }]);

  if (!reports) return null;
  const report = reports ? reports[selectedItemIndex] : null;
  return (
    <View style={pageContentContainer}>
      <View style={container}>
        <View style={pageContainer}>
          <ReportSideBar
            data={reports}
            onPressItem={onPressItem}
            selectedItemIndex={selectedItemIndex}
            dimensions={localStyles.sidebarDimensions}
          />
          <ReportChart report={report} />
        </View>
      </View>
    </View><commit_msg>Change UseEffect and remove map<commit_after>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2018
 */

import React, { useState, useEffect } from 'react';
import { View, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import globalStyles from '../globalStyles';
import { testData } from '../database/utilities/ReportDemoData';
import { ReportSideBar } from './ReportSideBar';
import { ReportChart } from './ReportChart';
import { createOrUpdateRecord } from '../sync/incomingSyncUtils';

export const DashboardPage = ({ database, settings }) => {
  const [reports, setReports] = useState(null);
  const [selectedItemIndex, setSelectedItemIndex] = useState(0);

  // Used for adding data within ./ReportDemoData to the database for
  // testing while syncing from mobile is not functional.
  database.write(() =>
    testData.forEach(report => {
      const recordCopy = { ...report };
      recordCopy.data = JSON.stringify(report.data);
      createOrUpdateRecord(database, settings, 'Report', recordCopy);
    })
  );

  useEffect(() => {
    setReports(
      database
        .objects('Report')
        .snapshot()
        .then((report, index) => ({
          id: report.reportID,
          index,
          title: report.title,
          type: report.type,
          date: report.date,
          data: JSON.parse(report.data),
        }))
    );
  }, [reports]);

  const onPressItem = id => {
    if (selectedItemIndex === id) return;
    setSelectedItemIndex(id);
  };

  const { pageContentContainer, container, pageTopSectionContainer } = globalStyles;
  const pageContainer = StyleSheet.flatten([pageTopSectionContainer, { paddingHorizontal: 0 }]);

  if (!reports) return null;
  const report = reports ? reports[selectedItemIndex] : null;
  return (
    <View style={pageContentContainer}>
      <View style={container}>
        <View style={pageContainer}>
          <ReportSideBar
            data={reports}
            onPressItem={onPressItem}
            selectedItemIndex={selectedItemIndex}
            dimensions={localStyles.sidebarDimensions}
          />
          <ReportChart report={report} />
        </View>
      </View>
    </View>
{'fraction_non_alphanumeric': 0.12786960514233242, 'fraction_numerical': 0.0027548209366391185, 'mean_word_length': 2.9465579710144927, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '3923913', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1181, 'n_words': 298}