설명 없음
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.

search.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
  2. import { FormsModule } from '@angular/forms';
  3. import { ActivatedRoute, RouterLink } from '@angular/router';
  4. import { Danka } from '../../models/danka';
  5. import { Family } from '../../models/family';
  6. import { Kakocho } from '../../models/kakocho';
  7. import { DankaService } from '../../services/dankaService';
  8. import { FamilyService } from '../../services/family-service';
  9. import { KakochoService } from '../../services/kakocho-service';
  10. import { AppHeader } from '../../share/header/app-header';
  11. import { AppSideMenu } from '../../share/side-menu/app-side-menu';
  12. @Component({
  13. selector: 'app-search',
  14. imports: [AppHeader, AppSideMenu, FormsModule, RouterLink],
  15. templateUrl: './search.html',
  16. styleUrl: './search.scss',
  17. })
  18. export class Search implements OnInit{
  19. searchKeyword = '';
  20. selectedSearchType = 'all';
  21. searchTypeFilters = [
  22. { label: 'すべて', value: 'all' },
  23. { label: '檀家(世帯)', value: 'danka' },
  24. { label: '家族', value: 'family' },
  25. { label: '過去帳', value: 'kakocho' },
  26. ];
  27. dankaResults: Danka[] = [];
  28. familyResults: Family[] = [];
  29. kakochoResults: Kakocho[] = [];
  30. totalResultCount = 0;
  31. private searchTimer?: ReturnType<typeof setTimeout>;
  32. private searchRequestId = 0;
  33. constructor(
  34. private dankaService: DankaService,
  35. private familyService: FamilyService,
  36. private kakochoService: KakochoService,
  37. private route: ActivatedRoute,
  38. private cdr: ChangeDetectorRef,
  39. ) { }
  40. ngOnInit(): void {
  41. const keyword = this.route.snapshot.queryParamMap.get('keyword');
  42. if (keyword) {
  43. this.searchKeyword = keyword;
  44. this.submitSearch();
  45. }
  46. }
  47. // フィルタータブの選択処理
  48. changeSearchType(searchType: string): void {
  49. this.selectedSearchType = searchType;
  50. this.submitSearch();
  51. }
  52. onSearchKeywordChange(keyword: string): void {
  53. this.searchKeyword = keyword;
  54. if (this.searchTimer) {
  55. clearTimeout(this.searchTimer);
  56. }
  57. this.searchTimer = setTimeout(() => {
  58. this.searchAll();
  59. }, 250);
  60. }
  61. submitSearch(): void {
  62. if (this.searchTimer) {
  63. clearTimeout(this.searchTimer);
  64. }
  65. this.searchAll();
  66. }
  67. // 全検索の処理
  68. async searchAll(): Promise<void> {
  69. const requestId = ++this.searchRequestId;
  70. const keyword = this.searchKeyword.trim();
  71. const searchType = this.selectedSearchType;
  72. if (keyword === '') {
  73. this.setSearchResults([], [], [], requestId);
  74. return;
  75. }
  76. const [dankaList, familyList, kakochoList] = await Promise.all([
  77. this.dankaService.getDankaList(),
  78. this.familyService.getFamilyList(),
  79. this.kakochoService.getKakochoList(),
  80. ]);
  81. if (requestId !== this.searchRequestId) {
  82. return;
  83. }
  84. let dankaResults: Danka[] = [];
  85. let familyResults: Family[] = [];
  86. let kakochoResults: Kakocho[] = [];
  87. // 檀家検索
  88. if (searchType === 'all' || searchType === 'danka') {
  89. dankaResults = dankaList.filter((danka) =>
  90. this.includesKeyword(danka.householdName, keyword) ||
  91. this.includesKeyword(danka.householder, keyword) ||
  92. this.includesKeyword(danka.postalCode, keyword) ||
  93. this.includesKeyword(danka.address, keyword) ||
  94. this.getPhones(danka).some(
  95. (phone) =>
  96. this.includesKeyword(phone.tel, keyword) ||
  97. this.includesKeyword(phone.note, keyword)
  98. )
  99. );
  100. }
  101. // 家族検索
  102. if (searchType === 'all' || searchType === 'family') {
  103. familyResults = familyList.filter((family) =>
  104. this.includesKeyword(family.name, keyword) ||
  105. this.includesKeyword(family.furigana, keyword) ||
  106. this.includesKeyword(family.relationship, keyword) ||
  107. this.includesKeyword(family.birthDate, keyword) ||
  108. this.includesKeyword(family.note, keyword)
  109. );
  110. }
  111. // 過去帳検索
  112. if (searchType === 'all' || searchType === 'kakocho') {
  113. kakochoResults = kakochoList.filter((kakocho) =>
  114. this.includesKeyword(kakocho.name, keyword) ||
  115. this.includesKeyword(kakocho.furigana, keyword) ||
  116. this.includesKeyword(kakocho.relationship, keyword) ||
  117. this.includesKeyword(kakocho.kaimyo, keyword) ||
  118. this.includesKeyword(kakocho.deathDate, keyword) ||
  119. this.includesKeyword(kakocho.ageAtDeath, keyword) ||
  120. this.includesKeyword(kakocho.note, keyword)
  121. );
  122. }
  123. this.setSearchResults(dankaResults, familyResults, kakochoResults, requestId);
  124. }
  125. clearSearch(): void {
  126. if (this.searchTimer) {
  127. clearTimeout(this.searchTimer);
  128. }
  129. this.searchRequestId++;
  130. this.searchKeyword = '';
  131. this.selectedSearchType = 'all';
  132. this.dankaResults = [];
  133. this.familyResults = [];
  134. this.kakochoResults = [];
  135. this.totalResultCount = 0;
  136. this.cdr.detectChanges();
  137. }
  138. private includesKeyword(value: unknown, keyword: string): boolean {
  139. return String(value ?? '').includes(keyword);
  140. }
  141. private getPhones(danka: Danka) {
  142. return Array.isArray(danka.phones) ? danka.phones : [];
  143. }
  144. private setSearchResults(
  145. dankaResults: Danka[],
  146. familyResults: Family[],
  147. kakochoResults: Kakocho[],
  148. requestId: number,
  149. ): void {
  150. if (requestId !== this.searchRequestId) {
  151. return;
  152. }
  153. this.dankaResults = dankaResults;
  154. this.familyResults = familyResults;
  155. this.kakochoResults = kakochoResults;
  156. this.totalResultCount =
  157. dankaResults.length +
  158. familyResults.length +
  159. kakochoResults.length;
  160. this.cdr.detectChanges();
  161. }
  162. }