[ADF-4811] - added extra method to open snackbar with interpolate arg… (#4989)

* [ADF-4811] - added extra method to open snackbar with interpolate args for translated messages

* [ADF-4811] - added extra parameter to show info

* [ADF-4811] - added extra parameter to show info

* [ADF-4811] added review changes
This commit is contained in:
Vito
2019-08-13 15:13:21 +01:00
committed by GitHub
parent 9ca61975a5
commit 9873e9952a
3 changed files with 32 additions and 9 deletions

View File

@@ -21,7 +21,8 @@ Shows a notification message with optional feedback.
Opens a SnackBar notification to show a message. Opens a SnackBar notification to show a message.
- _message:_ `string` - The message (or resource key) to show. - _message:_ `string` - The message (or resource key) to show.
- _config:_ `number|MatSnackBarConfig` - (Optional) Time before notification disappears after being shown or MatSnackBarConfig object - _config:_ `number|MatSnackBarConfig` - (Optional) Time before notification disappears after being shown or MatSnackBarConfig object
- **Returns** [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)`<any>` - Information/control object for the SnackBar - translationArgs: `any` - The object with the keys for the interpolation of your translation string
- **Returns** [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)`<any>` - Information/control object for the SnackBar
- **openSnackMessageAction**(message: `string`, action: `string`, config?: `number|MatSnackBarConfig`): [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)`<any>`<br/> - **openSnackMessageAction**(message: `string`, action: `string`, config?: `number|MatSnackBarConfig`): [`MatSnackBarRef`](https://material.angular.io/components/snack-bar/overview)`<any>`<br/>
Opens a SnackBar notification with a message and a response button. Opens a SnackBar notification with a message and a response button.
- _message:_ `string` - The message (or resource key) to show. - _message:_ `string` - The message (or resource key) to show.

View File

@@ -45,6 +45,10 @@ class ProvidesNotificationServiceComponent {
return this.notificationService.openSnackMessage('Test notification', 1000); return this.notificationService.openSnackMessage('Test notification', 1000);
} }
sendMessageWithArgs() {
return this.notificationService.openSnackMessage('Test notification {{ arg }}', 1000, {arg: 'arg'});
}
sendCustomMessage() { sendCustomMessage() {
const matSnackBarConfig = new MatSnackBarConfig(); const matSnackBarConfig = new MatSnackBarConfig();
matSnackBarConfig.duration = 1000; matSnackBarConfig.duration = 1000;
@@ -108,6 +112,18 @@ describe('NotificationService', () => {
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should translate messages with args', (done) => {
spyOn(translationService, 'instant').and.callThrough();
const promise = fixture.componentInstance.sendMessageWithArgs();
promise.afterDismissed().subscribe(() => {
expect(translationService.instant).toHaveBeenCalledWith('Test notification {{ arg }}', {arg: 'arg'});
done();
});
fixture.detectChanges();
});
it('should open a message notification bar', (done) => { it('should open a message notification bar', (done) => {
const promise = fixture.componentInstance.sendMessage(); const promise = fixture.componentInstance.sendMessage();
promise.afterDismissed().subscribe(() => { promise.afterDismissed().subscribe(() => {

View File

@@ -41,22 +41,25 @@ export class NotificationService {
/** /**
* Opens a SnackBar notification to show a message. * Opens a SnackBar notification to show a message.
* @param message The message (or resource key) to show. * @param message The message (or resource key) to show.
* @param translationArgs The interpolation parameters to add for the translation
* @param config Time before notification disappears after being shown or MatSnackBarConfig object * @param config Time before notification disappears after being shown or MatSnackBarConfig object
* @returns Information/control object for the SnackBar * @returns Information/control object for the SnackBar
*/ */
openSnackMessage(message: string, config?: number | MatSnackBarConfig): MatSnackBarRef<any> { openSnackMessage(message: string, config?: number | MatSnackBarConfig, translationArgs?: any): MatSnackBarRef<any> {
const translatedMessage = this.translationService.instant(message, translationArgs);
return this.performOpening(translatedMessage, config);
}
private performOpening(translatedMessage: string, config?: number | MatSnackBarConfig): MatSnackBarRef<any> {
if (!config) { if (!config) {
config = this.DEFAULT_DURATION_MESSAGE; config = this.DEFAULT_DURATION_MESSAGE;
} }
const translatedMessage = this.translationService.instant(message);
if (typeof config === 'number') { if (typeof config === 'number') {
config = { config = {
duration: config duration: config
}; };
} }
this.messages.next({ message: translatedMessage, dateTime: new Date }); this.messages.next({ message: translatedMessage, dateTime: new Date });
return this.snackBar.open(translatedMessage, null, config); return this.snackBar.open(translatedMessage, null, config);
@@ -94,9 +97,12 @@ export class NotificationService {
return this.snackBar.dismiss(); return this.snackBar.dismiss();
} }
protected showMessage(message: string, panelClass: string, action?: string): MatSnackBarRef<any> { protected showMessage(message: string, panelClass: string, action?: string, interpolateArgs?: any): MatSnackBarRef<any> {
message = this.translationService.instant(message); message = this.translationService.instant(message, interpolateArgs);
return this.openMessageBar(message, panelClass, action);
}
private openMessageBar(message: string, panelClass: string, action?: string): MatSnackBarRef<any> {
this.messages.next({ message: message, dateTime: new Date }); this.messages.next({ message: message, dateTime: new Date });
return this.snackBar.open(message, action, { return this.snackBar.open(message, action, {
@@ -119,8 +125,8 @@ export class NotificationService {
* @param message Text message or translation key for the message. * @param message Text message or translation key for the message.
* @param action Action name * @param action Action name
*/ */
showInfo(message: string, action?: string): MatSnackBarRef<any> { showInfo(message: string, action?: string, interpolateArgs?: any): MatSnackBarRef<any> {
return this.showMessage(message, 'adf-info-snackbar', action); return this.showMessage(message, 'adf-info-snackbar', action, interpolateArgs);
} }
/** /**