Bladeren bron

[update]

描画の修正
poohr 3 weken geleden
bovenliggende
commit
19fb420f5c
3 gewijzigde bestanden met toevoegingen van 106 en 46 verwijderingen
  1. 5
    5
      src/app/pages/dashboard/dashboard.ts
  2. 4
    3
      src/app/pages/search/search.html
  3. 97
    38
      src/app/pages/search/search.ts

+ 5
- 5
src/app/pages/dashboard/dashboard.ts Bestand weergeven

@@ -1,10 +1,11 @@
1 1
 import { Component } from '@angular/core';
2
-import { RouterLink } from '@angular/router';
2
+import { Router, RouterLink } from '@angular/router';
3 3
 import { KakochoService } from '../../services/kakocho-service';
4 4
 import { DankaService } from '../../services/dankaService';
5 5
 import { Danka } from '../../models/danka';
6 6
 import { AppHeader } from '../../share/header/app-header';
7 7
 import { AppSideMenu } from '../../share/side-menu/app-side-menu';
8
+import { FormsModule } from '@angular/forms';
8 9
 
9 10
 interface UpcomingMemorial {
10 11
   id: string;
@@ -68,7 +69,7 @@ export class Dashboard {
68 69
         danka,
69 70
         nextMemorialLabel: await this.getNextMemorialLabel(danka.id),
70 71
         updatedAtLabel: this.formatUpdatedAt(danka.updatedAt),
71
-      }))
72
+      })),
72 73
     );
73 74
   }
74 75
 
@@ -142,7 +143,7 @@ export class Dashboard {
142 143
           type,
143 144
           status,
144 145
         };
145
-      })
146
+      }),
146 147
     );
147 148
 
148 149
     this.upcomingMemorials = results
@@ -186,8 +187,7 @@ export class Dashboard {
186 187
 
187 188
   private async getNextMemorialLabel(dankaId: string): Promise<string> {
188 189
     const today = this.toDateOnly(new Date());
189
-    const nextMemorial = (await this.kakochoService
190
-      .getKakochoByDankaId(dankaId))
190
+    const nextMemorial = (await this.kakochoService.getKakochoByDankaId(dankaId))
191 191
       .map((kakocho) => {
192 192
         const deathDate = this.parseDate(kakocho.deathDate);
193 193
         if (!deathDate) {

+ 4
- 3
src/app/pages/search/search.html Bestand weergeven

@@ -13,11 +13,12 @@
13 13
         <div class="search-input-row">
14 14
           <div class="search-input-box">
15 15
             <span class="search-icon">⌕</span>
16
-            <input type="text" [(ngModel)]="searchKeyword"
16
+            <input type="text" [ngModel]="searchKeyword"
17
+                   (ngModelChange)="onSearchKeywordChange($event)"
17 18
                    placeholder="三回忌 / 〇〇歳 / 2024 /"
18
-                   (keydown.enter)="searchAll()"/>
19
+                   (keydown.enter)="submitSearch()"/>
19 20
           </div>
20
-          <button type="button" class="search-button" (click)="searchAll()">
21
+          <button type="button" class="search-button" (click)="submitSearch()">
21 22
             検索
22 23
           </button>
23 24
           <button type="button" class="filter-button-main" (click)="clearSearch()">

+ 97
- 38
src/app/pages/search/search.ts Bestand weergeven

@@ -1,6 +1,6 @@
1 1
 import { Component, OnInit } from '@angular/core';
2
-import { FormBuilder, FormsModule } from '@angular/forms';
3
-import { ActivatedRoute, Router, RouterLink } from '@angular/router';
2
+import { FormsModule } from '@angular/forms';
3
+import { ActivatedRoute, RouterLink } from '@angular/router';
4 4
 import { Danka } from '../../models/danka';
5 5
 import { Family } from '../../models/family';
6 6
 import { Kakocho } from '../../models/kakocho';
@@ -29,6 +29,8 @@ export class Search implements OnInit{
29 29
   familyResults: Family[] = [];
30 30
   kakochoResults: Kakocho[] = [];
31 31
   totalResultCount = 0;
32
+  private searchTimer?: ReturnType<typeof setTimeout>;
33
+  private searchRequestId = 0;
32 34
 
33 35
   constructor(
34 36
     private dankaService: DankaService,
@@ -42,26 +44,46 @@ export class Search implements OnInit{
42 44
 
43 45
     if (keyword) {
44 46
       this.searchKeyword = keyword;
45
-      this.searchAll();
47
+      this.submitSearch();
46 48
     }
47 49
   }
48 50
 
49 51
   // フィルタータブの選択処理
50 52
   changeSearchType(searchType: string): void {
51 53
     this.selectedSearchType = searchType;
54
+    this.submitSearch();
55
+  }
56
+
57
+  onSearchKeywordChange(keyword: string): void {
58
+    this.searchKeyword = keyword;
59
+
60
+    if (this.searchTimer) {
61
+      clearTimeout(this.searchTimer);
62
+    }
63
+
64
+    this.searchTimer = setTimeout(() => {
65
+      this.searchAll();
66
+    }, 250);
67
+  }
68
+
69
+  submitSearch(): void {
70
+    if (this.searchTimer) {
71
+      clearTimeout(this.searchTimer);
72
+    }
73
+
52 74
     this.searchAll();
53 75
   }
54 76
 
55 77
   // 全検索の処理
56 78
   async searchAll(): Promise<void> {
79
+    const requestId = ++this.searchRequestId;
57 80
     const keyword = this.searchKeyword.trim();
81
+    const searchType = this.selectedSearchType;
58 82
 
59
-    this.dankaResults = [];
60
-    this.familyResults = [];
61
-    this.kakochoResults = [];
62
-    this.totalResultCount = 0;
63
-
64
-    if (keyword === '') return;
83
+    if (keyword === '') {
84
+      this.setSearchResults([], [], [], requestId);
85
+      return;
86
+    }
65 87
 
66 88
     const [dankaList, familyList, kakochoList] = await Promise.all([
67 89
       this.dankaService.getDankaList(),
@@ -69,52 +91,62 @@ export class Search implements OnInit{
69 91
       this.kakochoService.getKakochoList(),
70 92
     ]);
71 93
 
94
+    if (requestId !== this.searchRequestId) {
95
+      return;
96
+    }
97
+
98
+    let dankaResults: Danka[] = [];
99
+    let familyResults: Family[] = [];
100
+    let kakochoResults: Kakocho[] = [];
101
+
72 102
     // 檀家検索
73
-    if (this.selectedSearchType === 'all' || this.selectedSearchType === 'danka') {
74
-      this.dankaResults = dankaList.filter((danka) =>
75
-        danka.householdName.includes(keyword) ||
76
-        danka.householder.includes(keyword) ||
77
-        danka.postalCode.includes(keyword) ||
78
-        danka.address.includes(keyword) ||
79
-        danka.phones.some(
103
+    if (searchType === 'all' || searchType === 'danka') {
104
+      dankaResults = dankaList.filter((danka) =>
105
+        this.includesKeyword(danka.householdName, keyword) ||
106
+        this.includesKeyword(danka.householder, keyword) ||
107
+        this.includesKeyword(danka.postalCode, keyword) ||
108
+        this.includesKeyword(danka.address, keyword) ||
109
+        this.getPhones(danka).some(
80 110
           (phone) =>
81
-            phone.tel.includes(keyword) ||
82
-            phone.note.includes(keyword)
111
+            this.includesKeyword(phone.tel, keyword) ||
112
+            this.includesKeyword(phone.note, keyword)
83 113
         )
84 114
       );
85 115
     }
86 116
 
87 117
     // 家族検索
88
-    if (this.selectedSearchType === 'all' || this.selectedSearchType === 'family') {
89
-      this.familyResults = familyList.filter((family) =>
90
-        family.name.includes(keyword) ||
91
-        family.furigana.includes(keyword) ||
92
-        family.relationship.includes(keyword) ||
93
-        family.birthDate.includes(keyword) ||
94
-        family.note.includes(keyword)
118
+    if (searchType === 'all' || searchType === 'family') {
119
+      familyResults = familyList.filter((family) =>
120
+        this.includesKeyword(family.name, keyword) ||
121
+        this.includesKeyword(family.furigana, keyword) ||
122
+        this.includesKeyword(family.relationship, keyword) ||
123
+        this.includesKeyword(family.birthDate, keyword) ||
124
+        this.includesKeyword(family.note, keyword)
95 125
       );
96 126
     }
97 127
 
98 128
     // 過去帳検索
99
-    if (this.selectedSearchType === 'all' || this.selectedSearchType === 'kakocho') {
100
-      this.kakochoResults = kakochoList.filter((kakocho) =>
101
-        kakocho.name.includes(keyword) ||
102
-        kakocho.furigana.includes(keyword) ||
103
-        kakocho.relationship.includes(keyword) ||
104
-        kakocho.kaimyo.includes(keyword) ||
105
-        kakocho.deathDate.includes(keyword) ||
106
-        kakocho.ageAtDeath.includes(keyword) ||
107
-        kakocho.note.includes(keyword)
129
+    if (searchType === 'all' || searchType === 'kakocho') {
130
+      kakochoResults = kakochoList.filter((kakocho) =>
131
+        this.includesKeyword(kakocho.name, keyword) ||
132
+        this.includesKeyword(kakocho.furigana, keyword) ||
133
+        this.includesKeyword(kakocho.relationship, keyword) ||
134
+        this.includesKeyword(kakocho.kaimyo, keyword) ||
135
+        this.includesKeyword(kakocho.deathDate, keyword) ||
136
+        this.includesKeyword(kakocho.ageAtDeath, keyword) ||
137
+        this.includesKeyword(kakocho.note, keyword)
108 138
       );
109 139
     }
110 140
 
111
-    this.totalResultCount =
112
-      this.dankaResults.length +
113
-      this.familyResults.length +
114
-      this.kakochoResults.length;
141
+    this.setSearchResults(dankaResults, familyResults, kakochoResults, requestId);
115 142
   }
116 143
 
117 144
   clearSearch(): void {
145
+    if (this.searchTimer) {
146
+      clearTimeout(this.searchTimer);
147
+    }
148
+
149
+    this.searchRequestId++;
118 150
     this.searchKeyword = '';
119 151
     this.selectedSearchType = 'all';
120 152
     this.dankaResults = [];
@@ -122,4 +154,31 @@ export class Search implements OnInit{
122 154
     this.kakochoResults = [];
123 155
     this.totalResultCount = 0;
124 156
   }
157
+
158
+  private includesKeyword(value: unknown, keyword: string): boolean {
159
+    return String(value ?? '').includes(keyword);
160
+  }
161
+
162
+  private getPhones(danka: Danka) {
163
+    return Array.isArray(danka.phones) ? danka.phones : [];
164
+  }
165
+
166
+  private setSearchResults(
167
+    dankaResults: Danka[],
168
+    familyResults: Family[],
169
+    kakochoResults: Kakocho[],
170
+    requestId: number,
171
+  ): void {
172
+    if (requestId !== this.searchRequestId) {
173
+      return;
174
+    }
175
+
176
+    this.dankaResults = dankaResults;
177
+    this.familyResults = familyResults;
178
+    this.kakochoResults = kakochoResults;
179
+    this.totalResultCount =
180
+      dankaResults.length +
181
+      familyResults.length +
182
+      kakochoResults.length;
183
+  }
125 184
 }

Laden…
Annuleren
Opslaan