-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcustomer-create.component.ts
48 lines (40 loc) · 1.98 KB
/
customer-create.component.ts
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
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DrestaurantCustomerService, CustomerModel, EventManager, MyEvent } from '@d-restaurant-frontend/drestaurant-shared';
@Component({
selector: 'd-restaurant-frontend-customer-create',
templateUrl: './customer-create.component.html',
styleUrls: ['./customer-create.component.scss']
})
export class CustomerCreateComponent implements OnInit {
form: FormGroup;
constructor(private customerService: DrestaurantCustomerService, private formBuilder: FormBuilder, private eventManager: EventManager) { }
ngOnInit() {
this.form = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
orderLimit: [10, Validators.compose([Validators.required, Validators.min(1), Validators.max(1000)])]
});
}
onSubmit({ value, valid }: { value: CustomerModel; valid: boolean }) {
this.customerService
.createCustomer(value)
.subscribe(
response => this.onSaveSuccess(response),
() => this.onSaveError()
);
}
private onSaveSuccess(result) {
// Note that command for creating a Customer (command side) and materializing the CustomerEntity (query side - event handler) can happen in different threads (no transaction). We should wait websocket event from the backend marking that view has been materialized. We are 'eventually' consistent, and we have to handle it accordingly.
// We do not need to fire event here in order for the list to be refreshed
// because: STOMP message will be sent over the WebSocket protocol once the Customer is saved into the database on the backend side
// check: 'customer-list.datasource.ts' to see how we subscribe to WebSocket event (Customers list updated)
// this.eventManager.broadcast({
// name: MyEvent.CUSTOMER_LIST_MODIFICATION,
// content: 'OK'
// });
}
private onSaveError() {
//Do something smart
}
}