Skip to content

Commit fa4a220

Browse files
committed
fix: address copilot review comment
1 parent 919af53 commit fa4a220

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/app/devices/device-toolbar/device-toolbar.component.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ describe('DeviceToolbarComponent', () => {
148148
expect(devicesService.getPowerStateCached).not.toHaveBeenCalled()
149149
})
150150

151+
it('should show snackbar and reset loading state when refresh power state fails', () => {
152+
devicesService.getPowerState.and.returnValue(throwError(() => new Error('Network error')))
153+
154+
component.refreshPowerState()
155+
156+
expect(snackBar.open).toHaveBeenCalled()
157+
expect(component.isLoading()()).toBeFalse()
158+
})
159+
151160
it('should navigate to device', async () => {
152161
fixture.componentRef.setInput('deviceId', '12345-pokli-456772')
153162
const routerSpy = spyOn(component.router, 'navigate')

src/app/devices/device-toolbar/device-toolbar.component.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ export class DeviceToolbarComponent implements OnInit {
217217
this.powerOptions.set(options)
218218
}
219219

220+
private setPowerStateLabel(powerstate: number): void {
221+
this.powerState.set(
222+
powerstate.toString() === '2'
223+
? 'deviceToolbar.power.on.value'
224+
: powerstate.toString() === '3' || powerstate.toString() === '4'
225+
? 'deviceToolbar.power.sleep.value'
226+
: 'deviceToolbar.power.off.value'
227+
)
228+
}
229+
220230
private loadPowerState(): void {
221231
// Use cached to dedupe simultaneous requests (KVM deep-link + toolbar both loading).
222232
this.isLoading().set(true)
@@ -233,13 +243,7 @@ export class DeviceToolbarComponent implements OnInit {
233243
takeUntilDestroyed(this.destroyRef)
234244
)
235245
.subscribe((powerState) => {
236-
this.powerState.set(
237-
powerState.powerstate.toString() === '2'
238-
? 'deviceToolbar.power.on.value'
239-
: powerState.powerstate.toString() === '3' || powerState.powerstate.toString() === '4'
240-
? 'deviceToolbar.power.sleep.value'
241-
: 'deviceToolbar.power.off.value'
242-
)
246+
this.setPowerStateLabel(powerState.powerstate)
243247
})
244248
}
245249

@@ -250,8 +254,10 @@ export class DeviceToolbarComponent implements OnInit {
250254
this.devicesService
251255
.getPowerState(this.deviceId())
252256
.pipe(
253-
catchError(() => {
254-
// Network/backend failure; finalize will reset loading state.
257+
catchError((err) => {
258+
console.error(err)
259+
const msg: string = this.translate.instant('kvm.errorRetrievePower.value')
260+
this.snackBar.open(msg, undefined, SnackbarDefaults.defaultError)
255261
return EMPTY
256262
}),
257263
finalize(() => {
@@ -260,13 +266,7 @@ export class DeviceToolbarComponent implements OnInit {
260266
takeUntilDestroyed(this.destroyRef)
261267
)
262268
.subscribe((powerState) => {
263-
this.powerState.set(
264-
powerState.powerstate.toString() === '2'
265-
? 'deviceToolbar.power.on.value'
266-
: powerState.powerstate.toString() === '3' || powerState.powerstate.toString() === '4'
267-
? 'deviceToolbar.power.sleep.value'
268-
: 'deviceToolbar.power.off.value'
269-
)
269+
this.setPowerStateLabel(powerState.powerstate)
270270
})
271271
}
272272

0 commit comments

Comments
 (0)