YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / components / material-ui / snackDisplay.tsx
1 /**
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import * as React from 'react';
19
20 import { IApplicationStoreState } from '../../store/applicationStore';
21 import { Connect, connect, IDispatcher } from '../../flux/connect';
22 import { RemoveSnackbarNotification } from '../../actions/snackbarActions';
23
24 import { InjectedNotistackProps, withSnackbar } from 'notistack';
25
26 const mapProps = (state: IApplicationStoreState) => ({
27   notifications: state.framework.applicationState.snackBars
28 });
29
30 const mapDispatch = (dispatcher: IDispatcher) => ({
31   removeSnackbar: (key: number) => {
32     dispatcher.dispatch(new RemoveSnackbarNotification(key));
33    }
34 });
35
36 type DisplaySnackbarsComponentProps = Connect<typeof mapProps, typeof mapDispatch> & InjectedNotistackProps;
37
38 class DisplaySnackbarsComponent extends React.Component<DisplaySnackbarsComponentProps> {
39   private displayed: number[] = [];
40
41   private storeDisplayed = (id: number) => {
42     this.displayed = [...this.displayed, id];
43   };
44
45   public shouldComponentUpdate({ notifications: newSnacks = [] }: DisplaySnackbarsComponentProps) {
46     
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;
52     }
53     return notExists;
54   }
55
56   componentDidUpdate() {
57     const { notifications = [] } = this.props;
58
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);
65     });
66   }
67
68   render() {
69     return null;
70   }
71 }
72
73 const DisplayStackbars = withSnackbar(connect(mapProps, mapDispatch)(DisplaySnackbarsComponent));
74 export default DisplayStackbars;