Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

danka-edit.ts 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Component, inject } from '@angular/core';
  2. import {
  3. FormBuilder,
  4. FormGroup,
  5. FormControl,
  6. FormArray,
  7. ReactiveFormsModule,
  8. Validators,
  9. } from '@angular/forms';
  10. import { ActivatedRoute, Router, RouterLink } from '@angular/router';
  11. import { AppHeader } from '../../share/header/app-header';
  12. import { AppSideMenu } from '../../share/side-menu/app-side-menu';
  13. import { DankaService } from '../../services/dankaService';
  14. import { Danka } from '../../models/danka';
  15. @Component({
  16. selector: 'app-danka-edit',
  17. imports: [AppHeader, AppSideMenu, ReactiveFormsModule, RouterLink],
  18. templateUrl: './danka-edit.html',
  19. styleUrl: './danka-edit.scss',
  20. })
  21. export class DankaEdit {
  22. danka: Danka | undefined;
  23. dankaForm = new FormGroup({
  24. householdName: new FormControl('鈴木家'),
  25. householder: new FormControl('鈴木 太郎'),
  26. postalCode: new FormControl('123-4567'),
  27. address: new FormControl('市内 1-2-3'),
  28. phones: new FormArray([
  29. this.createPhoneForm('03-xxxx-xxxx', '自宅'),
  30. this.createPhoneForm('090-xxxx-xxxx', '世帯主'),
  31. this.createPhoneForm('0584-xx-xxxx', '寺報連絡'),
  32. ]),
  33. });
  34. constructor(
  35. private dankaService: DankaService,
  36. private route: ActivatedRoute,
  37. private router: Router,
  38. ) {
  39. const id = this.route.snapshot.params['id'];
  40. if (id) {
  41. this.danka = this.dankaService.getDankaById(id);
  42. if (this.danka) {
  43. this.dankaForm.patchValue({
  44. householdName: this.danka.householdName,
  45. householder: this.danka.householder,
  46. postalCode: this.danka.postalCode,
  47. address: this.danka.address,
  48. });
  49. this.phones.clear();
  50. for (const phone of this.danka.phones) {
  51. this.phones.push(this.createPhoneForm(phone.tel, phone.note));
  52. }
  53. }
  54. }
  55. console.log(this.danka);
  56. }
  57. get phones() {
  58. return this.dankaForm.get('phones') as FormArray;
  59. }
  60. createPhoneForm(string = '', note = '') {
  61. return new FormGroup({
  62. tel: new FormControl(string),
  63. note: new FormControl(note),
  64. });
  65. }
  66. addPhone() {
  67. this.phones.push(this.createPhoneForm());
  68. }
  69. removePhone(index: number) {
  70. this.phones.removeAt(index);
  71. }
  72. saveDanka() {
  73. if (!this.danka) {
  74. return;
  75. }
  76. const formValue = this.dankaForm.value;
  77. const updatedDanka = {
  78. id: this.danka.id,
  79. householdName: formValue.householdName ?? '',
  80. householder: formValue.householder ?? '',
  81. postalCode: formValue.postalCode ?? '',
  82. address: formValue.address ?? '',
  83. phones: (formValue.phones ?? []).map((phone) => ({
  84. tel: phone.tel ?? '',
  85. note: phone.note ?? '',
  86. })),
  87. };
  88. this.dankaService.saveDanka(updatedDanka);
  89. this.router.navigate(['./danka-detail', this.danka.id]);
  90. console.log(this.dankaService.getDankaList());
  91. }
  92. }