-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new builder pattern to handle pre-grouping
`// Create parent toastconst parent = new HotToastBuilder(template, toast) .setOptions({ position: 'top-right', className: 'custom-class' });// Add childrennotifications.map(n => new HotToastBuilder(n.message, toast) .setOptions(n.options)).forEach(child => parent.addChild(child));// Create and show when readyconst ref = parent.create();ref.afterGroupRefsAttached.subscribe(() => ref.show());` fix #9
- Loading branch information
1 parent
39347a7
commit 8b59a9a
Showing
8 changed files
with
148 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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,86 @@ | ||
import { Content } from '@ngneat/overview'; | ||
import { CreateHotToastRef, ToastOptions } from './hot-toast.model'; | ||
import { HotToastService } from './hot-toast.service'; | ||
import { Observable } from 'rxjs'; | ||
|
||
type ToastMethod = 'show' | 'success' | 'error' | 'warning' | 'info' | 'loading'; | ||
|
||
export class HotToastBuilder<DataType = unknown> { | ||
private options: ToastOptions<DataType>; | ||
private groupChildren: HotToastBuilder<unknown>[] = []; | ||
private toastRef: CreateHotToastRef<DataType>; | ||
|
||
constructor(private message: Content, private service: HotToastService) { | ||
this.options = {}; | ||
} | ||
|
||
setOptions(options: ToastOptions<DataType>): this { | ||
this.options = { ...this.options, ...options }; | ||
return this; | ||
} | ||
|
||
addChild(child: HotToastBuilder<unknown>): this { | ||
this.groupChildren.push(child); | ||
return this; | ||
} | ||
|
||
get afterGroupRefsAttached(): Observable<CreateHotToastRef<unknown>[]> { | ||
return this.toastRef?.afterGroupRefsAttached; | ||
} | ||
|
||
private addChildrenToOptions() { | ||
if (this.groupChildren.length > 0) { | ||
const children = this.groupChildren.map((child) => ({ | ||
options: { | ||
message: child.message, | ||
...child.options, | ||
}, | ||
})); | ||
|
||
this.options.group = { | ||
...this.options.group, | ||
children, | ||
}; | ||
} | ||
} | ||
|
||
// Create method that creates but doesn't show the toast. Call show() to show the toast. | ||
create(method: ToastMethod = 'show'): CreateHotToastRef<DataType> { | ||
this.addChildrenToOptions(); | ||
this.toastRef = this.service[method](this.message, { | ||
...this.options, | ||
...{ visible: false }, | ||
}) as CreateHotToastRef<DataType>; | ||
return this.toastRef; | ||
} | ||
|
||
private createToast(method: ToastMethod): CreateHotToastRef<DataType> { | ||
this.addChildrenToOptions(); | ||
this.toastRef = this.service[method](this.message, this.options) as CreateHotToastRef<DataType>; | ||
return this.toastRef; | ||
} | ||
|
||
show(): CreateHotToastRef<DataType> { | ||
return this.createToast('show'); | ||
} | ||
|
||
success(): CreateHotToastRef<DataType> { | ||
return this.createToast('success'); | ||
} | ||
|
||
error(): CreateHotToastRef<DataType> { | ||
return this.createToast('error'); | ||
} | ||
|
||
warning(): CreateHotToastRef<DataType> { | ||
return this.createToast('warning'); | ||
} | ||
|
||
info(): CreateHotToastRef<DataType> { | ||
return this.createToast('info'); | ||
} | ||
|
||
loading(): CreateHotToastRef<DataType> { | ||
return this.createToast('loading'); | ||
} | ||
} |
This file contains 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 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 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 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 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