File
Implements
Metadata
selector |
edit-employee |
templateUrl |
./edit-employee.component.html |
Methods
cancelClicked
|
cancelClicked()
|
|
|
deleteClicked
|
deleteClicked()
|
|
|
ngOnDestroy
|
ngOnDestroy()
|
|
|
saveClicked
|
saveClicked()
|
|
|
sub
|
sub: Subscription
|
Type : Subscription
|
|
import { Component, OnDestroy } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { switchMap, share } from 'rxjs/operators';
import { EmployeeApi } from '@enterprise-example/employee-api';
import { EmployeeNavigation } from '../employee-navigation.service';
@Component({
selector: 'edit-employee',
templateUrl: './edit-employee.component.html'
})
export class EditEmployeeComponent implements OnDestroy {
containerFormGroup: FormGroup;
sub: Subscription;
id: number;
constructor(private nav: EmployeeNavigation,
private api: EmployeeApi, fb: FormBuilder, route: ActivatedRoute) {
this.nav.calculateModuleBaseRoute(route);
this.containerFormGroup = fb.group({});
const employee$ = nav.employeeId(route)
.pipe(switchMap(id => api.loadOne(id)),
share());
this.sub = employee$.subscribe(e => {
this.id = e.id;
const { first_name, last_name, email, hourly_wage } = e;
const hours_worked = e.hours_worked || 0;
this.containerFormGroup.setValue({
employee: { first_name, last_name, email, hourly_wage, hours_worked }
});
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
cancelClicked() {
this.nav.list();
}
saveClicked() {
this.api.save({
...this.containerFormGroup.value.employee,
id: this.id
}).subscribe(_x => this.nav.list());
}
deleteClicked() {
this.api.delete(this.id).subscribe(_x => this.nav.list());
}
}
<section type="button" class="card">
<div class="card-content">
<div class="card-title">Edit employee</div>
<form [formGroup]="containerFormGroup">
<employee-fields [parentFormGroup]="containerFormGroup"></employee-fields>
<button type="button" class="btn" (click)="deleteClicked()"
[class.disabled]="false">Delete</button>
<button type="button" class="btn" (click)="saveClicked()"
[class.disabled]="false">Save</button>
<button type="button" class="btn" (click)="cancelClicked()"
[class.disabled]="false">Cancel</button>
</form>
</div>
</section>
Legend
Html element with directive