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

family-edit.ts 8.0KB

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