1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| import { Component, OnInit } from '@angular/core'; import { ListService, PagedResultDto } from '@abp/ng.core'; import { AuthorService, AuthorDto } from '@proxy/authors'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { NgbDateNativeAdapter, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap'; import { ConfirmationService, Confirmation } from '@abp/ng.theme.shared';
@Component({ selector: 'app-author', templateUrl: './author.component.html', styleUrls: ['./author.component.scss'], providers: [ListService, { provide: NgbDateAdapter, useClass: NgbDateNativeAdapter }], }) export class AuthorComponent implements OnInit { author = { items: [], totalCount: 0 } as PagedResultDto<AuthorDto>;
isModalOpen = false;
form: FormGroup;
selectedAuthor = {} as AuthorDto;
constructor( public readonly list: ListService, private authorService: AuthorService, private fb: FormBuilder, private confirmation: ConfirmationService ) {}
ngOnInit(): void { const authorStreamCreator = (query) => this.authorService.getList(query);
this.list.hookToQuery(authorStreamCreator).subscribe((response) => { this.author = response; }); }
createAuthor() { this.selectedAuthor = {} as AuthorDto; this.buildForm(); this.isModalOpen = true; }
editAuthor(id: string) { this.authorService.get(id).subscribe((author) => { this.selectedAuthor = author; this.buildForm(); this.isModalOpen = true; }); }
buildForm() { this.form = this.fb.group({ name: [this.selectedAuthor.name || '', Validators.required], birthDate: [ this.selectedAuthor.birthDate ? new Date(this.selectedAuthor.birthDate) : null, Validators.required, ], shortBio: [this.selectedAuthor.shortBio || ''] }); }
save() { if (this.form.invalid) { return; }
if (this.selectedAuthor.id) { this.authorService .update(this.selectedAuthor.id, this.form.value) .subscribe(() => { this.isModalOpen = false; this.form.reset(); this.list.get(); }); } else { debugger; var input = this.form.value this.authorService.create(this.form.value).subscribe(() => { this.isModalOpen = false; this.form.reset(); this.list.get(); }); } }
delete(id: string) { this.confirmation.warn('::AreYouSureToDelete', '::AreYouSure') .subscribe((status) => { if (status === Confirmation.Status.confirm) { this.authorService.delete(id).subscribe(() => this.list.get()); } }); } }
|