-
Notifications
You must be signed in to change notification settings - Fork 3
SED-3222 add custom toast #910
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
Draft
lukasz-lepa
wants to merge
10
commits into
master
Choose a base branch
from
SED-3222-duplicate-edit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d71a0db
SED-3222 add custom toast
lukasz-lepa 6db75d4
SED-3222 set styles, fix bugs
lukasz-lepa 155367c
SED-3222 add icon
lukasz-lepa 8d68e1b
Update projects/step-core/src/lib/components/toast-container/toast-co…
lukasz-lepa 6b4d8f4
Update projects/step-core/src/lib/components/toast/toast.component.html
lukasz-lepa 49025bb
SED-3222 fixes after code review
lukasz-lepa 6f010de
Merge branch 'SED-3222-duplicate-edit' of github.com:exense/step-fron…
lukasz-lepa 1760538
SED-3222 use signals
lukasz-lepa 0a5892e
SED-3222 fix issue
lukasz-lepa f576aaf
SED-3222 modify styling
lukasz-lepa 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
14 changes: 14 additions & 0 deletions
14
projects/step-core/src/lib/components/toast-container/toast-container.component.html
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,14 @@ | ||
| <div class="toast-container"> | ||
| @for (toast of _toasts(); track toast.message) { | ||
| <step-toast | ||
| [type]="toast.type" | ||
| [message]="toast.message" | ||
| [values]="toast.values" | ||
| [entity]="toast.entity" | ||
| [entityName]="toast.entityName" | ||
| [actions]="toast.actions" | ||
| [autoClose]="!!toast.autoClose" | ||
| [duration]="toast.duration" | ||
| /> | ||
| } | ||
| </div> |
9 changes: 9 additions & 0 deletions
9
projects/step-core/src/lib/components/toast-container/toast-container.component.scss
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,9 @@ | ||
| .toast-container { | ||
| position: fixed; | ||
| top: 2rem; | ||
| right: 2rem; | ||
| z-index: 1000; | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 1rem; | ||
| } |
11 changes: 11 additions & 0 deletions
11
projects/step-core/src/lib/components/toast-container/toast-container.component.ts
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,11 @@ | ||
| import { Component, inject } from '@angular/core'; | ||
| import { ToastService } from '../../services/toast.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'step-toast-container', | ||
| templateUrl: './toast-container.component.html', | ||
| styleUrls: ['./toast-container.component.scss'], | ||
| }) | ||
| export class ToastContainerComponent { | ||
| protected readonly _toasts = inject(ToastService).toastMessages; | ||
| } |
25 changes: 25 additions & 0 deletions
25
projects/step-core/src/lib/components/toast/toast.component.html
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,25 @@ | ||
| @if (message) { | ||
| <div class="toast"> | ||
| <div class="toast-message"> | ||
| @if (entity) { | ||
| <entity-icon [entity]="entity" [entityName]="entityName" /> | ||
| } | ||
| <ng-container *ngFor="let part of messageParts"> | ||
| <span *ngIf="part.isPlaceholder" [matTooltip]="part.originalValue">{{ part.displayValue }}</span> | ||
| <span *ngIf="!part.isPlaceholder">{{ part }}</span> | ||
| </ng-container> | ||
| </div> | ||
| <div class="toast-bottom"> | ||
| <div class="toast-actions"> | ||
| @for (action of actions; track action.label) { | ||
| <a (click)="action.handler()" class="action-btn" type="button" color="primary"> | ||
| {{ action.label }} | ||
| </a> | ||
| } | ||
| </div> | ||
| <div class="toast-close"> | ||
| <button class="dismiss-btn" (click)="closeToast()">close</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| } |
72 changes: 72 additions & 0 deletions
72
projects/step-core/src/lib/components/toast/toast.component.scss
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,72 @@ | ||
| @use 'projects/step-core/styles/core-variables' as var; | ||
|
|
||
| .toast { | ||
| display: flex; | ||
| align-items: center; | ||
| background-color: var.$white; | ||
| border: 1px solid var.$light-white-gray; | ||
| padding: 0.5rem; | ||
| border-radius: 1rem; | ||
| box-shadow: 0 0.2rem 1rem rgba(0, 0, 0, 0.1); | ||
| position: relative; | ||
| flex-direction: column; | ||
| gap: 0.5rem; | ||
|
|
||
| .action-icons { | ||
| padding: 0 0.2rem; | ||
| display: flex; | ||
| width: 100%; | ||
| justify-content: space-between; | ||
| } | ||
|
|
||
| .success-icon { | ||
| margin-right: 10px; | ||
| color: var.$green-650; | ||
| } | ||
|
|
||
| .error-icon, | ||
| .warning-icon { | ||
| margin-right: 10px; | ||
| color: var.$red-100; | ||
| } | ||
|
|
||
| .info-icon { | ||
| margin-right: 10px; | ||
| color: black; | ||
| } | ||
|
|
||
| .toast-message { | ||
| flex: 1; | ||
| padding: 1rem 2rem 1rem 1rem; | ||
| } | ||
|
|
||
| .toast-bottom { | ||
| display: flex; | ||
| width: 100%; | ||
| justify-content: space-between; | ||
|
|
||
| .toast-actions { | ||
| display: flex; | ||
| width: 100%; | ||
| padding: 0 0.2rem; | ||
| justify-content: start; | ||
| padding-left: 1rem; | ||
| gap: 1rem; | ||
|
|
||
| .action-btn { | ||
| cursor: pointer; | ||
| } | ||
| } | ||
|
|
||
| .toast-close { | ||
| padding-right: 1rem; | ||
| } | ||
| } | ||
|
|
||
| .dismiss-btn { | ||
| background: none; | ||
| border: none; | ||
| cursor: pointer; | ||
| padding: 0; | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
projects/step-core/src/lib/components/toast/toast.component.ts
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 @@ | ||
| import { Component, inject, Input, OnInit } from '@angular/core'; | ||
| import { ToastService } from '../../services/toast.service'; | ||
| import { NotificationAction } from '../../shared/toast-action.interface'; | ||
| import { ToastType } from '../../shared/toast-type.enum'; | ||
| import { Entity } from '../../modules/entity/types/entity'; | ||
|
|
||
| @Component({ | ||
| selector: 'step-toast', | ||
| templateUrl: 'toast.component.html', | ||
| styleUrls: ['toast.component.scss'], | ||
| }) | ||
| export class ToastComponent implements OnInit { | ||
| @Input() type!: ToastType; | ||
| @Input() message: string = ''; | ||
| @Input() actions: NotificationAction[] | undefined = []; | ||
| @Input() duration: number | undefined = 3000; | ||
| @Input() autoClose: boolean = true; | ||
| @Input() values: string[] = []; | ||
| @Input() entity?: Entity; | ||
| @Input() entityName?: string; | ||
| private _toastService = inject(ToastService); | ||
| private timer: any; | ||
| toastType = ToastType; | ||
| messageParts: any[] = []; | ||
|
|
||
| ngOnInit(): void { | ||
| this.formatMessage(); | ||
| if (this.autoClose) { | ||
| this.timer = setTimeout(() => this.closeToast(), this.duration || 3000); | ||
| } | ||
| } | ||
|
|
||
| ngOnDestroy(): void { | ||
| if (this.timer) { | ||
| clearTimeout(this.timer); | ||
| } | ||
| } | ||
|
|
||
| closeToast(): void { | ||
| this._toastService.removeToast(this.message, this.values); | ||
| } | ||
|
|
||
| private formatMessage(): void { | ||
| const formattedMessage = this.message.replace(/{(\d+)}/g, (match, index) => { | ||
| return `{${index}}`; | ||
| }); | ||
|
|
||
| const parts = formattedMessage.split(/(\{\d+\})/g); | ||
| this.messageParts = parts.map((part) => { | ||
| const match = part.match(/\{(\d+)\}/); | ||
| if (match) { | ||
| const index = parseInt(match[1], 10); | ||
| const originalValue = this.values[index]; | ||
| const displayValue = this.truncate(originalValue, 50); | ||
| return { isPlaceholder: true, originalValue, displayValue }; | ||
| } | ||
| return part; | ||
| }); | ||
| } | ||
|
|
||
| private truncate(value: string, maxLength: number): string { | ||
| return value.length > maxLength ? value.substring(0, maxLength) + '...' : value; | ||
| } | ||
| } |
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,49 @@ | ||
| import { Injectable, signal } from '@angular/core'; | ||
| import { NotificationAction } from '../shared/toast-action.interface'; | ||
| import { ToastType } from '../shared/toast-type.enum'; | ||
| import { Entity } from '../modules/entity/types/entity'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root', | ||
| }) | ||
| export class ToastService { | ||
| private toastsInternal = signal< | ||
| { | ||
| type: ToastType; | ||
| message: string; | ||
| values: string[]; | ||
| entity?: Entity; | ||
| entityName?: string; | ||
| actions?: NotificationAction[]; | ||
| autoClose?: boolean; | ||
| duration?: number; | ||
| }[] | ||
| >([]); | ||
| readonly toastMessages = this.toastsInternal.asReadonly(); | ||
|
|
||
| showToast( | ||
| type: ToastType, | ||
| message: string, | ||
| values: string[], | ||
| entity?: Entity, | ||
| entityName?: string, | ||
| actions: NotificationAction[] = [], | ||
| autoClose: boolean = true, | ||
| duration: number = 3000, | ||
| ): void { | ||
| this.toastsInternal.update((toasts) => | ||
| toasts.concat({ type, message, values, entity, entityName, actions, autoClose, duration }), | ||
| ); | ||
| } | ||
|
|
||
| removeToast(message: string, values: string[]): void { | ||
| this.toastsInternal.update((toasts) => | ||
| toasts.filter((toast) => toast.message !== message || !this.arraysEqual(toast.values, values)), | ||
| ); | ||
| } | ||
|
|
||
| private arraysEqual(arr1: string[], arr2: string[]): boolean { | ||
| if (arr1.length !== arr2.length) return false; | ||
| return arr1.every((value, index) => value === arr2[index]); | ||
| } | ||
| } |
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,4 @@ | ||
| export interface NotificationAction { | ||
| label: string; | ||
| handler: () => void; | ||
| } |
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,6 @@ | ||
| export enum ToastType { | ||
| SUCCESS = 'success', | ||
| ERROR = 'error', | ||
| WARNING = 'warning', | ||
| INFO = 'info', | ||
| } |
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
1 change: 1 addition & 0 deletions
1
projects/step-frontend/src/lib/components/root/root.component.html
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| <step-theme></step-theme> | ||
| <step-toast-container></step-toast-container> | ||
| <router-outlet></router-outlet> |
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
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.
Uh oh!
There was an error while loading. Please reload this page.