This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
weather-map.tsx
204 lines (185 loc) · 6.59 KB
/
weather-map.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import Alert from 'antd/es/alert';
import Col from 'antd/es/col';
import Row from 'antd/es/row';
import Spin from 'antd/es/spin';
import { isEmpty, isUndefined } from 'lodash';
import * as React from 'react';
import { useEffect, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getGeocode } from '../api';
import { ApiKey } from '../constants/api-key';
import { USE_DEFAULT_LOCATION } from '../constants/message';
import { GeoCode, RootState, WeatherMapState } from '../constants/types';
import { getWeatherData } from '../store/actions';
const usePrevious = (value: any) => {
const ref = useRef<any>();
useEffect(() => {
ref.current = value;
});
return ref.current;
};
export const WeatherMap: React.FC<any> = () => {
const dispatch = useDispatch();
const filter = useSelector((state: RootState) => state.weather.filter);
const location = useSelector((state: RootState) => state.weather.location);
const timezone = useSelector((state: RootState) => state.weather.timezone);
const [searchedLocation, setSearchedLocation] = React.useState(filter.searchedLocation);
const [weatherMapState, setWeatherMapState] = React.useState<WeatherMapState>({
latitude: 0,
longitude: 0,
location: '',
isLoading: false,
error: '',
});
const prevState = usePrevious(weatherMapState);
const renderMap = () => {
try {
const weatherMap = document.getElementById('windy');
weatherMap.parentNode.removeChild(weatherMap);
} catch (err) {
console.log('map does not exist');
}
const divElement: HTMLDivElement = document.createElement('div');
divElement.setAttribute('id', 'windy');
divElement.setAttribute('class', 'windy');
document.getElementById('weather-map-wrapper').appendChild(divElement);
const options = {
key: ApiKey.windy,
lat: weatherMapState.latitude,
lon: weatherMapState.longitude,
};
windyInit(options, (windyAPI: any) => {
const { map } = windyAPI;
map.options.minZoom = 4;
map.options.maxZoom = 17;
const topLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
minZoom: 12,
maxZoom: 17,
}).addTo(map);
topLayer.setOpacity('0');
map.on('zoomend', () => {
if (map.getZoom() >= 12) {
topLayer.setOpacity('1');
} else {
topLayer.setOpacity('0');
}
});
map.setZoom(10);
L.popup()
.setLatLng([weatherMapState.latitude, weatherMapState.longitude])
.setContent(weatherMapState.location)
.openOn(map);
});
};
const fetchLatitudeAndLongitude = async (lat: number, lon: number, city: string) => {
if (lat !== 0 && lon !== 0) {
setWeatherMapState({
latitude: lat,
longitude: lon,
location: city,
isLoading: false,
error: '',
});
} else {
try {
const geocode: GeoCode = await getGeocode(null, null, city);
if (geocode.status === 'OK') {
setWeatherMapState({
latitude: geocode.latitude,
longitude: geocode.longitude,
location: geocode.address,
isLoading: false,
error: '',
});
dispatch(getWeatherData(geocode.latitude, geocode.longitude, geocode.address));
}
} catch (error) {
setWeatherMapState({ ...weatherMapState, error: error.message });
}
}
};
/**
* Only be called when error occurs
* @param {string} message
*/
const searchByDefaultLocation = (message: string) => {
setWeatherMapState({ ...weatherMapState, error: message });
setTimeout(async () => {
await fetchLatitudeAndLongitude(-36.8484597, 174.7633315, 'Auckland');
}, 5000);
};
// Do initialise data, get user's location at first
useEffect(() => {
const { latitude, longitude } = timezone || {};
if (isUndefined(latitude) || isUndefined(longitude)) {
setWeatherMapState({ ...weatherMapState, isLoading: true });
// Get user's coordinates when user access the web app, it will ask user's location permission
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
};
const handleLocation = async (location: any) => {
try {
const geocode: GeoCode = await getGeocode(location.coords.latitude, location.coords.longitude, '');
if (geocode.status === 'OK') {
setWeatherMapState({
latitude: geocode.latitude,
longitude: geocode.longitude,
location: geocode.address,
isLoading: false,
error: '',
});
renderMap();
dispatch(getWeatherData(geocode.latitude, geocode.longitude, geocode.address));
}
} catch (error) {
searchByDefaultLocation(`${error.message}.${USE_DEFAULT_LOCATION}`);
}
};
const handleError = (error: any) => searchByDefaultLocation(`${error.message}.${USE_DEFAULT_LOCATION}`);
if (process.env.NODE_ENV === 'development') {
searchByDefaultLocation(USE_DEFAULT_LOCATION);
} else {
navigator.geolocation.getCurrentPosition(handleLocation, handleError, options);
}
} else {
setWeatherMapState({ ...weatherMapState, latitude, longitude, location });
}
}, []);
useEffect(() => {
if (
weatherMapState.latitude !== 0 &&
weatherMapState.longitude !== 0 &&
(weatherMapState.latitude !== prevState.latitude || weatherMapState.longitude !== prevState.longitude)
) {
renderMap();
}
if (filter.searchedLocation !== searchedLocation) {
setWeatherMapState({ ...weatherMapState, isLoading: true });
fetchLatitudeAndLongitude(0, 0, filter.searchedLocation);
setSearchedLocation(filter.searchedLocation);
}
});
return (
<div>
{weatherMapState.isLoading ? (
<Row justify='center' className='fetching-weather-content'>
<Spin className='fetching-weather-spinner' size='large' />
<h2 className='loading-text'>Fetching location...</h2>
</Row>
) : !isEmpty(weatherMapState.error) ? (
<div>
<Row justify='center' className='fetching-weather-content'>
<Col xs={24} sm={24} md={18} lg={16} xl={16}>
<Alert message='Error' description={weatherMapState.error} type='error' showIcon={true} />
</Col>
</Row>
</div>
) : (
<div id='weather-map-wrapper' />
)}
</div>
);
};