Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lost And Found .... Locate me #234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/assets/images/icons/locate.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ function SocialMediaButtons({ classes, city }) {
<Grid item className={classes.twitter}>
<TwitterShareButton
url="https://sensors.AFRICA/air"
title={`Did you know the #AirQuality in ${city.name} directly affects my health ${city.twitterHandle}? Check our city’s pollution levels on `}
title={`Did you know the #AirQuality in ${
city.name
} directly affects my health ${
city.twitterHandle
}? Check our city’s pollution levels on `}
via="sensorsAFRICA"
hashtags={['sensorsAFRICA']}
className={classes.buttonLink}
Expand Down
1 change: 1 addition & 0 deletions src/components/DocumentHead/PageHeads.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const URLS = {
ABOUT: '/about',

AIR: {
LOCATE_ME: '/locate-me',
HOME: '/air',
ABOUT: '/air/about',
DATA: '/air/data',
Expand Down
4 changes: 3 additions & 1 deletion src/components/Embeds/AirMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function AirMap({ location }) {
const city = params.get('city');
return (
<iframe
src={`https://v2.map.aq.sensors.africa/#${CITIES_LOCATION[city].zoom}/${CITIES_LOCATION[city].latitude}/${CITIES_LOCATION[city].longitude}`}
src={`https://v2.map.aq.sensors.africa/#${CITIES_LOCATION[city].zoom}/${
CITIES_LOCATION[city].latitude
}/${CITIES_LOCATION[city].longitude}`}
name={`sensors-map-${CITIES_LOCATION[city].slug}`}
title={`sensors.AFRICA | ${CITIES_LOCATION[city].name} Sensor Map`}
scrolling="no"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const styles = theme => ({
position: 'absolute'
},
gaugeWhoGuidelineText: {
font: `bold ${theme.typography.body1.fontSize} ${theme.typography.h6.fontFamily}`,
font: `bold ${theme.typography.body1.fontSize} ${
theme.typography.h6.fontFamily
}`,
fill: 'white'
},
gaugeNeedleItem: {
Expand Down
90 changes: 80 additions & 10 deletions src/components/SearchBar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';

import Select from 'react-select';

import { MenuItem, Paper, TextField, Typography } from '@material-ui/core';
import {
MenuItem,
Paper,
TextField,
Typography,
Grid
} from '@material-ui/core';
import { withStyles } from '@material-ui/core/styles';
import { CITIES_LOCATION } from '../api';
import locateIcon from '../assets/images/icons/locate.svg';

const styles = theme => ({
root: {
Expand Down Expand Up @@ -132,7 +140,16 @@ Control.defaultProps = {
selectProps: null
};

function Option({ children, innerProps, innerRef, isFocused, isSelected }) {
function Option({
data,
children,
innerProps: { onClick, ...props },
innerRef,
isFocused,
isSelected,
selectOption
}) {
const [label, setLabel] = useState(children);
return (
<MenuItem
buttonRef={innerRef}
Expand All @@ -142,13 +159,65 @@ function Option({ children, innerProps, innerRef, isFocused, isSelected }) {
color: '#fff',
fontWeight: isSelected ? 500 : 400
}}
{...innerProps}
{...props}
onClick={e => {
if (data.value === 'locate-me') {
setLabel('LOCATING...');
navigator.geolocation.getCurrentPosition(
async ({ coords: { latitude, longitude } }) => {
const { error, address } = await fetch(
`https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${latitude}&lon=${longitude}`
).then(res => res.json());

// If not really there
if (error || !address) {
setLabel('COULD NOT LOCATE');
} else {
// Find country
const foundEntry = Object.entries(CITIES_LOCATION).find(
([, country]) => address.country === country.name
);
if (foundEntry) {
const [slug] = foundEntry;
selectOption({ value: slug });
} else {
setLabel(
<p style={{ lineHeight: '1em' }}>
{address.city}, {address.country}
<br />
<b style={{ fontSize: '0.625rem' }}>
Is not covered by sensorsAFRICA
</b>
</p>
);
}
}
},
failure => setLabel(failure.message.toUpperCase()),
{
timeout: 10000
}
);
Comment on lines +166 to +200
Copy link
Member

Choose a reason for hiding this comment

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

Move all these to a function

} else {
onClick(e);
}
}}
>
{children}
<Grid container justify="space-between" alignItems="center">
<Grid item>{label}</Grid>

{data.value === 'locate-me' && (
<Grid item>
<img alt="icon" src={locateIcon} />
</Grid>
)}
</Grid>
</MenuItem>
);
}
Option.propTypes = {
selectOption: PropTypes.func.isRequired,
data: PropTypes.shape({}).isRequired,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
Expand Down Expand Up @@ -275,11 +344,12 @@ const components = {
DropdownIndicator: null
};

const DEFAULT_OPTIONS = [
{ value: 'nairobi', label: 'Nairobi, Kenya' },
{ value: 'lagos', label: 'Lagos, Nigeria' },
{ value: 'dar-es-salaam', label: 'Dar-es-Salaam, Tanzania' }
];
const DEFAULT_OPTIONS = [{ value: 'locate-me', label: 'LOCATE ME' }].concat(
Object.values(CITIES_LOCATION).map(({ slug: value, name: label }) => ({
value,
label
}))
);

class SearchBar extends React.Component {
constructor(props) {
Expand Down
5 changes: 2 additions & 3 deletions src/pages/air/City.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ const styles = () => ({
}
});

const CITY_PATHNAME = '/air/city';

class City extends React.Component {
constructor() {
super();
Expand Down Expand Up @@ -207,7 +205,8 @@ class City extends React.Component {
const searchedCity = (option && option.value) || DEFAULT_CITY;
const { city } = this.state;
if (searchedCity !== city) {
const path = `${CITY_PATHNAME}/${searchedCity}`;
const path =
city === 'locate-me' ? '/air/locate-me' : `/air/city/${searchedCity}`;

const { history } = this.props;
history.push(path);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/air/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Support from '../../components/Support';
import HealthAndClimateImpacts from './HealthAndClimateImpacts';
import Showcase from '../../components/Showcase';

const CITY_PATHNAME = '/air/city';
class AirHome extends React.Component {
constructor(props) {
super(props);
Expand All @@ -28,7 +27,8 @@ class AirHome extends React.Component {
}

handleSearch(city) {
const path = `${CITY_PATHNAME}/${city.value}`;
const path =
city.value === 'locate-me' ? '/air/locate-me' : `/air/city/${city.value}`;
const { history } = this.props;
history.push(path);
}
Expand Down