From 3d3e2eed8c679a0a88cac349a58dd31cc3f7bd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malak?= Date: Tue, 26 Oct 2021 14:37:01 +0200 Subject: [PATCH] Fixed bug with weather logging. Fixed bug with search bar shortcuts --- CHANGELOG.md | 5 +++ client/src/components/SearchBar/SearchBar.tsx | 16 +++++++++ utils/getExternalWeather.js | 8 ----- utils/jobs.js | 35 +++++++++++++------ 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc2dbd5..0f57ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### v1.7.2 (TBA) +- Use search bar shortcuts when it's not focused ([#124](https://github.com/pawelmalak/flame/issues/124)) +- Fixed bug with Weather API still logging with module being disabled ([#125](https://github.com/pawelmalak/flame/issues/125)) +- Added option to disable search bar autofocus ([#127](https://github.com/pawelmalak/flame/issues/127)) + ### v1.7.1 (2021-10-22) - Fixed search action not being triggered by Numpad Enter - Added option to change date formatting ([#92](https://github.com/pawelmalak/flame/issues/92)) diff --git a/client/src/components/SearchBar/SearchBar.tsx b/client/src/components/SearchBar/SearchBar.tsx index a535c19..c20b457 100644 --- a/client/src/components/SearchBar/SearchBar.tsx +++ b/client/src/components/SearchBar/SearchBar.tsx @@ -25,12 +25,28 @@ const SearchBar = (props: ComponentProps): JSX.Element => { const inputRef = useRef(document.createElement('input')); + // Search bar autofocus useEffect(() => { if (!loading && !config.disableAutofocus) { inputRef.current.focus(); } }, [config]); + // Listen for keyboard events outside of search bar + useEffect(() => { + const keyOutsideFocus = (e: any) => { + const { key } = e as KeyboardEvent; + + if (key === 'Escape') { + clearSearch(); + } + }; + + window.addEventListener('keydown', keyOutsideFocus); + + return () => window.removeEventListener('keydown', keyOutsideFocus); + }, []); + const clearSearch = () => { inputRef.current.value = ''; setLocalSearch(''); diff --git a/utils/getExternalWeather.js b/utils/getExternalWeather.js index 8b2be8d..20edac4 100644 --- a/utils/getExternalWeather.js +++ b/utils/getExternalWeather.js @@ -5,14 +5,6 @@ const loadConfig = require('./loadConfig'); const getExternalWeather = async () => { const { WEATHER_API_KEY: secret, lat, long } = await loadConfig(); - if (!secret) { - throw new Error('API key was not found. Weather updated failed'); - } - - if (!lat || !long) { - throw new Error('Location was not found. Weather updated failed'); - } - // Fetch data from external API try { const res = await axios.get( diff --git a/utils/jobs.js b/utils/jobs.js index 935f497..9716af0 100644 --- a/utils/jobs.js +++ b/utils/jobs.js @@ -3,20 +3,33 @@ const getExternalWeather = require('./getExternalWeather'); const clearWeatherData = require('./clearWeatherData'); const Sockets = require('../Sockets'); const Logger = require('./Logger'); +const loadConfig = require('./loadConfig'); const logger = new Logger(); // Update weather data every 15 minutes -const weatherJob = schedule.scheduleJob('updateWeather', '0 */15 * * * *', async () => { - try { - const weatherData = await getExternalWeather(); - logger.log('Weather updated'); - Sockets.getSocket('weather').socket.send(JSON.stringify(weatherData)); - } catch (err) { - logger.log(err.message, 'ERROR'); +const weatherJob = schedule.scheduleJob( + 'updateWeather', + '0 */15 * * * *', + async () => { + const { WEATHER_API_KEY: secret } = await loadConfig(); + + try { + const weatherData = await getExternalWeather(); + logger.log('Weather updated'); + Sockets.getSocket('weather').socket.send(JSON.stringify(weatherData)); + } catch (err) { + if (secret) { + logger.log(err.message, 'ERROR'); + } + } } -}) +); // Clear old weather data every 4 hours -const weatherCleanerJob = schedule.scheduleJob('clearWeather', '0 5 */4 * * *', async () => { - clearWeatherData(); -}) \ No newline at end of file +const weatherCleanerJob = schedule.scheduleJob( + 'clearWeather', + '0 5 */4 * * *', + async () => { + clearWeatherData(); + } +);