Commit ec5a9f24 authored by jvfpw18's avatar jvfpw18

Add csv download to AgreementsInExecution

parent 03b712cc
......@@ -63,6 +63,10 @@ class Table extends Component {
var header = this.loadHeader();
if (data) this.setState({ data: data });
if (header) this.setState({ headers: header });
if (this.props.getCsv) {
this.dataToCsv();
}
}
componentDidUpdate(prevProps) {
......@@ -72,6 +76,12 @@ class Table extends Component {
if (data) this.setState({ data: data });
if (header) this.setState({ headers: header });
}
// Update CSV data
if (this.props.getCsv && JSON.stringify(this.props.body) !== JSON.stringify(prevProps.body)) {
this.dataToCsv();
}
prevProps = null;
}
......@@ -177,6 +187,34 @@ class Table extends Component {
}
return (body);
}
dataToCsv() {
let data = this.props.body;
console.log(data, this.props)
let csvTable = '';
if (data[0] !== undefined) {
let headerList = this.props.header;
let header = '';
headerList.forEach(h => {
header += '"' + h + '",';
})
header.slice(0, -1)
csvTable = "data:text/csv;charset=utf-8," + header + '\n';
data.forEach(row => {
row.forEach(item => {
if (item !== null)
csvTable += '"' + item + '"';
csvTable += ',';
})
csvTable = csvTable.slice(0, -1) + '\n'
})
csvTable = encodeURI(csvTable);
}
this.props.getCsv(csvTable);
}
}
export default Table;
......
......@@ -30,6 +30,7 @@ import './Styles/AgreementsInExecution.css'
import { getRegionState, getStateAcronym } from '../enviroment';
import Collapsible from 'react-materialize/lib/Collapsible';
import CollapsibleContainer from '../Components/CollapsibleContainer';
import CardFooter from '../Components/CardFooter'
// /**
// * Contains adherence level of states and cities to the SINAPIR.<br />
......@@ -48,9 +49,13 @@ class AgreementsInExecution extends Component {
state: null,
area: null,
clickedInMap: null,
municipiosCsv: '',
contatosCsv: ''
}
this.handleMapClick = this.handleMapClick.bind(this);
this.handleFilterChange = this.handleFilterChange.bind(this);
this.conveniosCsv = this.conveniosCsv.bind(this);
this.emendasCsv = this.emendasCsv.bind(this);
}
formatData(objectConvenios) {
......@@ -140,6 +145,14 @@ class AgreementsInExecution extends Component {
return this.formatData(data);
}
conveniosCsv(csv) {
this.setState({ conveniosCsv: csv });
}
emendasCsv(csv) {
this.setState({ emendasCsv: csv});
}
render() {
let contrastString = this.context.state.contrast;
return (
......@@ -200,8 +213,10 @@ class AgreementsInExecution extends Component {
body={this.conveniosTable(conveniosJson)}
classes="striped agreementsTableCells"
divClasses="agreementsTable"
getCsv={this.conveniosCsv}
pageSi>
</Table>
<CardFooter data={this.state.conveniosCsv} source=" Sistema de Convênios - SICONV"></CardFooter>
</CollapsibleContainer>
</Collapsible>
</Col>
......@@ -217,8 +232,10 @@ class AgreementsInExecution extends Component {
body={this.conveniosTable(emendasJson)}
classes="striped agreementsTableCells"
divClasses="agreementsTable"
getCsv={this.emendasCsv}
pageSi>
</Table>
<CardFooter data={this.state.emendasCsv} source=" Sistema de Convênios - SICONV"></CardFooter>
</CollapsibleContainer>
</Collapsible>
</Col>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment