Skip to content

Commit

Permalink
Changes for #183 (#213)
Browse files Browse the repository at this point in the history
* changes for adding state, district and city filter

* changes after merge

* final changes

Co-authored-by: ashishdawale20 <[email protected]>
  • Loading branch information
ashishdawale20 and ashishdawale20 authored Apr 13, 2020
1 parent db40d29 commit 47ed525
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 50 deletions.
28 changes: 16 additions & 12 deletions components/FilterPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from 'styled-components'

import { connect } from 'react-redux'
import _ from 'lodash'

import normalize from '../../util/normalize'
import { state, city, abroad, p2p } from '../../images/index'
import {
addStates,
Expand All @@ -13,7 +13,8 @@ import {
addTravel,
removeTravel,
} from '../../util/filters'
import { updateGraph, selectFilter } from '../Redux/actions'
import { updateGraph, selectFilter, updatePatients } from '../Redux/actions'
import { rowsToGraph } from '../../util/parse'

const filters = [
{
Expand Down Expand Up @@ -98,11 +99,11 @@ const FilterCategory = ({ filter, onClick, selected }) => {

const FilterPanel = ({
graph,
patients,
updateGraph,
selectFilter,
filter,
states
states,
rawPatients
}) => {
// const [selected, selectCategory] = React.useState('P2P')

Expand All @@ -114,14 +115,17 @@ const FilterPanel = ({
let choosenFilter = _.find(filters, function(o) {
return o.name === name
})

let newGraph = currentFilter.remove(graph, patients.byId, states)

selectFilter(name)
newGraph = choosenFilter.add(newGraph, patients.byId, states)
console.log(newGraph)
updateGraph(newGraph)
if(name != 'State')
{
var newGraph = rowsToGraph(rawPatients, false, false);
let allPatients = normalize(rawPatients, false);
newGraph = choosenFilter.add(newGraph, allPatients.byId, states);
updateGraph(newGraph)
}
}


const FilterHeader = styled.div`
text-align: center;
text-transform: uppercase;
Expand Down Expand Up @@ -152,8 +156,8 @@ const FilterPanel = ({
}

const mapStateToProps = state => {
const { patients, graph, filter, states } = state
return { graph, patients, filter, states}
const { patients, graph, filter, states,rawPatients } = state
return { graph, patients, filter, states, rawPatients}
}

export default connect(mapStateToProps, { updateGraph, selectFilter })(
Expand Down
22 changes: 13 additions & 9 deletions components/NetworkMap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef, useMemo } from 'react'
import Graph from 'react-graph-vis'
import { Tooltip, TooltipArrow, TooltipInner } from 'styled-tooltip-component'
import { connect, useSelector } from 'react-redux'
import { updateGraph, updatePatients, updateLastRefreshed, selectPatient, updateStates } from '../Redux/actions'
import { updateGraph, updatePatients, updateLastRefreshed, selectPatient, updateStates, updateRawPatients } from '../Redux/actions'
import { rowsToGraph, letterToCode } from '../../util/parse'
import normalize from '../../util/normalize'
import DatePicker from '../DatePicker'
Expand All @@ -18,7 +18,8 @@ const NetworkMap = ({
selectPatient,
height,
width,
states
states,
updateRawPatients
}) => {
const graphRef = useRef()
const [isLoading, setIsLoading] = useState(true)
Expand All @@ -31,6 +32,7 @@ const NetworkMap = ({
searchTerm: state.searchTerm,
selected: state.patient,
}))

useEffect(() => {
if(!states){
fetch('https://api.covid19india.org/state_district_wise.json', {
Expand All @@ -56,13 +58,14 @@ const NetworkMap = ({
})
.then(resp => resp.json())
.then(res => {
updateGraph(rowsToGraph(res.data.rawPatientData))
updatePatients(normalize(res.data.rawPatientData))
updateLastRefreshed(res.data.lastRefreshed)
setIsLoading(false)
updateGraph(rowsToGraph(res.data.rawPatientData, false, true));
updatePatients(normalize(res.data.rawPatientData, true));
updateLastRefreshed(res.data.lastRefreshed);
updateRawPatients(res.data.rawPatientData);
setIsLoading(false);
})
.catch(err => console.log('error', err))
}, [isLoading])
}, [])

useEffect(() => {
// TODO: Figure out a way to make this do-able with patient Id search
Expand Down Expand Up @@ -180,8 +183,8 @@ const NetworkMap = ({
}

const mapStateToProps = state => {
let { graph, searchTerm, filter, states } = state
return { graph, searchTerm, filter, states}
let { graph, searchTerm, filter, states, rawPatientData } = state
return { graph, searchTerm, filter, states, rawPatientData}
}

export default connect(mapStateToProps, {
Expand All @@ -190,4 +193,5 @@ export default connect(mapStateToProps, {
updateStates,
updateLastRefreshed,
selectPatient,
updateRawPatients
})(NetworkMap)
1 change: 1 addition & 0 deletions components/Redux/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export default {
SELECT_FILTER: 'SELECT_FILTER',
SEARCH: 'SEARCH',
UPDATE_STATES: 'UPDATE_STATES',
UPDATE_RAW_PATIENTS: 'UPDATE_RAW_PATIENTS',
}
12 changes: 10 additions & 2 deletions components/Redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const updatePatients = patients => (dispatch, getState) => {
})
}

const updateRawPatients = rawPatients => (dispatch, getState) => {
// Dispatch the result.
dispatch({
type: actionTypes.UPDATE_RAW_PATIENTS,
payload: {
rawPatients: rawPatients,
},
})
}
const updateLastRefreshed = lastRefreshed => (dispatch, getState) => {
// Dispatch the result.
dispatch({
Expand All @@ -46,7 +55,6 @@ const selectPatient = patient => (dispatch, getState) => {
}

const selectFilter = filter => (dispatch, getState) => {
// Dispatch the result.
dispatch({
type: actionTypes.SELECT_FILTER,
payload: {
Expand Down Expand Up @@ -75,4 +83,4 @@ const updateStates = states => (dispatch, getState) => {
}

// Export the actions.
export { updateGraph, updatePatients, updateLastRefreshed, selectPatient, selectFilter, setSearchTerm, updateStates}
export { updateGraph, updatePatients, updateRawPatients, updateLastRefreshed, selectPatient, selectFilter, setSearchTerm, updateStates}
9 changes: 7 additions & 2 deletions components/Redux/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const initialState = {
graph: null,
patients: null,
searchTerm: '',
states: null
states: null,
rawPatients: null
}

// Export the Device Reducer.
Expand All @@ -35,7 +36,11 @@ export default (state = initialState, action) => {
}
case actionTypes.UPDATE_PATIENTS: {
const { patients } = action.payload
return { ...state, patients: patients, patient: patients.byId[251] } // `P1` in code
return { ...state, patients: patients, patient: patients.byId[patients.allIds[0]] } // `P1` in code
}
case actionTypes.UPDATE_RAW_PATIENTS: {
const { rawPatients } = action.payload
return { ...state, rawPatients: rawPatients }
}
case actionTypes.UPDATE_LAST_REFRESHED: {
const { lastRefreshed } = action.payload
Expand Down
188 changes: 188 additions & 0 deletions components/SidePanel/extendedfilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import React, {useState, useEffect} from 'react';
import styled from 'styled-components'
import { updateRawPatients, updatePatients, updateGraph } from '../Redux/actions'
import { connect } from 'react-redux'
import normalize from '../../util/normalize'
import { rowsToGraph } from '../../util/parse'
import Select from 'react-select';
import {
addStates,
removeStates,
addCities,
removeCities,
addTravel,
removeTravel,
} from '../../util/filters'

function filterByObject(obj, filters) {
const keys = Object.keys(filters);
return obj.filter((p) => {
return keys.every((key) => {
if (!filters[key].length) return true;
return p[key] === filters[key];
});
});
}


const ExtendedFilter = ({rawPatients,
updateGraph,
updatePatients,
graph,
patients,
states
}) =>
{
const [stateOptions, setstateOptions] = useState([]);
const [districtOptions, setdistrictOptions] = useState([]);
const [cityOptions, setCityOptions] = useState([]);
const Title = styled.div`
text-transform: uppercase;
font-weight: bold;
font-size: 14px;
color: #858383;
`
const [filters, setFilters] = useState({
state: '',
district: '',
city: ''
});

const addFilters = [
{ name: 'State', add: addStates, remove: removeStates },
{ name: 'City', add: addCities, remove: removeCities },
{ name: 'Travel', add: addTravel, remove: removeTravel },
]
const handleFilters = (label, value) => {
setFilters((f) => {
// Create new object (deep copy)
const newFilters = {...f};
newFilters[label] = value;
if (label === 'state') {
const district = document.getElementById('dvdistrict');
const city = document.getElementById('dvcity');
setdistrictOptions(getSortedValues(filterByObject(rawPatients, {
state: value,
}), 'district'));
// Hide boxes
if (value === '')
{
district.style.display = 'none';
}
else
{
district.style.display = 'block';
}
city.style.display = 'none';
// Default to empty selection
district.selectedIndex = 0;
city.selectedIndex = 0;
newFilters['district'] = '';
newFilters['city'] = '';
updatePatients(normalize(rawPatients.filter(x => x.state == value), false));
var newGraph = rowsToGraph(rawPatients.filter(x => x.state == value), false, false);
let filterState = [];
filterState[value] = states[value];
newGraph = addStates(newGraph, rawPatients.filter(x => x.state == value), filterState);
updateGraph(newGraph);
} else if (label === 'district') {
const city = document.getElementById('dvcity');
setdistrictOptions(getSortedValues(filterByObject(rawPatients, {
state: filters.state,
district: value,
}), 'city'));
console.log(value);
// Hide box
if (value === '')
{
city.style.display = 'none'
}
else
{
city.style.display = 'block';
}
// Default to empty selection
city.selectedIndex = 0;
newFilters['city'] = '';
updatePatients(normalize(rawPatients.filter(x => x.district == value), false));
updateGraph(rowsToGraph(rawPatients.filter(x => x.district == value), false, false));
}
else if(label == 'city')
{
updatePatients(normalize(rawPatients.filter(x => x.city == value), false));
updateGraph(rowsToGraph(rawPatients.filter(x => x.city == value), false, false));
}

return newFilters;
}
)
};

useEffect(() => {
setstateOptions(getSortedValues(rawPatients, 'state'));
},[]);

function getSortedValues(obj, key) {
const setValues = new Set(obj.map((p) => p[key]));
let returnValue = Array.from(setValues).sort().map((x, i) =>
{
return {'value': i, 'label':x};
})
return returnValue;
}

return (

<div className="filters-left">
<br></br>
<Title>
State, District and city filter:
</Title>
<br></br>
<div className="select">
<Select
id="state"
onChange={(value) => {
handleFilters('state', value.label);}}
options={stateOptions}
isClearable={true}
defaultValue={{ label: 'Select State', value: -1 }}

/>
<div id="dvdistrict" style={{display: "none", "margin-top": "10px"}}>
<Select
id="district"
onChange={(value) => {
handleFilters('district', value.label);}}
options={districtOptions}
isClearable={true}
defaultValue={{ label: 'Select District', value: -1 }}
/>
</div>
<div id="dvcity" style={{display: "none", "margin-top": "10px"}}>
<Select
id="city"
onChange={(value) => {
handleFilters('city', value.label);}}
options={districtOptions}
isClearable={true}
defaultValue={{ label: 'Select City', value: -1 }}
/>
</div>
<br></br>
<hr></hr>
</div>
</div>
)
}
const mapStateToProps = state => {
let { rawPatients, patients, graph, states} = state
return { rawPatients, patients, graph, states}
}

export default connect(mapStateToProps, {
updateRawPatients,
updatePatients,
updateGraph
})(ExtendedFilter)

Loading

1 comment on commit 47ed525

@vercel
Copy link

@vercel vercel bot commented on 47ed525 Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.