Skip to content

Commit

Permalink
feat: allow to opt-in toast notifications (#2177)
Browse files Browse the repository at this point in the history
* feat: allow to opt-in toast notifications

* Extended docs with configuration of opt-in toast notifications

Co-authored-by: ulrichschulte <ulrich.schulte@codecentric.de>
  • Loading branch information
SteKoe and ulischulte committed Dec 9, 2022
1 parent 702c951 commit 0ac7a6b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions spring-boot-admin-docs/src/main/asciidoc/server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ In addition when the reverse proxy terminates the https connection, it may be ne
| Polling duration in ms to fetch new threads data.
| `2500`

| spring.boot.admin.ui.enable-toasts
| Allows to enable toast notifications.
| `false`

|===

include::server-discovery.adoc[]
Expand Down
33 changes: 20 additions & 13 deletions spring-boot-admin-server-ui/src/main/frontend/utils/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import sbaConfig from '@/sba-config'
import sbaConfig from '@/sba-config';
import axios from 'axios';
import Vue from 'vue';

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.xsrfHeaderName = sbaConfig.csrf.headerName;

export const redirectOn401 = (predicate = () => true) => error => {
export const redirectOn401 = (predicate = () => true) => (error) => {
if (error.response && error.response.status === 401 && predicate(error)) {
window.location.assign(`login?redirectTo=${encodeURIComponent(window.location.href)}`);
window.location.assign(
`login?redirectTo=${encodeURIComponent(window.location.href)}`
);
}
return Promise.reject(error);
};

const instance = axios.create({withCredentials: true, headers: {'Accept': 'application/json'}});
instance.interceptors.response.use(response => response, redirectOn401());
const instance = axios.create({
withCredentials: true,
headers: { Accept: 'application/json' },
});
instance.interceptors.response.use((response) => response, redirectOn401());
instance.create = axios.create;

export default instance;

export const registerErrorToastInterceptor = (axios) => {
axios.interceptors.response.use(
response => response,
error => {
let data = error.request;
Vue.$toast.error(`Request failed: ${data.status} - ${data.statusText}`);
}
)
}
if (sbaConfig.uiSettings.enableToasts === true) {
axios.interceptors.response.use(
(response) => response,
(error) => {
let data = error.request;
Vue.$toast.error(`Request failed: ${data.status} - ${data.statusText}`);
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public UiController homeUiController(UiExtensions uiExtensions) throws IOExcepti

Settings uiSettings = Settings.builder().brand(this.adminUi.getBrand()).title(this.adminUi.getTitle())
.loginIcon(this.adminUi.getLoginIcon()).favicon(this.adminUi.getFavicon())
.faviconDanger(this.adminUi.getFaviconDanger())
.faviconDanger(this.adminUi.getFaviconDanger()).enableToasts(this.adminUi.getEnableToasts())
.notificationFilterEnabled(
!this.applicationContext.getBeansOfType(NotificationFilterController.class).isEmpty())
.routes(routes).rememberMeEnabled(this.adminUi.isRememberMeEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public class AdminServerUiProperties {
*/
private List<String> additionalRouteExcludes = new ArrayList<>();

/**
* Allows to enable toast notifications in SBA.
*/
private Boolean enableToasts = false;

@lombok.Data
public static class PollTimer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public static class Settings {

private final List<ViewSettings> viewSettings;

private final Boolean enableToasts;

}

@lombok.Data
Expand Down

0 comments on commit 0ac7a6b

Please sign in to comment.