-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Introduce alert snackbar UI #4244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| load("//tensorboard/defs:defs.bzl", "tf_ts_library") | ||
| load("@npm_angular_bazel//:index.bzl", "ng_module") | ||
|
|
||
| package(default_visibility = ["//tensorboard:internal"]) | ||
|
|
||
| ng_module( | ||
| name = "alert", | ||
| srcs = [ | ||
| "alert_module.ts", | ||
| ], | ||
| deps = [ | ||
| ":alert_action", | ||
| "//tensorboard/webapp/alert/effects", | ||
| "//tensorboard/webapp/alert/store", | ||
| "//tensorboard/webapp/alert/store:types", | ||
| "//tensorboard/webapp/alert/views:alert_snackbar", | ||
| "@npm//@angular/core", | ||
| "@npm//@ngrx/effects", | ||
| "@npm//@ngrx/store", | ||
| ], | ||
| ) | ||
|
|
||
| ng_module( | ||
| name = "alert_action", | ||
| srcs = [ | ||
| "alert_action_module.ts", | ||
| ], | ||
| deps = [ | ||
| ":types", | ||
| "@npm//@angular/core", | ||
| "@npm//@ngrx/store", | ||
| ], | ||
| ) | ||
|
|
||
| tf_ts_library( | ||
| name = "types", | ||
| srcs = [ | ||
| "types.ts", | ||
| ], | ||
| ) | ||
|
|
||
| tf_ts_library( | ||
| name = "test_lib", | ||
| testonly = True, | ||
| srcs = [ | ||
| "alert_action_test.ts", | ||
| ], | ||
| deps = [ | ||
| ":alert_action", | ||
| "//tensorboard/webapp:app_state", | ||
| "//tensorboard/webapp/alert/actions", | ||
| "//tensorboard/webapp/alert/effects", | ||
| "//tensorboard/webapp/alert/store", | ||
| "//tensorboard/webapp/angular:expect_angular_core_testing", | ||
| "//tensorboard/webapp/angular:expect_ngrx_store_testing", | ||
| "@npm//@angular/common", | ||
| "@npm//@angular/compiler", | ||
| "@npm//@angular/core", | ||
| "@npm//@ngrx/effects", | ||
| "@npm//@ngrx/store", | ||
| "@npm//@types/jasmine", | ||
| "@npm//rxjs", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| load("//tensorboard/defs:defs.bzl", "tf_ts_library") | ||
|
|
||
| package(default_visibility = ["//tensorboard:internal"]) | ||
|
|
||
| tf_ts_library( | ||
| name = "actions", | ||
| srcs = [ | ||
| "index.ts", | ||
| ], | ||
| visibility = [ | ||
| "//tensorboard/webapp/alert:__subpackages__", | ||
| ], | ||
| deps = [ | ||
| "//tensorboard/webapp/alert:types", | ||
| "@npm//@ngrx/store", | ||
| ], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
| import {createAction, props} from '@ngrx/store'; | ||
|
|
||
| import {AlertReport} from '../types'; | ||
|
|
||
| /** @typehack */ import * as _typeHackModels from '@ngrx/store/src/models'; | ||
| /** @typehack */ import * as _typeHackStore from '@ngrx/store'; | ||
|
|
||
| /** | ||
| * Fires when an alert is to be reported. | ||
| */ | ||
| export const alertReported = createAction( | ||
| '[Alert] Alert Reported', | ||
| props<AlertReport>() | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
| import { | ||
| Inject, | ||
| ModuleWithProviders, | ||
| NgModule, | ||
| Optional, | ||
| InjectionToken, | ||
| } from '@angular/core'; | ||
| import {Action, ActionCreator, Creator} from '@ngrx/store'; | ||
| import {AlertReport} from './types'; | ||
|
|
||
| // While this token is not used outside, it must be exported so that stricter | ||
| // build tools may discover it during compilation. | ||
| export const ACTION_TO_ALERT_PROVIDER = new InjectionToken< | ||
| ActionToAlertConfig[] | ||
| >('[Alert] Action-To-Alert Provider'); | ||
|
|
||
| export type ActionToAlertTransformer = (action: Action) => AlertReport | null; | ||
|
|
||
| export interface ActionToAlertConfig { | ||
| /** | ||
| * The action to listen for. | ||
| */ | ||
| actionCreator: ActionCreator<string, Creator>; | ||
|
|
||
| /** | ||
| * A function that returns an alert report, or null, when the action is | ||
| * received. | ||
| */ | ||
| alertFromAction: ActionToAlertTransformer; | ||
| } | ||
|
|
||
| /** | ||
| * An NgModule that provides alert-producing actions. These action configs are | ||
| * collected by AlertModule, which tracks application alerts. | ||
| * | ||
| * When the configured action fires, the AlertModule may respond. | ||
| * | ||
| * @NgModule({ | ||
| * imports: [ | ||
| * AlertActionModule.registerAlertActions([ | ||
| * { | ||
| * action: fetchKeysFailed, | ||
| * alertFromAction: () => {localizedMessage: "Keys failed to fetch."}, | ||
| * }, | ||
| * { | ||
| * action: greenButtonClicked, | ||
| * alertFromAction: (actionPayload) => { | ||
| * if (!actionPayload.wasButtonEnabled) { | ||
| * return {localizedMessage: "Green button failed."}; | ||
| * } | ||
| * return null; | ||
| * } | ||
| * } | ||
| * ]), | ||
| * ], | ||
| * }) | ||
| */ | ||
| @NgModule({}) | ||
| export class AlertActionModule { | ||
| /** | ||
| * Map from action creator type to transformer function. | ||
| */ | ||
| private readonly providers = new Map<string, ActionToAlertTransformer>(); | ||
|
|
||
| constructor( | ||
| @Optional() | ||
| @Inject(ACTION_TO_ALERT_PROVIDER) | ||
| providers: ActionToAlertConfig[][] | ||
| ) { | ||
| for (const configs of providers || []) { | ||
| for (const config of configs) { | ||
| if (this.providers.has(config.actionCreator.type)) { | ||
| throw new RangeError( | ||
| `"${config.actionCreator.type}" is already registered for alerts.` + | ||
| ' Multiple alerts for the same action is not allowed.' | ||
| ); | ||
| } | ||
| this.providers.set(config.actionCreator.type, config.alertFromAction); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| getAlertFromAction(action: Action): AlertReport | null { | ||
| const lambda = this.providers.get(action.type); | ||
| if (!lambda) { | ||
| return null; | ||
| } | ||
| return lambda(action); | ||
| } | ||
|
|
||
| static registerAlertActions( | ||
| providerFactory: () => ActionToAlertConfig[] | ||
| ): ModuleWithProviders<AlertActionModule> { | ||
| return { | ||
| ngModule: AlertActionModule, | ||
| providers: [ | ||
| { | ||
| provide: ACTION_TO_ALERT_PROVIDER, | ||
| multi: true, | ||
| useFactory: providerFactory, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
| import {TestBed} from '@angular/core/testing'; | ||
| import {EffectsModule} from '@ngrx/effects'; | ||
| import {provideMockActions} from '@ngrx/effects/testing'; | ||
| import {Action, createAction, Store} from '@ngrx/store'; | ||
| import {MockStore, provideMockStore} from '@ngrx/store/testing'; | ||
| import {ReplaySubject} from 'rxjs'; | ||
| import {State} from '../app_state'; | ||
| import * as alertActions from './actions'; | ||
| import {AlertActionModule} from './alert_action_module'; | ||
| import {AlertEffects} from './effects'; | ||
|
|
||
| const alertActionOccurred = createAction('[Test] Action Occurred (need alert)'); | ||
| const noAlertActionOccurred = createAction('[Test] Action Occurred (no alert)'); | ||
|
|
||
| describe('alert_effects', () => { | ||
| let actions$: ReplaySubject<Action>; | ||
| let store: MockStore<Partial<State>>; | ||
| let recordedActions: Action[] = []; | ||
| let shouldReportAlert: boolean; | ||
|
|
||
| beforeEach(async () => { | ||
| shouldReportAlert = false; | ||
| actions$ = new ReplaySubject<Action>(1); | ||
|
|
||
| await TestBed.configureTestingModule({ | ||
| imports: [ | ||
| AlertActionModule.registerAlertActions(() => [ | ||
| { | ||
| actionCreator: alertActionOccurred, | ||
| alertFromAction: (action: Action) => { | ||
| if (shouldReportAlert) { | ||
| return {localizedMessage: 'alert details'}; | ||
| } | ||
| return null; | ||
| }, | ||
| }, | ||
| ]), | ||
| EffectsModule.forFeature([AlertEffects]), | ||
| EffectsModule.forRoot([]), | ||
| ], | ||
| providers: [provideMockActions(actions$), provideMockStore({})], | ||
| }).compileComponents(); | ||
|
|
||
| store = TestBed.inject<Store<State>>(Store) as MockStore<State>; | ||
| recordedActions = []; | ||
| spyOn(store, 'dispatch').and.callFake((action: Action) => { | ||
| recordedActions.push(action); | ||
| }); | ||
| }); | ||
|
|
||
| it(`reports an alert when 'alertFromAction' returns a report`, () => { | ||
| shouldReportAlert = true; | ||
| actions$.next(alertActionOccurred); | ||
|
|
||
| expect(recordedActions).toEqual([ | ||
| alertActions.alertReported({localizedMessage: 'alert details'}), | ||
| ]); | ||
| }); | ||
|
|
||
| it(`does not alert when 'alertFromAction' returns null`, () => { | ||
| shouldReportAlert = false; | ||
| actions$.next(alertActionOccurred); | ||
|
|
||
| expect(recordedActions).toEqual([]); | ||
| }); | ||
|
|
||
| it(`does not alert when a non-matching action is fired`, () => { | ||
| shouldReportAlert = true; | ||
| actions$.next(noAlertActionOccurred); | ||
|
|
||
| expect(recordedActions).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
| import {NgModule} from '@angular/core'; | ||
| import {EffectsModule} from '@ngrx/effects'; | ||
| import {StoreModule} from '@ngrx/store'; | ||
| import {AlertActionModule} from './alert_action_module'; | ||
| import {AlertEffects} from './effects'; | ||
| import {reducers} from './store'; | ||
| import {ALERT_FEATURE_KEY} from './store/alert_types'; | ||
| import {AlertSnackbarModule} from './views/alert_snackbar_module'; | ||
|
|
||
| @NgModule({ | ||
| imports: [ | ||
| AlertActionModule, | ||
| AlertSnackbarModule, | ||
| StoreModule.forFeature(ALERT_FEATURE_KEY, reducers), | ||
| EffectsModule.forFeature([AlertEffects]), | ||
| ], | ||
| }) | ||
| export class AlertModule {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure this is only usable by the effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Visibility reduced to alerts/.