説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

family-edit.ts 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { Component, inject } from '@angular/core';
  2. import {
  3. FormBuilder,
  4. FormGroup,
  5. FormControl,
  6. ReactiveFormsModule,
  7. Validators,
  8. } from '@angular/forms';
  9. import { OnInit } from '@angular/core';
  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 { FamilyService } from '../../services/family-service';
  15. import { MarriageRelationService } from '../../services/marriage-relation-service';
  16. import { Danka } from '../../models/danka';
  17. import { Family } from '../../models/family';
  18. import { MarriageRelation } from '../../models/marriage-relation';
  19. @Component({
  20. selector: 'app-family-edit',
  21. imports: [AppHeader, AppSideMenu, ReactiveFormsModule],
  22. templateUrl: './family-edit.html',
  23. styleUrl: './family-edit.scss',
  24. })
  25. export class FamilyEdit implements OnInit {
  26. danka: Danka | undefined;
  27. family: Family | undefined;
  28. families: Family[] = [];
  29. dankaId: string = '';
  30. familyId: string | null = null;
  31. relationMode: string = '';
  32. baseFamilyId: string = '';
  33. marriageErrorMessages: string[] = [];
  34. private setHouseholderOnSave = false;
  35. constructor(
  36. private dankaService: DankaService,
  37. private familyService: FamilyService,
  38. private marriageRelationService: MarriageRelationService,
  39. private route: ActivatedRoute,
  40. private router: Router,
  41. ) {
  42. }
  43. ngOnInit(): void {
  44. this.init();
  45. }
  46. async init(): Promise<void> {
  47. this.dankaId = this.route.snapshot.params['dankaId'];
  48. this.familyId = this.route.snapshot.params['familyId'];
  49. this.relationMode = this.route.snapshot.queryParamMap.get('relationMode') ?? '';
  50. this.baseFamilyId = this.route.snapshot.queryParamMap.get('baseFamilyId') ?? '';
  51. // ★ここが重要
  52. this.danka = await this.dankaService.getDankaById(this.dankaId);
  53. this.families = await this.familyService.getFamiliesByDankaId(this.dankaId);
  54. if (this.familyId) {
  55. this.family = await this.familyService.getFamilyById(this.familyId);
  56. if (this.family) {
  57. this.familyForm.patchValue({
  58. furigana: this.family.furigana,
  59. name: this.family.name,
  60. relationship: this.family.relationship,
  61. birthDate: this.family.birthDate,
  62. note: this.family.note,
  63. fatherId: this.family.fatherId,
  64. motherId: this.family.motherId,
  65. spouseId: this.family.spouseId,
  66. gender: this.family.gender,
  67. });
  68. this.patchMarriageRelationFields(this.family.id);
  69. }
  70. }
  71. if (!this.familyId && this.relationMode === 'spouse') {
  72. this.familyForm.patchValue({
  73. spouseId: this.baseFamilyId,
  74. marriageStatus: 'current',
  75. });
  76. }
  77. if (!this.familyId && this.relationMode === 'child') {
  78. const baseFamily = await this.familyService.getFamilyById(this.baseFamilyId);
  79. if (baseFamily?.gender === 'male') {
  80. this.familyForm.patchValue({
  81. fatherId: this.baseFamilyId,
  82. });
  83. }
  84. if (baseFamily?.gender === 'female') {
  85. this.familyForm.patchValue({
  86. motherId: this.baseFamilyId,
  87. });
  88. }
  89. }
  90. }
  91. familyForm = new FormGroup({
  92. furigana: new FormControl('', [Validators.required]),
  93. name: new FormControl('', [Validators.required]),
  94. relationship: new FormControl(''),
  95. birthDate: new FormControl('', Validators.required),
  96. note: new FormControl(''),
  97. fatherId: new FormControl(''),
  98. motherId: new FormControl(''),
  99. spouseId: new FormControl(''),
  100. marriageStatus: new FormControl<MarriageRelation['status']>('current'),
  101. gender: new FormControl('unknown'),
  102. });
  103. getAgeText() {
  104. const birthDate = this.familyForm.get('birthDate')?.value;
  105. if (!birthDate) {
  106. return '生年月日を入力すると自動計算されます';
  107. }
  108. const birth = new Date(birthDate);
  109. const today = new Date();
  110. let age = today.getFullYear() - birth.getFullYear();
  111. const thisYearsBirthday = new Date(today.getFullYear(), birth.getMonth(), birth.getDate());
  112. if (thisYearsBirthday > today) {
  113. age--;
  114. }
  115. return `${age}歳`;
  116. }
  117. getFamilyOptions(): Family[] {
  118. return this.families.filter((family) => family.id !== this.familyId);
  119. }
  120. async patchMarriageRelationFields(familyId: string): Promise<void> {
  121. const relations = await this.marriageRelationService.getMarriageRelationsByFamilyId(familyId);
  122. const relation =
  123. relations.find((marriageRelation) => marriageRelation.status === 'current') ?? relations[0];
  124. if (!relation) return;
  125. this.familyForm.patchValue({
  126. spouseId: relation.person1Id === familyId ? relation.person2Id : relation.person1Id,
  127. marriageStatus: relation.status,
  128. });
  129. }
  130. async findMarriageRelation(
  131. person1Id: string,
  132. person2Id: string
  133. ): Promise<MarriageRelation | undefined> {
  134. const relations = await this.marriageRelationService.getMarriageRelationsByFamilyId(person1Id);
  135. return relations.find(
  136. (relation) =>
  137. (relation.person1Id === person1Id && relation.person2Id === person2Id) ||
  138. (relation.person1Id === person2Id && relation.person2Id === person1Id),
  139. );
  140. }
  141. saveFamily() {
  142. if (this.familyForm.invalid) {
  143. return;
  144. }
  145. this.marriageErrorMessages = [];
  146. const familyId = this.familyId ?? Date.now().toString();
  147. const dankaId = this.dankaId;
  148. const formValue = this.familyForm.value;
  149. const spouseId = formValue.spouseId ?? '';
  150. const marriageStatus = formValue.marriageStatus ?? 'current';
  151. const currentSpouseId = marriageStatus === 'current' ? spouseId : '';
  152. const updatedFamily = {
  153. id: familyId,
  154. dankaId: dankaId,
  155. furigana: formValue.furigana?.trim() ?? '',
  156. name: formValue.name?.trim() ?? '',
  157. relationship: formValue.relationship?.trim() ?? '',
  158. birthDate: formValue.birthDate?.trim() ?? '',
  159. note: formValue.note?.trim() ?? '',
  160. fatherId: this.familyForm.value.fatherId ?? '',
  161. motherId: this.familyForm.value.motherId ?? '',
  162. spouseId: currentSpouseId,
  163. gender: this.familyForm.value.gender ?? 'unknown',
  164. };
  165. if (spouseId) {
  166. const existingRelation = this.findMarriageRelation(familyId, spouseId);
  167. const errors = this.marriageRelationService.saveMarriageRelation({
  168. id: existingRelation?.id ?? Date.now().toString(),
  169. dankaId,
  170. person1Id: familyId,
  171. person2Id: spouseId,
  172. status: marriageStatus,
  173. startDate: existingRelation?.startDate ?? '',
  174. endDate: existingRelation?.endDate ?? '',
  175. note: existingRelation?.note ?? '',
  176. });
  177. if (errors.length > 0) {
  178. this.marriageErrorMessages = errors;
  179. return;
  180. }
  181. } else {
  182. const currentMarriage = this.marriageRelationService.getCurrentMarriageByFamilyId(familyId);
  183. if (currentMarriage) {
  184. this.marriageRelationService.deleteMarriageRelation(currentMarriage.id);
  185. }
  186. }
  187. this.familyService.saveFamily(updatedFamily);
  188. this.saveHouseholderIfSelected(updatedFamily);
  189. this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
  190. }
  191. cancelFamilyEdit(): void {
  192. const dankaId = this.dankaId;
  193. this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
  194. }
  195. deleteFamily() {
  196. const dankaId = this.dankaId;
  197. if (!this.familyId) {
  198. return;
  199. }
  200. this.familyService.deleteFamily(this.familyId);
  201. this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
  202. }
  203. setAsHouseholder(): void {
  204. this.setHouseholderOnSave = true;
  205. this.familyForm.patchValue({
  206. relationship: '施主',
  207. });
  208. }
  209. private saveHouseholderIfSelected(family: Family): void {
  210. if (!this.setHouseholderOnSave || !this.danka) {
  211. return;
  212. }
  213. this.dankaService.saveDanka({
  214. ...this.danka,
  215. householder: family.name,
  216. householderFurigana: family.furigana,
  217. updatedAt: this.formatDateForSave(new Date()),
  218. });
  219. }
  220. private formatDateForSave(date: Date): string {
  221. const year = date.getFullYear();
  222. const month = String(date.getMonth() + 1).padStart(2, '0');
  223. const day = String(date.getDate()).padStart(2, '0');
  224. return `${year}-${month}-${day}`;
  225. }
  226. }