2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import * as React from 'react';
20 import { IApplicationStoreState } from '../../store/applicationStore';
21 import { Connect, connect, IDispatcher } from '../../flux/connect';
22 import { RemoveSnackbarNotification } from '../../actions/snackbarActions';
24 import { InjectedNotistackProps, withSnackbar } from 'notistack';
26 const mapProps = (state: IApplicationStoreState) => ({
27 notifications: state.framework.applicationState.snackBars
30 const mapDispatch = (dispatcher: IDispatcher) => ({
31 removeSnackbar: (key: number) => {
32 dispatcher.dispatch(new RemoveSnackbarNotification(key));
36 type DisplaySnackbarsComponentProps = Connect<typeof mapProps, typeof mapDispatch> & InjectedNotistackProps;
38 class DisplaySnackbarsComponent extends React.Component<DisplaySnackbarsComponentProps> {
39 private displayed: number[] = [];
41 private storeDisplayed = (id: number) => {
42 this.displayed = [...this.displayed, id];
45 public shouldComponentUpdate({ notifications: newSnacks = [] }: DisplaySnackbarsComponentProps) {
47 const { notifications: currentSnacks } = this.props;
48 let notExists = false;
49 for (let i = 0; i < newSnacks.length; i++) {
50 if (notExists) continue;
51 notExists = notExists || !currentSnacks.filter(({ key }) => newSnacks[i].key === key).length;
56 componentDidUpdate() {
57 const { notifications = [] } = this.props;
59 notifications.forEach(notification => {
60 if (this.displayed.includes(notification.key)) return;
61 const options = notification.options || {};
62 this.props.enqueueSnackbar(notification.message, options);
63 this.storeDisplayed(notification.key);
64 this.props.removeSnackbar(notification.key);
73 const DisplayStackbars = withSnackbar(connect(mapProps, mapDispatch)(DisplaySnackbarsComponent));
74 export default DisplayStackbars;