diff --git a/src/components/TurkHit/index.js b/src/components/TurkHit/index.js index c2f8e720eba8bf24e1860fb7ff5e66f307863a00..6a236d5d124a83ac24f97d488a21fe0a260b5146 100644 --- a/src/components/TurkHit/index.js +++ b/src/components/TurkHit/index.js @@ -6,7 +6,6 @@ import defaultForm from './defaultForm'; import './styles.css'; import featuresDict from './features'; import turkHitPropTypes from '../../containers/Dimensions/propTypes'; -import documentsPropType from '../../containers/Documents/propTypes'; import turkStatus from './turkStatus'; // Need to call this to load the 3D highcharts module @@ -33,7 +32,7 @@ class TurkHit extends Component { componentDidUpdate() { // Create the chart when the component updates with turk data const { hit } = this.props; - if (hit == null) { + if (hit === null || hit.status === null) { return; } const status = hit.status; @@ -356,6 +355,190 @@ class TurkHit extends Component { this.setState({ displayChart: !this.state.displayChart }); } + // Generate the href to download mech turk data + generateBillDownload = (dimensions) => { + const FEATURE_DICT = { + 1: 'Door', + 2: 'Window', + 3: 'Building Point', + 4: 'Roof Point', + 5: 'Adjacent Building 1 Point', + 6: 'Adjacent Building 2 Point', + 7: 'Adjacent Building 3 Point', + }; + + const DIRECTION_DICT = { + 1: 'SOUTH', + 2: 'EAST', + 3: 'WEST', + 4: 'NORTH', + }; + // The building dimensions Data + let csvString = '' + + 'building_id,' + + 'hit_id,' + + 'number of floors,' + + 'perimeter,' + + 'area,' + + ''; + csvString += '\n'; + csvString += `${dimensions.building_dimensions.building_id},`; + csvString += `${dimensions.building_dimensions.hit_id},`; + csvString += `${dimensions.building_dimensions.num_floors},`; + csvString += `${dimensions.building_dimensions.perimeter},`; + csvString += `${dimensions.building_dimensions.area},`; + + const pointLines = { + buildingPoint: '', + roofPoint: '', + adjacentPoint1: '', + adjacentPoint2: '', + adjacentPoint3: '', + }; + dimensions.points.map((val) => { + let line = ''; + line += `${val.latitude},`; + line += `${val.longitude},`; + line += `${val.elevation},`; + switch (FEATURE_DICT[val.feature]) { + case 'Building Point': + line += `${val.wall_num_first},`; + line += `${val.wall_num_second},`; + pointLines.buildingPoint += `${line}\n`; + break; + case 'Roof Point': + pointLines.roofPoints += `${line}\n`; + break; + case 'Adjacent Building 1 Point': + pointLines.adjacentPoint1 += `${line}\n`; + break; + case 'Adjacent Building 2 Point': + pointLines.adjacentPoint2 += `${line}\n`; + break; + case 'Adjacent Building 3 Point': + pointLines.adjacentPoint3 += `${line}\n`; + break; + default: + break; + } + return val; + }); + + csvString += '\n'; + csvString += '\n'; + csvString += 'Building Points,'; + csvString += '\n'; + csvString += '' + + 'latitude,' + + 'longitude,' + + 'elevation,' + + 'first wall,' + + 'second wall,' + + ''; + csvString += '\n'; + csvString += pointLines.buildingPoint; + csvString += '\n'; + csvString += '\n'; + csvString += 'Roof Points,'; + csvString += '\n'; + csvString += '' + + 'latitude,' + + 'longitude,' + + 'elevation,' + + ''; + csvString += '\n'; + csvString += pointLines.roofPoint; + csvString += '\n'; + csvString += '\n'; + csvString += 'Adjacent Building 1 Points,'; + csvString += '\n'; + csvString += '' + + 'latitude,' + + 'longitude,' + + 'elevation,' + + ''; + csvString += '\n'; + csvString += pointLines.adjacentPoint1; + csvString += '\n'; + csvString += '\n'; + csvString += 'Adjacent Building 2 Points,'; + csvString += '\n'; + csvString += '' + + 'latitude,' + + 'longitude,' + + 'elevation,' + + ''; + csvString += '\n'; + csvString += pointLines.adjacentPoint2; + csvString += '\n'; + csvString += '\n'; + csvString += 'Adjacent Building 3 Points,'; + csvString += '\n'; + csvString += '' + + 'latitude,' + + 'longitude,' + + 'elevation,' + + ''; + csvString += '\n'; + csvString += pointLines.adjacentPoint3; + + const windowDoorLines = { + doorLines: '', + windowLines: '', + }; + dimensions.windows_doors.map((val) => { + let line = ''; + line += `${val.height},`; + line += `${val.width},`; + line += `${val.quantity},`; + line += `${DIRECTION_DICT[val.orientation]},`; + line += `${val.wall_num},`; + switch (FEATURE_DICT[val.feature]) { + case 'Window': + windowDoorLines.windowLines += `${line}\n`; + break; + case 'Door': + windowDoorLines.doorLines += `${line}\n`; + break; + default: + break; + } + return val; + }); + csvString += '\n'; + csvString += '\n'; + csvString += 'Windows,'; + csvString += '\n'; + csvString += '' + + 'height,' + + 'width,' + + 'quantity,' + + 'direction,' + + 'wall number,' + + ''; + csvString += '\n'; + csvString += windowDoorLines.windowLines; + csvString += '\n'; + csvString += '\n'; + csvString += 'Doors,'; + csvString += '\n'; + csvString += '' + + 'height,' + + 'width,' + + 'quantity,' + + 'direction,' + + 'wall number,' + + ''; + csvString += '\n'; + csvString += windowDoorLines.doorLines; + + const mimeType = 'text/csv;encoding:utf-8'; + const href = URL.createObjectURL(new Blob([csvString], { + type: mimeType, + })); + return href; + } + renderDefinitions = () => (

Mechanical Turk

@@ -370,25 +553,14 @@ class TurkHit extends Component {
); - renderDownloadLink = (documentKey) => { - const dimensions = this.props.documents.files.buildingDimensions; - const document = dimensions.reduce((acc, val) => { - if (!acc) { - if (val.key === documentKey) { - return val; - } - } - return acc; - }, null); - if (this.props.documents.loading) { - return 'Loading documents...'; - } - if (document) { + renderDownloadButton = (dimensions) => { + if (dimensions != null && dimensions.building_dimensions != null) { return (
Download @@ -467,7 +639,7 @@ class TurkHit extends Component {
- {this.renderDownloadLink(val.csv_document_key)} + {this.renderDownloadButton(val.dimensions)}
); @@ -549,7 +721,7 @@ class TurkHit extends Component {
- {currStatus.downloadLink && this.renderDownloadLink(curHit.csv_document_key)} + {currStatus.downloadLink && this.renderDownloadButton(curHit.dimensions)} {this.state.chart && this.renderChartToggleButton()} {currStatus.fileActions && this.renderHitActions(curHit)} {currStatus.expireable && this.renderDeleteHitButton(curHit)} @@ -604,7 +776,6 @@ TurkHit.propTypes = { address: PropTypes.string, building_id: PropTypes.number, hit: turkHitPropTypes, - documents: documentsPropType, }; export default TurkHit; diff --git a/src/containers/Dimensions/index.js b/src/containers/Dimensions/index.js index d8ae462117c438abf8843ae17ed59f22562b3677..70186f83cde374494174f0431728dc937ebeb79f 100644 --- a/src/containers/Dimensions/index.js +++ b/src/containers/Dimensions/index.js @@ -2,7 +2,6 @@ import React, { Component, PropTypes } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import buildingDetailPropTypes, { completeOverviewPropTypes } from '../../containers/Building/propTypes'; -import documentsPropType from '../Documents/propTypes'; import { loadHit, @@ -11,10 +10,6 @@ import { deleteHit, } from './actions'; -import { - loadDocuments, -} from '../Documents/actions'; - import turkHitPropTypes from './propTypes'; import './styles.css'; @@ -26,21 +21,6 @@ class Dimensions extends Component { this.props.loadHit(this.props.buildingId, this.props.building.address); } - componentDidUpdate(prevProps) { - const curHitData = this.props.dimensions.hit.hitData; - const prevHitData = prevProps.dimensions.hit.hitData; - if (curHitData !== prevHitData) { - let docKeys = curHitData.map(val => (val.csv_document_key)); - // Reduce docKeys to remove null entries - docKeys = docKeys.filter(val => ( - val - )); - if (docKeys.length > 0) { - this.props.loadDocuments([], docKeys, 'buildingDimensions'); - } - } - } - createHitConfirmation = (form) => { // TODO: Remove alert, create dialog box /* eslint-disable no-alert */ @@ -64,7 +44,6 @@ class Dimensions extends Component { USA`} building_id={parseInt(this.props.buildingId, 10)} hit={this.props.dimensions.hit} - documents={this.props.documents} />
); @@ -82,8 +61,6 @@ Dimensions.propTypes = { createHit: PropTypes.func, deleteHit: PropTypes.func, hitDecision: PropTypes.func, - documents: documentsPropType, - loadDocuments: PropTypes.func, }; function mapDispatchToProps(dispatch) { @@ -92,12 +69,11 @@ function mapDispatchToProps(dispatch) { createHit, deleteHit, hitDecision, - loadDocuments, }, dispatch); } -function mapStateToProps({ dimensions, buildingDetail, documents }) { - return { dimensions, buildingDetail, documents }; +function mapStateToProps({ dimensions, buildingDetail }) { + return { dimensions, buildingDetail }; } export default connect(mapStateToProps, mapDispatchToProps)(Dimensions); diff --git a/src/containers/Dimensions/reducer.js b/src/containers/Dimensions/reducer.js index 333b762b3c84b4c2167ee2636ee8faf56f0466cb..59ab72899ccc1e7933b4f62c38287adde65d338c 100644 --- a/src/containers/Dimensions/reducer.js +++ b/src/containers/Dimensions/reducer.js @@ -126,7 +126,11 @@ export default function (state = initState, action) { hit: { ...state.hit, hitData: [ - action.payload, + { + // Dimensions data is not returned by the PUT endpoint + dimensions: state.hit.hitData[0].dimensions, + ...action.payload, + }, ...state.hit.hitData.slice(1), ], status: action.payload.status_id,