Skip to content

Commit

Permalink
fix: upon extension error, loading spinner should be removed (#461)
Browse files Browse the repository at this point in the history
* fix: upon extension error, loading spinner should be removed

* feat: show a message when an extension fails to load actions

* fix: extension state data structure

* fix: initialize extensions state

Co-authored-by: Johnny Almonte <johnny243@users.noreply.github.com>
  • Loading branch information
johnny243 and johnny243 authored Sep 7, 2020
1 parent d458915 commit 6717907
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
61 changes: 45 additions & 16 deletions app/assets/javascripts/directives/views/actionsMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ type UpdateActionParams = {
subrows?: ActionSubRow[]
}

type ExtensionState = {
hidden: boolean
loading: boolean
error: boolean
}

type ActionsMenuState = {
extensions: SNActionsExtension[],
hiddenState: Record<UuidString, Boolean>
loadingState: Record<UuidString, Boolean>
extensions: SNActionsExtension[]
extensionsState: Record<UuidString, ExtensionState>
}

class ActionsMenuCtrl extends PureViewCtrl<{}, ActionsMenuState> implements ActionsMenuScope {
Expand All @@ -54,10 +59,17 @@ class ActionsMenuCtrl extends PureViewCtrl<{}, ActionsMenuState> implements Acti
const extensions = this.application.actionsManager!.getExtensions().sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
});
let extensionsState: Record<UuidString, ExtensionState> = {};
extensions.map((extension) => {
extensionsState[extension.uuid] = {
loading: false,
error: false,
hidden: false
};
});
return {
extensions,
loadingState: {},
hiddenState: {}
extensionsState
};
}

Expand All @@ -68,7 +80,11 @@ class ActionsMenuCtrl extends PureViewCtrl<{}, ActionsMenuState> implements Acti
extension,
this.item
);
await this.updateExtension(updatedExtension!);
if (updatedExtension) {
await this.updateExtension(updatedExtension!);
} else {
await this.setErrorExtension(extension.uuid, true);
}
await this.setLoadingExtension(extension.uuid, false);
}));
}
Expand Down Expand Up @@ -178,29 +194,42 @@ class ActionsMenuCtrl extends PureViewCtrl<{}, ActionsMenuState> implements Acti
}

private async toggleExtensionVisibility(extensionUuid: UuidString) {
const { hiddenState } = this.state;
hiddenState[extensionUuid] = !hiddenState[extensionUuid] ?? false;
const { extensionsState } = this.state;
extensionsState[extensionUuid].hidden = !extensionsState[extensionUuid].hidden;
await this.setState({
hiddenState
extensionsState
});
}

private isExtensionVisible(extensionUuid: UuidString) {
const { hiddenState } = this.state;
return hiddenState[extensionUuid] ?? false;
const { extensionsState } = this.state;
return extensionsState[extensionUuid].hidden;
}

private async setLoadingExtension(extensionUuid: UuidString, value = false) {
const { loadingState } = this.state;
loadingState[extensionUuid] = value;
const { extensionsState } = this.state;
extensionsState[extensionUuid].loading = value;
await this.setState({
loadingState
extensionsState
});
}

private isExtensionLoading(extensionUuid: UuidString) {
const { loadingState } = this.state;
return loadingState[extensionUuid] ?? false;
const { extensionsState } = this.state;
return extensionsState[extensionUuid].loading;
}

private async setErrorExtension(extensionUuid: UuidString, value = false) {
const { extensionsState } = this.state;
extensionsState[extensionUuid].error = value;
await this.setState({
extensionsState
});
}

private extensionHasError(extensionUuid: UuidString) {
const { extensionsState } = this.state;
return extensionsState[extensionUuid].error;
}
}

Expand Down
8 changes: 7 additions & 1 deletion app/assets/templates/directives/actions-menu.pug
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
menu-row(
action='self.executeAction(action, extension)',
label='action.label',
ng-if='!self.isExtensionVisible(extension.uuid) && !self.isExtensionLoading(extension.uuid)',
ng-if='!self.isExtensionVisible(extension.uuid) && !self.isExtensionLoading(extension.uuid) && !self.extensionHasError(extension.uuid)',
ng-repeat='action in extension.actionsWithContextForItem(self.item) track by $index',
disabled='action.running'
spinner-class="action.running ? 'info' : null",
Expand All @@ -35,3 +35,9 @@
label="'No Actions Available'",
ng-if='extension.actionsWithContextForItem(self.item).length == 0'
)
menu-row(
faded='true',
label="'Error loading actions'",
subtitle="'Please try again later.'"
ng-if='self.extensionHasError(extension.uuid)'
)

0 comments on commit 6717907

Please sign in to comment.