From e2b9e9dfdca6a1d7bc0548d1fd9f08ead2698e39 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 13 Oct 2019 07:48:49 +0200 Subject: [PATCH] add clear cache button for session --- src/domain/SessionPickerViewModel.js | 24 ++++++++++++++++++++++++ src/ui/web/login/SessionPickerView.js | 19 ++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/domain/SessionPickerViewModel.js b/src/domain/SessionPickerViewModel.js index 97c07090..a0a8db64 100644 --- a/src/domain/SessionPickerViewModel.js +++ b/src/domain/SessionPickerViewModel.js @@ -7,6 +7,7 @@ class SessionItemViewModel extends EventEmitter { this._pickerVM = pickerVM; this._sessionInfo = sessionInfo; this._isDeleting = false; + this._isClearing = false; this._error = null; } @@ -29,10 +30,29 @@ class SessionItemViewModel extends EventEmitter { } } + async clear() { + this._isClearing = true; + this.emit("change", "isClearing"); + try { + await this._pickerVM.clear(this.id); + } catch(err) { + this._error = err; + console.error(err); + this.emit("change", "error"); + } finally { + this._isClearing = false; + this.emit("change", "isClearing"); + } + } + get isDeleting() { return this._isDeleting; } + get isClearing() { + return this._isClearing; + } + get id() { return this._sessionInfo.id; } @@ -73,6 +93,10 @@ export default class SessionPickerViewModel { this._sessions.remove(idx); } + async clear(id) { + await this._storageFactory.delete(id); + } + get sessions() { return this._sessions; } diff --git a/src/ui/web/login/SessionPickerView.js b/src/ui/web/login/SessionPickerView.js index 363158ba..e14ba2c1 100644 --- a/src/ui/web/login/SessionPickerView.js +++ b/src/ui/web/login/SessionPickerView.js @@ -5,22 +5,35 @@ class SessionPickerItem extends TemplateView { constructor(vm) { super(vm, true); this._onDeleteClick = this._onDeleteClick.bind(this); + this._onClearClick = this._onClearClick.bind(this); } _onDeleteClick(event) { event.stopPropagation(); event.preventDefault(); - this.viewModel.delete(); + if (confirm("Are you sure?")) { + this.viewModel.delete(); + } + } + + _onClearClick(event) { + event.stopPropagation(); + event.preventDefault(); + this.viewModel.clear(); } render(t) { const deleteButton = t.button({ disabled: vm => vm.isDeleting, - onClick: event => this._onDeleteClick(event) + onClick: this._onDeleteClick, }, "Delete"); + const clearButton = t.button({ + disabled: vm => vm.isClearing, + onClick: this._onClearClick, + }, "Clear"); const userName = t.span({className: "userId"}, vm => vm.userId); const errorMessage = t.if(vm => vm.error, t => t.span({className: "error"}, vm => vm.error)); - return t.li([userName, errorMessage, deleteButton]); + return t.li([userName, errorMessage, clearButton, deleteButton]); } }