Skip to content

Commit 9764a89

Browse files
committed
Bugfix - handle null temperature unit
1 parent 5b98e34 commit 9764a89

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

‎src/services/weather-service.ts‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ export class WeatherService {
3434
const endIndex = Math.min(startIndex + totalHours, this._forecast.length);
3535
const result = new Map<string, WeatherRowData>();
3636

37+
const tempUnit = this.temperatureUnit;
38+
3739
for (let i = startIndex; i < endIndex; i++) {
38-
const tempUnit = this.temperatureUnit;
40+
const item = this._forecast[i];
41+
const temp = item.temperature.toFixed(1);
3942

40-
const weatherColour = ((): string => {
41-
if (tempUnit === '°C') {
42-
return getColourFromScale(-10, 40, this._forecast[i].temperature.toFixed(1), 'redToBlue', true);
43-
}
44-
return getColourFromScale(14, 104, this._forecast[i].temperature.toFixed(1), 'redToBlue');
45-
})();
43+
const weatherColour = tempUnit === '°C'
44+
? getColourFromScale(-10, 40, temp, 'redToBlue', true)
45+
: getColourFromScale(14, 104, temp, 'redToBlue');
4646

4747
result.set(this._forecast[i].datetime.toString(), {
4848
'weather-column': { value: this._forecast[i].condition, colour: white },
@@ -114,9 +114,7 @@ export class WeatherService {
114114
return this._forecast !== null;
115115
}
116116

117-
public get temperatureUnit(): TemperatureUnit {
118-
if (this._temperatureUnit === null) throw new Error('this._temperatureUnit is null');
119-
117+
public get temperatureUnit(): TemperatureUnit | null {
120118
return this._temperatureUnit;
121119
}
122120

‎src/utils/TableRenderer.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class TableRenderer {
5151
this._state = state;
5252
}
5353

54-
public render(predbatData: PredbatData, temperatureUnit: TemperatureUnit, dayLimit: number, generatingPlan: boolean, carChargeSwitch: 'on' | 'off' | undefined, forceEntityObjects: EntityObject[], weather: Map<string, WeatherRowData> | null): void {
54+
public render(predbatData: PredbatData, temperatureUnit: TemperatureUnit | null, dayLimit: number, generatingPlan: boolean, carChargeSwitch: 'on' | 'off' | undefined, forceEntityObjects: EntityObject[], weather: Map<string, WeatherRowData> | null): void {
5555
if (this._container === null) return;
5656

5757
const leftColumn = divElement('table-left-column');
@@ -233,6 +233,7 @@ export class TableRenderer {
233233
////
234234
case 'temp-column': {
235235
if (typeof columnData.value !== 'number') throw new Error('Invalid temp-column data' + columnData.value);
236+
if(temperatureUnit === null) throw new Error('temperatureUnit must have a value to render temp-column'); // this error should be impossible due to the column being excluded for having no data
236237

237238
cellInner.appendChild(temperatureElement(columnData.value, columnData.colour, temperatureUnit));
238239

0 commit comments

Comments
 (0)