Created
April 3, 2021 00:59
-
-
Save guiseek/d8e593163b15fef41c53d8f94701e32b to your computer and use it in GitHub Desktop.
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
import { MatDialog } from '@angular/material/dialog'; | |
import { ConfirmDialogComponent } from '@webdev/ui'; | |
import { Injector } from '@angular/core'; | |
/** | |
* Decorator para dialog de confirmação | |
* @param message | |
*/ | |
export function Confirmador(message: string) { | |
/** | |
* @param target é a classe, no nosso claso o componente ListaComponent | |
* @param key é o método qual o decorator está interceptando | |
* @param descriptor pode ser usado para observar, modificar ou substituir as definições de um acessador | |
*/ | |
return function( | |
target: any, | |
key: string | symbol, | |
descriptor: PropertyDescriptor | |
) { | |
const original = descriptor.value; | |
descriptor.value = function(...args: any[]) { | |
const injector = Injector.create({ | |
providers: [{ provide: MatDialog, deps: [] }] | |
}); | |
target.dialog = injector.get(MatDialog); | |
const ref = this.dialog.open(ConfirmDialogComponent, { | |
data: message.replace('{item}', args[1].nome) | |
}); | |
ref.afterClosed().subscribe(result => { | |
if (result) { | |
console.log('result: ', result); | |
return original.apply(this, args); | |
} | |
return null; | |
}); | |
}; | |
return descriptor; | |
}; | |
} |
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
import { Input, Output, Component, EventEmitter } from '@angular/core'; | |
import { ComTaxa, Confirmador } from '@webdev/utils'; | |
import { Item } from '@webdev/api-interfaces'; | |
import { MatDialog } from '@angular/material'; | |
@Component({ | |
selector: 'webdev-lista', | |
templateUrl: './lista.component.html', | |
styleUrls: ['./lista.component.css'], | |
providers: [MatDialog] | |
}) | |
export class ListaComponent { | |
@Input() lista: Item[]; | |
@Output() onRemove = new EventEmitter<number>(); | |
constructor(public dialog: MatDialog) {} | |
@ComTaxa(0.15) | |
get total() { | |
return this.lista.reduce((prev, cur) => prev + cur.preco, 0); | |
} | |
/** | |
* Decorator de confirmação | |
* | |
* @param {number} index | |
* @param {Item} item Deve ser mantido, pois é usado no decorator | |
* @memberof ListaComponent | |
*/ | |
@Confirmador('Tem certeza que deseja remover {item} ?') | |
remove(index: number, item: Item) { | |
this.onRemove.emit(index); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment