Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <a [routerLink]="['/customers/single', this.customer.id]"></a>
  2.  
  3. export interface CustomerDetails {
  4. id?: number;
  5. name: string;
  6. surname: string,
  7. email: string;
  8. phoneNumber: string;
  9. status: string;
  10. username: string;
  11. }
  12.  
  13. import {Component, OnInit} from '@angular/core';
  14. import {ActivatedRoute, Router} from '@angular/router';
  15. import {CustomerService} from "../shared/customer.service";
  16. import {Observable} from "rxjs";
  17. import {CustomerDetails} from "../model/customer-details";
  18.  
  19. @Component({
  20. selector: 'app-customer-single',
  21. templateUrl: './customer-single.component.html',
  22. styleUrls: ['./customer-single.component.css']
  23. })
  24. export class CustomerSingleComponent implements OnInit {
  25.  
  26. customer$: Observable<CustomerDetails>;
  27. customerDetails$: Observable<CustomerDetails>;
  28. customer: CustomerDetails;
  29.  
  30. constructor(private customerService: CustomerService,
  31. private activatedRoute: ActivatedRoute,
  32. private router: Router) {
  33. }
  34.  
  35. ngOnInit() {
  36. const id = this.activatedRoute.snapshot.params['id'];
  37. this.customer$ = this.customerService.getOne(id);
  38. }
  39.  
  40. onPreDetailsCustomer() {
  41. const id = this.activatedRoute.snapshot.params['id'];
  42. this.customerDetails$ = this.customerService.getOne(id);
  43. }
  44.  
  45. }
  46.  
  47. <div>
  48. <table class="table" style="width: 90%">
  49. <thead class="table">
  50. <tr>
  51. <th scope="col">No.</th>
  52. <th scope="col" width="10%">Name</th>
  53. <th scope="col" width="10%">Surname</th>
  54. <th scope="col" width="10%">Phone number</th>
  55. <th scope="col" width="10%">Email</th>
  56. <th scope="col" width="10%">NIP</th>
  57. <th scope="col">&nbsp;</th>
  58. <th scope="col">&nbsp;</th>
  59. <th scope="col">&nbsp;</th>
  60. <th scope="col">&nbsp;</th>
  61. </tr>
  62. </thead>
  63. <tbody>
  64. <tr *ngFor="let customer of ( customers$ | async ); let i = index">
  65. <td scope="row">{{i + 1}}</td>
  66. <td>{{customer.name}}</td>
  67. <td>{{customer.surname}}</td>
  68. <td>{{customer.phoneNumber}}</td>
  69. <td>{{customer.email}}</td>
  70. <td>{{customer.nip}}</td>
  71. <td>
  72. <a [routerLink]="['/customers/modify', customer.id]">
  73. <button mat-raised-button color="primary">Modify</button>
  74. </a>
  75. <button mat-raised-button color="accent" (click)="openDialog()">Details</button>
  76. <a (click)="onRemove(customer.id)">
  77. <button mat-raised-button color="warn">Remove</button>
  78. </a>
  79. <button type="button" class="btn btn-dark" routerLink="products">
  80. <span class="btn-label"><i class="fas fa-cart-plus"></i></span>Compose basket
  81. </button>
  82. </td>
  83. </tr>
  84. </tbody>
  85. </table>
  86. </div>
  87.  
  88. import {Component, OnInit} from '@angular/core';
  89. import {CustomerService} from "../shared/customer.service";
  90. import {Customer} from '../model/customer';
  91. import {ActivatedRoute, Router} from "@angular/router";
  92. import {Observable} from "rxjs";
  93. import {CustomerDetails} from "../model/customer-details";
  94. import {CustomerDetailsDialog} from "../modal/customer-details/customer-details-dialog";
  95. import {MatDialog} from "@angular/material";
  96.  
  97. @Component({
  98. selector: 'app-customer-list',
  99. templateUrl: './customer-list.component.html',
  100. styleUrls: ['./customer-list.component.css']
  101. })
  102. export class CustomerListComponent implements OnInit {
  103.  
  104. customers$: Observable<Customer[]>;
  105. customerDetails$: Observable<CustomerDetails>;
  106.  
  107.  
  108. constructor(private customerService: CustomerService, private router: Router, private activatedRoute: ActivatedRoute, public dialog: MatDialog) {
  109. }
  110.  
  111. ngOnInit() {
  112. this.customers$ = this.customerService.customersView();
  113. }
  114.  
  115. onRemove(id: number) {
  116. this.customerService
  117. .remove(id)
  118. .subscribe(
  119. customer => console.log('Customer ' + customer + ' successfully removed'),
  120. error => console.log('Something went terribly wrong: ' + error.message),
  121. () => setTimeout(() => this.ngOnInit(), 150)
  122. );
  123. }
  124.  
  125. onPreDetailsCustomer(id: number) {
  126. this.customerDetails$ = this.customerService.getOne(id);
  127. }
  128.  
  129. openDialog() {
  130. const dialog = this.dialog.open(CustomerDetailsDialog);
  131.  
  132. dialog.afterClosed().subscribe(result => {
  133. console.log(`Dialog result: ${result}`);
  134. });
  135. }
  136.  
  137. }
  138.  
  139. @Component({
  140. selector: 'customer-details-dialog',
  141. templateUrl: 'customer-details-dialog.html',
  142. })
  143. export class CustomerDetailsDialog {
  144.  
  145. customer$: Observable<CustomerDetails>;
  146.  
  147. constructor(public dialog: MatDialog,
  148. private customerService: CustomerService,
  149. private activatedRoute: ActivatedRoute,
  150. private router: Router) {
  151. }
  152.  
  153. ngOnInit() {
  154.  
  155. const id = this.activatedRoute.snapshot.params['id'];
  156. this.customer$ = this.customerService.getOne(id);
  157. }
  158.  
  159. }
  160.  
  161. <div *ngIf="(this.customer$ | async) as customer; else loading">
  162. {{customer.name}}
  163. </div>
  164.  
  165. <div *ngIf="(this.customer$ | async) as customer; else loading">
  166. {{customer.email}}
  167. </div>
  168.  
  169. <div *ngIf="(this.customer$ | async) as customer; else loading">
  170. {{customer.phoneNumber}}
  171. </div>
  172.  
  173. <ng-template #loading>
  174. Loading stuff in ngIf...
  175. </ng-template>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement