Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

kakocho-edit.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { Component } from '@angular/core';
  2. import {
  3. FormBuilder,
  4. FormGroup,
  5. ReactiveFormsModule,
  6. Validators,
  7. } from '@angular/forms';
  8. import {
  9. ActivatedRoute,
  10. Router,
  11. } from '@angular/router';
  12. import { AppHeader } from '../../share/header/app-header';
  13. import { AppSideMenu } from '../../share/side-menu/app-side-menu';
  14. import { DankaService } from '../../services/dankaService';
  15. import { KakochoService } from '../../services/kakocho-service';
  16. import { Danka } from '../../models/danka';
  17. import { Kakocho } from '../../models/kakocho';
  18. @Component({
  19. selector: 'app-kakocho-edit',
  20. imports: [
  21. AppHeader,
  22. AppSideMenu,
  23. ReactiveFormsModule,
  24. ],
  25. templateUrl: './kakocho-edit.html',
  26. styleUrl: './kakocho-edit.scss',
  27. })
  28. export class KakochoEdit {
  29. danka?: Danka;
  30. kakocho?: Kakocho;
  31. kakochoForm: FormGroup;
  32. dankaId: string;
  33. constructor(
  34. private fb: FormBuilder,
  35. private dankaService: DankaService,
  36. private kakochoService: KakochoService,
  37. private route: ActivatedRoute,
  38. private router: Router,
  39. ) {
  40. // フォーム初期化
  41. this.kakochoForm = this.fb.group({
  42. name: ['', Validators.required],
  43. furigana: [''],
  44. relationship: [''],
  45. kaimyo: [''],
  46. deathDate: [''],
  47. ageAtDeath: [''],
  48. note: [''],
  49. });
  50. // 檀家ID
  51. const dankaId = this.route.snapshot.params['dankaId'];
  52. this.dankaId = this.route.snapshot.params['dankaId'];
  53. if (dankaId) {
  54. this.danka =
  55. this.dankaService.getDankaById(dankaId);
  56. }
  57. // 編集対象ID
  58. const kakochoId =
  59. this.route.snapshot.params['kakochoId'];
  60. // 編集モード
  61. if (kakochoId) {
  62. this.kakocho =
  63. this.kakochoService.getKakochoById(kakochoId);
  64. if (this.kakocho) {
  65. this.kakochoForm.patchValue({
  66. name: this.kakocho.name,
  67. furigana: this.kakocho.furigana,
  68. relationship: this.kakocho.relationship,
  69. kaimyo: this.kakocho.kaimyo,
  70. deathDate: this.kakocho.deathDate,
  71. ageAtDeath: this.kakocho.ageAtDeath,
  72. note: this.kakocho.note,
  73. });
  74. }
  75. }
  76. }
  77. saveKakocho(): void {
  78. // form値取得
  79. const formValue = this.kakochoForm.value;
  80. // 編集
  81. if (this.kakocho) {
  82. const updatedKakocho: Kakocho = {
  83. ...this.kakocho,
  84. ...formValue,
  85. };
  86. this.kakochoService.updateKakocho(
  87. updatedKakocho
  88. );
  89. } else {
  90. // 新規追加
  91. const newKakocho: Kakocho = {
  92. id: crypto.randomUUID(),
  93. dankaId: this.danka?.id ?? '',
  94. familyId: '',
  95. name: formValue.name,
  96. furigana: formValue.furigana,
  97. relationship: formValue.relationship,
  98. kaimyo: formValue.kaimyo,
  99. deathDate: formValue.deathDate,
  100. ageAtDeath: formValue.ageAtDeath,
  101. note: formValue.note,
  102. };
  103. this.kakochoService.addKakocho(
  104. newKakocho
  105. );
  106. }
  107. // 一覧へ戻る
  108. this.router.navigate([
  109. '/danka-detail',
  110. this.danka?.id,
  111. ], { queryParams: { tab: 'kakocho' } });
  112. }
  113. deleteKakocho(): void {
  114. if (!this.kakocho) {
  115. return;
  116. }
  117. this.kakochoService.deleteKakocho(this.kakocho.id);
  118. this.router.navigate(['/danka-detail', this.danka?.id], { queryParams: { tab: 'kakocho' } });
  119. }
  120. cancelKakochoEdit() {
  121. this.router.navigate(['/danka-detail', this.danka?.id], { queryParams: { tab: 'kakocho' } }); }
  122. }