I've been working on a crime map demo - and I am keen to learn more about how to rewrite this application to use redux. What is the best approach, why - what is wrong with the way its currently written - too reliant on jQuery?
What would be the advantage of using redux - to keep component data available for other sister components - master parent components?
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
var $ = require("jquery");
import MapChart from './modules/mapChart/MapChart';
var CrimeMap = React.createClass({
getInitialState: function() {
return {
result: ''
};
},
componentDidMount: function () {
this.serverRequest = $.get(this.props.source, function (result) {
this.setState({
result: result
});
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
getLayers: function(data){
var items = [];
items.push( <MapChart
key="1"
data= {data} /> );
return items;
},
render: function () {
var result = this.state.result;
return (
<div className="apps">
{this.getLayers(result)}
</div>
);
}
});
//https://data.police.uk/docs/method/crime-street/
ReactDOM.render(
<CrimeMap source="https://data.police.uk/api/crimes-street/all-crime?poly=52.268,0.543:52.794,0.238:52.130,0.478&date=2013-01" />,
document.getElementById('root')
);
//MapChart.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
var $ = require("jquery");
import './MapChart.css';
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';
export class MapContainer extends Component {
render() {
function getIcon(category){
//anti-social-behaviour
//burglary
//criminal-damage-arson
//drugs
//other-theft
//shoplifting
//vehicle-crime
//other-crime
switch(category) {
case "anti-social-behaviour":
return 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
break;
case "burglary":
return 'http://maps.google.com/mapfiles/ms/icons/red-dot.png'
break;
case "criminal-damage-arson":
return 'http://maps.google.com/mapfiles/ms/icons/yellow-dot.png'
break;
case "drugs":
return 'http://maps.google.com/mapfiles/ms/icons/purple-dot.png'
break;
case "other-theft":
return 'http://maps.google.com/mapfiles/ms/icons/pink-dot.png'
break;
case "shoplifting":
return 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
break;
case "other-crime":
return 'http://maps.google.com/mapfiles/ms/icons/orange-dot.png'
break;
default:
return 'http://maps.google.com/mapfiles/ms/icons/ltblue-dot.png'
}
}
const data = [];
$.each(this.props.data, function( index, value ) {
var obj = {
"label": value.category,
"lat": value.location.latitude,
"lng": value.location.longitude,
"icon": getIcon(value.category)
}
data.push(obj);
});
return (
<Map
google={this.props.google}
initialCenter={{
lat: 52.225827,
lng: 0.484861
}}
zoom={11}
>
{
data.map(function (item, index) {
return (
<Marker
key={index}
icon={item.icon}
title={item.label}
name={item.label}
position={{lat: item.lat, lng: item.lng}} />
)
})
}
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: 'xx'
})(MapContainer)
//MapChart.css
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}