Bez popisu
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.

family-edit.ts 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 { FamilyService } from '../../services/family-service';
  13. import { Danka } from '../../models/danka';
  14. import { Family } from '../../models/family';
  15. @Component({
  16. selector: 'app-family-edit',
  17. imports: [AppHeader, AppSideMenu, ReactiveFormsModule],
  18. templateUrl: './family-edit.html',
  19. styleUrl: './family-edit.scss',
  20. })
  21. export class FamilyEdit {
  22. danka: Danka | undefined;
  23. family: Family | undefined;
  24. families: Family[] = [];
  25. dankaId: string = '';
  26. familyId: string | null = null;
  27. relationMode: string = '';
  28. baseFamilyId: string = '';
  29. constructor(
  30. private familyService: FamilyService,
  31. private route: ActivatedRoute,
  32. private router: Router,
  33. ) {
  34. this.dankaId = this.route.snapshot.params['dankaId'];
  35. this.familyId = this.route.snapshot.params['familyId'];
  36. this.families = this.familyService.getFamiliesByDankaId(this.dankaId);
  37. this.relationMode = this.route.snapshot.queryParamMap.get('relationMode') ?? '';
  38. this.baseFamilyId = this.route.snapshot.queryParamMap.get('baseFamilyId') ?? '';
  39. if (this.familyId) {
  40. this.family = this.familyService.getFamilyById(this.familyId);
  41. if (this.family) {
  42. this.familyForm.patchValue({
  43. furigana: this.family.furigana,
  44. name: this.family.name,
  45. relationship: this.family.relationship,
  46. birthDate: this.family.birthDate,
  47. note: this.family.note,
  48. fatherId: this.family.fatherId,
  49. motherId: this.family.motherId,
  50. spouseId: this.family.spouseId,
  51. gender: this.family.gender,
  52. });
  53. }
  54. }
  55. if (!this.familyId && this.relationMode === 'spouse') {
  56. this.familyForm.patchValue({
  57. spouseId: this.baseFamilyId,
  58. });
  59. }
  60. if (!this.familyId && this.relationMode === 'child') {
  61. const baseFamily = this.familyService.getFamilyById(this.baseFamilyId);
  62. if (baseFamily?.gender === 'male') {
  63. this.familyForm.patchValue({
  64. fatherId: this.baseFamilyId,
  65. });
  66. }
  67. if (baseFamily?.gender === 'female') {
  68. this.familyForm.patchValue({
  69. motherId: this.baseFamilyId,
  70. });
  71. }
  72. }
  73. }
  74. familyForm = new FormGroup({
  75. furigana: new FormControl('', [Validators.required]),
  76. name: new FormControl('', [Validators.required]),
  77. relationship: new FormControl(''),
  78. birthDate: new FormControl('', Validators.required),
  79. note: new FormControl(''),
  80. fatherId: new FormControl(''),
  81. motherId: new FormControl(''),
  82. spouseId: new FormControl(''),
  83. gender: new FormControl('unknown'),
  84. });
  85. getAgeText() {
  86. const birthDate = this.familyForm.get('birthDate')?.value;
  87. if (!birthDate) {
  88. return '生年月日を入力すると自動計算されます';
  89. }
  90. const birth = new Date(birthDate);
  91. const today = new Date();
  92. let age = today.getFullYear() - birth.getFullYear();
  93. const thisYearsBirthday = new Date(today.getFullYear(), birth.getMonth(), birth.getDate());
  94. if (thisYearsBirthday > today) {
  95. age--;
  96. }
  97. return `${age}歳`;
  98. }
  99. getFamilyOptions(): Family[] {
  100. return this.families.filter((family) => family.id !== this.familyId);
  101. }
  102. saveFamily() {
  103. if (this.familyForm.invalid) {
  104. return;
  105. }
  106. const familyId = this.familyId ?? Date.now().toString();
  107. const dankaId = this.dankaId;
  108. const formValue = this.familyForm.value;
  109. const updatedFamily = {
  110. id: familyId,
  111. dankaId: dankaId,
  112. furigana: formValue.furigana?.trim() ?? '',
  113. name: formValue.name?.trim() ?? '',
  114. relationship: formValue.relationship?.trim() ?? '',
  115. birthDate: formValue.birthDate?.trim() ?? '',
  116. note: formValue.note?.trim() ?? '',
  117. fatherId: this.familyForm.value.fatherId ?? '',
  118. motherId: this.familyForm.value.motherId ?? '',
  119. spouseId: this.familyForm.value.spouseId ?? '',
  120. gender: this.familyForm.value.gender ?? 'unknown',
  121. };
  122. this.familyService.saveFamily(updatedFamily);
  123. this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
  124. }
  125. cancelFamilyEdit(): void {
  126. const dankaId = this.dankaId;
  127. this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
  128. }
  129. deleteFamily() {
  130. const dankaId = this.dankaId;
  131. if (!this.familyId) {
  132. return;
  133. }
  134. this.familyService.deleteFamily(this.familyId);
  135. this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
  136. }
  137. setAsHouseholder() {}
  138. }