Procházet zdrojové kódy

[add]

家族編集に配偶者との関係の選択肢を追加
poohr před 3 týdny
rodič
revize
0e79b158a7

+ 20
- 0
src/app/pages/family-edit/family-edit.html Zobrazit soubor

141
                 </div>
141
                 </div>
142
               </div>
142
               </div>
143
 
143
 
144
+              <div class="form-row">
145
+                <label for="marriageStatus">配偶者の状態</label>
146
+                <div class="form-field">
147
+                  <select id="marriageStatus" formControlName="marriageStatus">
148
+                    <option value="current">現在の配偶者</option>
149
+                    <option value="divorced">離婚</option>
150
+                    <option value="widowed">死別</option>
151
+                    <option value="unknown">不明</option>
152
+                  </select>
153
+
154
+                  @if (marriageErrorMessages.length > 0) {
155
+                    <div class="error-message-list">
156
+                      @for (error of marriageErrorMessages; track error) {
157
+                        <p class="error-message">{{ error }}</p>
158
+                      }
159
+                    </div>
160
+                  }
161
+                </div>
162
+              </div>
163
+
144
               <div class="form-row">
164
               <div class="form-row">
145
                 <label for="note">備考</label>
165
                 <label for="note">備考</label>
146
                 <div class="form-field">
166
                 <div class="form-field">

+ 62
- 1
src/app/pages/family-edit/family-edit.ts Zobrazit soubor

10
 import { AppHeader } from '../../share/header/app-header';
10
 import { AppHeader } from '../../share/header/app-header';
11
 import { AppSideMenu } from '../../share/side-menu/app-side-menu';
11
 import { AppSideMenu } from '../../share/side-menu/app-side-menu';
12
 import { FamilyService } from '../../services/family-service';
12
 import { FamilyService } from '../../services/family-service';
13
+import { MarriageRelationService } from '../../services/marriage-relation-service';
13
 import { Danka } from '../../models/danka';
14
 import { Danka } from '../../models/danka';
14
 import { Family } from '../../models/family';
15
 import { Family } from '../../models/family';
16
+import { MarriageRelation } from '../../models/marriage-relation';
15
 
17
 
16
 @Component({
18
 @Component({
17
   selector: 'app-family-edit',
19
   selector: 'app-family-edit',
27
   familyId: string | null = null;
29
   familyId: string | null = null;
28
   relationMode: string = '';
30
   relationMode: string = '';
29
   baseFamilyId: string = '';
31
   baseFamilyId: string = '';
32
+  marriageErrorMessages: string[] = [];
30
 
33
 
31
   constructor(
34
   constructor(
32
     private familyService: FamilyService,
35
     private familyService: FamilyService,
36
+    private marriageRelationService: MarriageRelationService,
33
     private route: ActivatedRoute,
37
     private route: ActivatedRoute,
34
     private router: Router,
38
     private router: Router,
35
   ) {
39
   ) {
52
           spouseId: this.family.spouseId,
56
           spouseId: this.family.spouseId,
53
           gender: this.family.gender,
57
           gender: this.family.gender,
54
         });
58
         });
59
+        this.patchMarriageRelationFields(this.family.id);
55
       }
60
       }
56
     }
61
     }
57
 
62
 
58
     if (!this.familyId && this.relationMode === 'spouse') {
63
     if (!this.familyId && this.relationMode === 'spouse') {
59
       this.familyForm.patchValue({
64
       this.familyForm.patchValue({
60
         spouseId: this.baseFamilyId,
65
         spouseId: this.baseFamilyId,
66
+        marriageStatus: 'current',
61
       });
67
       });
62
     }
68
     }
63
 
69
 
87
     fatherId: new FormControl(''),
93
     fatherId: new FormControl(''),
88
     motherId: new FormControl(''),
94
     motherId: new FormControl(''),
89
     spouseId: new FormControl(''),
95
     spouseId: new FormControl(''),
96
+    marriageStatus: new FormControl<MarriageRelation['status']>('current'),
90
     gender: new FormControl('unknown'),
97
     gender: new FormControl('unknown'),
91
   });
98
   });
92
 
99
 
110
     return this.families.filter((family) => family.id !== this.familyId);
117
     return this.families.filter((family) => family.id !== this.familyId);
111
   }
118
   }
112
 
119
 
120
+  patchMarriageRelationFields(familyId: string): void {
121
+    const relations = this.marriageRelationService.getMarriageRelationsByFamilyId(familyId);
122
+    const relation =
123
+      relations.find((marriageRelation) => marriageRelation.status === 'current') ?? relations[0];
124
+
125
+    if (!relation) {
126
+      return;
127
+    }
128
+
129
+    this.familyForm.patchValue({
130
+      spouseId: relation.person1Id === familyId ? relation.person2Id : relation.person1Id,
131
+      marriageStatus: relation.status,
132
+    });
133
+  }
134
+
135
+  findMarriageRelation(person1Id: string, person2Id: string): MarriageRelation | undefined {
136
+    return this.marriageRelationService
137
+      .getMarriageRelationsByFamilyId(person1Id)
138
+      .find(
139
+        (relation) =>
140
+          (relation.person1Id === person1Id && relation.person2Id === person2Id) ||
141
+          (relation.person1Id === person2Id && relation.person2Id === person1Id),
142
+      );
143
+  }
144
+
113
   saveFamily() {
145
   saveFamily() {
114
     if (this.familyForm.invalid) {
146
     if (this.familyForm.invalid) {
115
       return;
147
       return;
116
     }
148
     }
117
 
149
 
150
+    this.marriageErrorMessages = [];
118
     const familyId = this.familyId ?? Date.now().toString();
151
     const familyId = this.familyId ?? Date.now().toString();
119
     const dankaId = this.dankaId;
152
     const dankaId = this.dankaId;
120
     const formValue = this.familyForm.value;
153
     const formValue = this.familyForm.value;
154
+    const spouseId = formValue.spouseId ?? '';
155
+    const marriageStatus = formValue.marriageStatus ?? 'current';
156
+    const currentSpouseId = marriageStatus === 'current' ? spouseId : '';
121
     const updatedFamily = {
157
     const updatedFamily = {
122
       id: familyId,
158
       id: familyId,
123
       dankaId: dankaId,
159
       dankaId: dankaId,
128
       note: formValue.note?.trim() ?? '',
164
       note: formValue.note?.trim() ?? '',
129
       fatherId: this.familyForm.value.fatherId ?? '',
165
       fatherId: this.familyForm.value.fatherId ?? '',
130
       motherId: this.familyForm.value.motherId ?? '',
166
       motherId: this.familyForm.value.motherId ?? '',
131
-      spouseId: this.familyForm.value.spouseId ?? '',
167
+      spouseId: currentSpouseId,
132
       gender: this.familyForm.value.gender ?? 'unknown',
168
       gender: this.familyForm.value.gender ?? 'unknown',
133
     };
169
     };
170
+
171
+    if (spouseId) {
172
+      const existingRelation = this.findMarriageRelation(familyId, spouseId);
173
+      const errors = this.marriageRelationService.saveMarriageRelation({
174
+        id: existingRelation?.id ?? Date.now().toString(),
175
+        dankaId,
176
+        person1Id: familyId,
177
+        person2Id: spouseId,
178
+        status: marriageStatus,
179
+        startDate: existingRelation?.startDate ?? '',
180
+        endDate: existingRelation?.endDate ?? '',
181
+        note: existingRelation?.note ?? '',
182
+      });
183
+
184
+      if (errors.length > 0) {
185
+        this.marriageErrorMessages = errors;
186
+        return;
187
+      }
188
+    } else {
189
+      const currentMarriage = this.marriageRelationService.getCurrentMarriageByFamilyId(familyId);
190
+      if (currentMarriage) {
191
+        this.marriageRelationService.deleteMarriageRelation(currentMarriage.id);
192
+      }
193
+    }
194
+
134
     this.familyService.saveFamily(updatedFamily);
195
     this.familyService.saveFamily(updatedFamily);
135
     this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
196
     this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
136
   }
197
   }

Loading…
Zrušit
Uložit