From 2ea897731cc8d2520661b09a850ef9d919ec3417 Mon Sep 17 00:00:00 2001 From: Conrad Date: Tue, 16 May 2017 14:59:12 -0400 Subject: [PATCH 1/3] Download mech turk csv from database rather than box --- src/components/TurkHit/index.js | 213 +++++++++++++++++++++++++++++--- 1 file changed, 193 insertions(+), 20 deletions(-) diff --git a/src/components/TurkHit/index.js b/src/components/TurkHit/index.js index c2f8e720..0f1ae54c 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,192 @@ class TurkHit extends Component { this.setState({ displayChart: !this.state.displayChart }); } + // Generate the href to download a bill + // Works for disaggregate bill data and normal bill data + generateBillDownload = (dimensions) => { + console.log(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 +555,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.building_dimensions != null) { return (
Download @@ -467,7 +641,7 @@ class TurkHit extends Component {
- {this.renderDownloadLink(val.csv_document_key)} + {this.renderDownloadButton(val.dimensions)}
); @@ -549,7 +723,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 +778,6 @@ TurkHit.propTypes = { address: PropTypes.string, building_id: PropTypes.number, hit: turkHitPropTypes, - documents: documentsPropType, }; export default TurkHit; -- GitLab From d4444b0cf50b7c4a240134400a384d95dd958f0f Mon Sep 17 00:00:00 2001 From: Conrad Date: Tue, 16 May 2017 15:23:48 -0400 Subject: [PATCH 2/3] Remove all document props from dimensions --- src/components/TurkHit/index.js | 6 ++---- src/containers/Dimensions/index.js | 28 ++-------------------------- src/containers/Dimensions/reducer.js | 6 +++++- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/components/TurkHit/index.js b/src/components/TurkHit/index.js index 0f1ae54c..f9309fd6 100644 --- a/src/components/TurkHit/index.js +++ b/src/components/TurkHit/index.js @@ -355,10 +355,8 @@ class TurkHit extends Component { this.setState({ displayChart: !this.state.displayChart }); } - // Generate the href to download a bill - // Works for disaggregate bill data and normal bill data + // Generate the href to download mech turk data generateBillDownload = (dimensions) => { - console.log(dimensions); const FEATURE_DICT = { 1: 'Door', 2: 'Window', @@ -556,7 +554,7 @@ class TurkHit extends Component { ); renderDownloadButton = (dimensions) => { - if (dimensions.building_dimensions != null) { + if (dimensions != null && dimensions.building_dimensions != null) { return (
(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 333b762b..59ab7289 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, -- GitLab From 9e254d385c9a3affd0552ede20e914406d1d59f6 Mon Sep 17 00:00:00 2001 From: Conrad Date: Thu, 18 May 2017 13:50:17 -0400 Subject: [PATCH 3/3] Change comparison to triple equals --- src/components/TurkHit/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/TurkHit/index.js b/src/components/TurkHit/index.js index f9309fd6..6a236d5d 100644 --- a/src/components/TurkHit/index.js +++ b/src/components/TurkHit/index.js @@ -32,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 || hit.status == null) { + if (hit === null || hit.status === null) { return; } const status = hit.status; -- GitLab