From fba40128754dad63b8179a085cbf1b87f28088bc Mon Sep 17 00:00:00 2001 From: alexsparkes Date: Wed, 7 Feb 2024 12:13:07 +0000 Subject: [PATCH] refactor(settings): Split weather into sections for increased readability --- .../modals/main/settings/sections/Time.jsx | 18 +- .../modals/main/settings/sections/Weather.jsx | 187 ++++++++++-------- src/scss/index.scss | 5 + 3 files changed, 122 insertions(+), 88 deletions(-) diff --git a/src/components/modals/main/settings/sections/Time.jsx b/src/components/modals/main/settings/sections/Time.jsx index 37517081..ef7864d9 100644 --- a/src/components/modals/main/settings/sections/Time.jsx +++ b/src/components/modals/main/settings/sections/Time.jsx @@ -149,12 +149,18 @@ export default class TimeSettings extends PureComponent { ); - if (this.state.timeType === 'digital') { - timeSettings = digitalSettings; - } else if (this.state.timeType === 'analogue') { - timeSettings = analogSettings; - } else if (this.state.timeType === 'verticalClock') { - timeSettings = verticalClock; + switch (this.state.timeType) { + case 'digital': + timeSettings = digitalSettings; + break; + case 'analogue': + timeSettings = analogSettings; + break; + case 'verticalClock': + timeSettings = verticalClock; + break; + default: + timeSettings = null; } return ( diff --git a/src/components/modals/main/settings/sections/Weather.jsx b/src/components/modals/main/settings/sections/Weather.jsx index 82764681..7fdeb107 100644 --- a/src/components/modals/main/settings/sections/Weather.jsx +++ b/src/components/modals/main/settings/sections/Weather.jsx @@ -36,75 +36,13 @@ export default class TimeSettings extends PureComponent { this.showReminder(); } - getAuto() { - navigator.geolocation.getCurrentPosition( - async (position) => { - const data = await ( - await fetch( - `${variables.constants.API_URL}/gps?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}`, - ) - ).json(); - this.setState({ - location: data[0].name, - }); - - this.showReminder(); - }, - (error) => { - // firefox requires this 2nd function - console.log(error); - }, - { - enableHighAccuracy: true, - }, - ); - } - render() { const weatherType = localStorage.getItem('weatherType'); const WEATHER_SECTION = 'modals.main.settings.sections.weather'; - const weatherOptions = [ - { - name: 'weatherdescription', - textKey: `${WEATHER_SECTION}.extra_info.show_description`, - }, - { - name: 'cloudiness', - textKey: `${WEATHER_SECTION}.extra_info.cloudiness`, - }, - { name: 'humidity', textKey: `${WEATHER_SECTION}.extra_info.humidity` }, - { - name: 'visibility', - textKey: `${WEATHER_SECTION}.extra_info.visibility`, - }, - { - name: 'windspeed', - textKey: `${WEATHER_SECTION}.extra_info.wind_speed`, - onChange: () => this.setState({ windSpeed: localStorage.getItem('windspeed') !== 'true' }), - }, - { - name: 'windDirection', - textKey: `${WEATHER_SECTION}.extra_info.wind_direction`, - disabled: this.state.windSpeed, - }, - { - name: 'atmosphericpressure', - textKey: `${WEATHER_SECTION}.extra_info.atmospheric_pressure`, - }, - ]; - - return ( - <> -
+ const WidgetType = () => { + return ( {variables.getMessage(`${WEATHER_SECTION}.options.custom`)} + ); + }; + + const LocationSetting = () => { + const getAuto = () => { + navigator.geolocation.getCurrentPosition( + async (position) => { + const data = await ( + await fetch( + `${variables.constants.API_URL}/gps?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}`, + ) + ).json(); + this.setState({ + location: data[0].name, + }); + + this.showReminder(); + }, + (error) => { + // firefox requires this 2nd function + console.log(error); + }, + { + enableHighAccuracy: true, + }, + ); + }; + return ( - this.getAuto()}> + {variables.getMessage(`${WEATHER_SECTION}.auto`)} + ); + }; + + const TemperatureFormat = () => { + return ( - {weatherType === '4' && ( - - {weatherOptions.map((item) => ( - - ))} - - )} + ); + }; + + const CustomOptions = () => { + const weatherOptions = [ + { + name: 'weatherdescription', + textKey: `${WEATHER_SECTION}.extra_info.show_description`, + }, + { + name: 'cloudiness', + textKey: `${WEATHER_SECTION}.extra_info.cloudiness`, + }, + { name: 'humidity', textKey: `${WEATHER_SECTION}.extra_info.humidity` }, + { + name: 'visibility', + textKey: `${WEATHER_SECTION}.extra_info.visibility`, + }, + { + name: 'windspeed', + textKey: `${WEATHER_SECTION}.extra_info.wind_speed`, + onChange: () => + this.setState({ windSpeed: localStorage.getItem('windspeed') !== 'true' }), + }, + { + name: 'windDirection', + textKey: `${WEATHER_SECTION}.extra_info.wind_direction`, + disabled: this.state.windSpeed, + }, + { + name: 'atmosphericpressure', + textKey: `${WEATHER_SECTION}.extra_info.atmospheric_pressure`, + }, + ]; + + return ( + + {weatherOptions.map((item) => ( + + ))} + + ); + }; + + return ( + <> +
+ + {/* https://stackoverflow.com/a/65328486 when using inputs it may defocus so we do the {} instead of <> */} + {LocationSetting()} + + {weatherType === '4' && } ); } diff --git a/src/scss/index.scss b/src/scss/index.scss index fffac06e..d9fac966 100644 --- a/src/scss/index.scss +++ b/src/scss/index.scss @@ -130,3 +130,8 @@ body { padding-right: 20px; } } + +.inactiveSetting { + opacity: 0.5; + pointer-events: none; +}