fix weathersettings broken logic

This commit is contained in:
Gerome Matilla 2020-06-12 15:49:43 +08:00
parent 53026c94b4
commit 70a07fd5c3

View File

@ -22,15 +22,14 @@ class WeatherSettings {
this._init(); this._init();
} }
// Apply credentials _init = () => {
_applyWeatherSettings = (key, city, units) => { // this._updat2eWeatherSettings();
this._localStorage.setItem('apiKey', key); this._registerWeatherResetOnClickEvent();
this._localStorage.setItem('cityID', city); this._registerWeatherApplyOnClickEvent();
this._localStorage.setItem('units', units);
} }
// Clear credentials // Clear credentials
_resetWeatherSettings = () => { _clearWeatherCredentials = () => {
this._localStorage.removeItem('apiKey'); this._localStorage.removeItem('apiKey');
this._localStorage.removeItem('cityID'); this._localStorage.removeItem('cityID');
this._localStorage.removeItem('units'); this._localStorage.removeItem('units');
@ -43,6 +42,20 @@ class WeatherSettings {
this._weatherSelectUnits.value = 'metric'; this._weatherSelectUnits.value = 'metric';
} }
// Apply credentials
_applyWeatherSettings = (key, city, units) => {
this._localStorage.setItem('apiKey', key);
this._localStorage.setItem('cityID', city);
this._localStorage.setItem('units', units);
}
// Update credential variables
_updateCredentialVariables = () => {
this._appID = localStorage.getItem('apiKey') || 'API Key';
this._cityID = localStorage.getItem('cityID') || 'City ID';
this._units = localStorage.getItem('units') || 'metric';
}
// Update textbox placeholders // Update textbox placeholders
_updateWeatherSettingsPlaceholder = () => { _updateWeatherSettingsPlaceholder = () => {
this._apiKeySet.placeholder = this._appID; this._apiKeySet.placeholder = this._appID;
@ -52,9 +65,9 @@ class WeatherSettings {
// Update weather settings // Update weather settings
_updateWeatherSettings = () => { _updateWeatherSettings = () => {
this._appID = localStorage.getItem('apiKey') || 'API Key';
this._cityID = localStorage.getItem('cityID') || 'City ID'; // Update cred vars
this._units = localStorage.getItem('units') || 'metric'; this._updateCredentialVariables();
// Update weather forecast elements // Update weather forecast elements
this.getWeatherData(this._appID, this._cityID, this._units); this.getWeatherData(this._appID, this._cityID, this._units);
@ -62,36 +75,40 @@ class WeatherSettings {
this._deleteWeatherSettingsValue(); this._deleteWeatherSettingsValue();
this._updateWeatherSettingsPlaceholder(); this._updateWeatherSettingsPlaceholder();
} }
// Reset
_weatherResetOnClickEvent = e => { _weatherResetOnClickEvent = e => {
// Reset keys // Reset keys
this._resetWeatherSettings(); this._clearWeatherCredentials();
this._updateWeatherSettings();
this._updateCredentialVariables();
this._deleteWeatherSettingsValue();
this._updateWeatherSettingsPlaceholder();
alert('Credentials deleted!'); alert('Credentials deleted!');
} }
// Apply Onclick event
_weatherApplyOnClickEvent = e => {
// Update credentials/Settings
this._applyWeatherSettings(
this._apiKeySet.value || this._apiKeySet.placeholder,
this._cityIDSet.value || this._cityIDSet.placeholder,
this._weatherSelectUnits.options[this._weatherSelectUnits.selectedIndex].value
);
this._updateWeatherSettings();
alert('Successfully updated!');
}
_registerWeatherResetOnClickEvent = () => { _registerWeatherResetOnClickEvent = () => {
this._weatherSettingsReset.onclick = this._weatherResetOnClickEvent; this._weatherSettingsReset.onclick = this._weatherResetOnClickEvent;
} }
_weatherApplyOnClickEvent = e => {
this._applyWeatherSettings(
this._apiKeySet.value || this._apiKeySet.placeholder,
this._cityIDSet.value || this._cityIDSet.placeholder,
this._weatherSelectUnits.options[this._weatherSelectUnits.selectedIndex].value
);
this._updateWeatherSettings();
alert('Successfully updated!');
}
_registerWeatherApplyOnClickEvent = () => { _registerWeatherApplyOnClickEvent = () => {
this._weatherSettingsApply.onclick = this._weatherApplyOnClickEvent; this._weatherSettingsApply.onclick = this._weatherApplyOnClickEvent;
} }
_init = () => {
this._updateWeatherSettings();
this._registerWeatherResetOnClickEvent();
this._registerWeatherApplyOnClickEvent();
}
} }