Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { Component, OnInit } 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 { FamilyService } from '../../services/family-service';
  17. import { Danka } from '../../models/danka';
  18. import { Kakocho } from '../../models/kakocho';
  19. import { Family } from '../../models/family';
  20. @Component({
  21. selector: 'app-kakocho-edit',
  22. imports: [
  23. AppHeader,
  24. AppSideMenu,
  25. ReactiveFormsModule,
  26. ],
  27. templateUrl: './kakocho-edit.html',
  28. styleUrl: './kakocho-edit.scss',
  29. })
  30. export class KakochoEdit implements OnInit {
  31. danka?: Danka;
  32. kakocho?: Kakocho;
  33. sourceFamily?: Family;
  34. kakochoForm: FormGroup;
  35. dankaId: string | undefined;
  36. returnTab: 'family' | 'kakocho' = 'kakocho';
  37. constructor(
  38. private fb: FormBuilder,
  39. private dankaService: DankaService,
  40. private kakochoService: KakochoService,
  41. private familyService: FamilyService,
  42. private route: ActivatedRoute,
  43. private router: Router,
  44. ) {
  45. this.kakochoForm = this.fb.group({
  46. name: ['', Validators.required],
  47. furigana: [''],
  48. relationship: [''],
  49. kaimyo: [''],
  50. deathDate: [''],
  51. ageAtDeath: [''],
  52. note: [''],
  53. });
  54. }
  55. ngOnInit(): void {
  56. this.init();
  57. }
  58. async init(): Promise<void> {
  59. const dankaId = this.route.snapshot.params['dankaId'];
  60. const familyId = this.route.snapshot.queryParams['familyId'];
  61. const kakochoId = this.route.snapshot.params['kakochoId'];
  62. this.dankaId = dankaId;
  63. if (dankaId) {
  64. this.danka = await this.dankaService.getDankaById(dankaId);
  65. }
  66. if (familyId) {
  67. this.sourceFamily = await this.familyService.getFamilyById(familyId);
  68. this.returnTab = 'family';
  69. }
  70. // 編集モード
  71. if (kakochoId) {
  72. this.kakocho = await this.kakochoService.getKakochoById(kakochoId);
  73. if (this.kakocho) {
  74. this.kakochoForm.patchValue({
  75. name: this.kakocho.name,
  76. furigana: this.kakocho.furigana,
  77. relationship: this.kakocho.relationship,
  78. kaimyo: this.kakocho.kaimyo,
  79. deathDate: this.kakocho.deathDate,
  80. ageAtDeath: this.kakocho.ageAtDeath,
  81. note: this.kakocho.note,
  82. });
  83. }
  84. }
  85. // 新規(家族からの引き継ぎ)
  86. else if (this.sourceFamily) {
  87. this.kakochoForm.patchValue({
  88. name: this.sourceFamily.name,
  89. furigana: this.sourceFamily.furigana,
  90. relationship: this.sourceFamily.relationship,
  91. });
  92. }
  93. }
  94. async saveKakocho(): Promise<void> {
  95. // form値取得
  96. const formValue = this.kakochoForm.value;
  97. // 編集
  98. if (this.kakocho) {
  99. const updatedKakocho: Kakocho = {
  100. ...this.kakocho,
  101. ...formValue,
  102. };
  103. await this.kakochoService.updateKakocho(
  104. updatedKakocho
  105. );
  106. } else {
  107. // 新規追加
  108. const newKakocho: Kakocho = {
  109. id: crypto.randomUUID(),
  110. dankaId: this.danka?.id ?? '',
  111. familyId: this.sourceFamily?.id ?? '',
  112. name: formValue.name,
  113. furigana: formValue.furigana,
  114. relationship: formValue.relationship,
  115. kaimyo: formValue.kaimyo,
  116. deathDate: formValue.deathDate,
  117. ageAtDeath: formValue.ageAtDeath,
  118. note: formValue.note,
  119. };
  120. this.kakochoService.addKakocho(
  121. newKakocho
  122. );
  123. if (this.sourceFamily) {
  124. // await this.familyService.deleteFamily(this.sourceFamily.id);
  125. this.sourceFamily.death = true;
  126. await this.familyService.saveFamily(this.sourceFamily);
  127. }
  128. }
  129. // 一覧へ戻る
  130. this.router.navigate([
  131. '/danka-detail',
  132. this.danka?.id,
  133. ], { queryParams: { tab: 'kakocho' } });
  134. }
  135. deleteKakocho(): void {
  136. if (!this.kakocho) {
  137. return;
  138. }
  139. this.kakochoService.deleteKakocho(this.kakocho.id);
  140. this.router.navigate(['/danka-detail', this.danka?.id], { queryParams: { tab: 'kakocho' } });
  141. }
  142. cancelKakochoEdit() {
  143. this.router.navigate(['/danka-detail', this.danka?.id], { queryParams: { tab: this.returnTab } });
  144. }
  145. }