component working

This commit is contained in:
Peter Holmberg 2018-10-22 14:22:40 +02:00
parent 5f515bb3fc
commit b7d821b524
8 changed files with 112 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import EmptyListCTA from './components/EmptyListCTA/EmptyListCTA';
import { SearchResult } from './components/search/SearchResult'; import { SearchResult } from './components/search/SearchResult';
import { TagFilter } from './components/TagFilter/TagFilter'; import { TagFilter } from './components/TagFilter/TagFilter';
import { SideMenu } from './components/sidemenu/SideMenu'; import { SideMenu } from './components/sidemenu/SideMenu';
import { AlertList } from './components/Alerts/AlertList'; import AlertList from './components/Alerts/AlertList';
export function registerAngularDirectives() { export function registerAngularDirectives() {
react2AngularDirective('passwordStrength', PasswordStrength, ['password']); react2AngularDirective('passwordStrength', PasswordStrength, ['password']);

View File

@ -1,4 +1,5 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
export interface Props { export interface Props {
alerts: any[]; alerts: any[];
@ -10,7 +11,7 @@ export class AlertList extends PureComponent<Props> {
}; };
render() { render() {
const alerts = [{ severity: 'success', icon: 'warning', title: 'test', text: 'test text' }]; const { alerts } = this.props;
return ( return (
<div> <div>
@ -34,3 +35,11 @@ export class AlertList extends PureComponent<Props> {
); );
} }
} }
function mapStateToProps(state) {
return {
alerts: state.alerts.alerts,
};
}
export default connect(mapStateToProps)(AlertList);

View File

@ -0,0 +1,23 @@
import { Alert } from 'app/types';
export enum ActionTypes {
AddAlert = 'ADD_ALERT',
ClearAlert = 'CLEAR_ALERT',
}
interface AddAlertAction {
type: ActionTypes.AddAlert;
payload: Alert;
}
interface ClearAlertAction {
type: ActionTypes.ClearAlert;
payload: Alert;
}
export type Action = AddAlertAction | ClearAlertAction;
export const clearAlert = (alert: Alert) => ({
type: ActionTypes.ClearAlert,
payload: alert,
});

View File

@ -0,0 +1,41 @@
import { alertsReducer } from './reducers';
import { ActionTypes } from './actions';
describe('clear alert', () => {
it('should filter alert', () => {
const initialState = {
alerts: [
{
severity: 'success',
icon: 'success',
title: 'test',
text: 'test alert',
},
{
severity: 'fail',
icon: 'warning',
title: 'test2',
text: 'test alert fail 2',
},
],
};
const result = alertsReducer(initialState, {
type: ActionTypes.ClearAlert,
payload: initialState.alerts[1],
});
const expectedResult = {
alerts: [
{
severity: 'success',
icon: 'success',
title: 'test',
text: 'test alert',
},
],
};
expect(result).toEqual(expectedResult);
});
});

View File

@ -0,0 +1,23 @@
import { Alert, AlertsState } from 'app/types';
import { Action, ActionTypes } from './actions';
export const initialState: AlertsState = {
alerts: [] as Alert[],
};
export const alertsReducer = (state = initialState, action: Action): AlertsState => {
switch (action.type) {
case ActionTypes.AddAlert:
return { ...state, alerts: state.alerts.concat([action.payload]) };
case ActionTypes.ClearAlert:
return {
...state,
alerts: state.alerts.filter(alert => alert !== action.payload),
};
}
return state;
};
export default {
alerts: alertsReducer,
};

View File

@ -20,8 +20,6 @@ export class AlertSrv {
this.$rootScope this.$rootScope
); );
this.list.push({ severity: 'success', icon: 'warning', title: 'test', text: 'test text' });
this.$rootScope.onAppEvent( this.$rootScope.onAppEvent(
'alert-warning', 'alert-warning',
(e, alert) => { (e, alert) => {

View File

@ -0,0 +1,10 @@
export interface Alert {
severity: string;
icon: string;
title: string;
text: string;
}
export interface AlertsState {
alerts: Alert[];
}

View File

@ -9,6 +9,7 @@ import { ApiKey, ApiKeysState, NewApiKey } from './apiKeys';
import { Invitee, OrgUser, User, UsersState } from './user'; import { Invitee, OrgUser, User, UsersState } from './user';
import { DataSource, DataSourcesState } from './datasources'; import { DataSource, DataSourcesState } from './datasources';
import { PluginDashboard, PluginMeta, Plugin, PluginsState } from './plugins'; import { PluginDashboard, PluginMeta, Plugin, PluginsState } from './plugins';
import { Alert, AlertsState } from './alerts';
export { export {
Team, Team,
@ -46,6 +47,8 @@ export {
User, User,
UsersState, UsersState,
PluginDashboard, PluginDashboard,
Alert,
AlertsState,
}; };
export interface StoreState { export interface StoreState {
@ -58,4 +61,5 @@ export interface StoreState {
dashboard: DashboardState; dashboard: DashboardState;
dataSources: DataSourcesState; dataSources: DataSourcesState;
users: UsersState; users: UsersState;
alerts: AlertsState;
} }