From 62655a691acf8430876e55eb1afa99bc75dcea6b Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Wed, 7 Apr 2021 12:24:32 +0300 Subject: [PATCH] Add search form for name, phonenumber and account address. --- src/app/_helpers/export-csv.ts | 6 ++- src/app/_services/user.service.ts | 6 +++ .../account-search.component.html | 49 +++++++++++++---- .../account-search.component.ts | 54 ++++++++++++++++++- .../pages/accounts/accounts.component.html | 3 +- 5 files changed, 104 insertions(+), 14 deletions(-) diff --git a/src/app/_helpers/export-csv.ts b/src/app/_helpers/export-csv.ts index 7100842..bb9406c 100644 --- a/src/app/_helpers/export-csv.ts +++ b/src/app/_helpers/export-csv.ts @@ -1,5 +1,8 @@ function exportCsv(arrayData: any[], filename: string, delimiter = ','): void { - if (arrayData === undefined || arrayData.length === 0) { return; } + if (arrayData === undefined || arrayData.length === 0) { + alert('No data to be exported!'); + return; + } const header = Object.keys(arrayData[0]).join(delimiter) + '\n'; let csv = header; arrayData.forEach(obj => { @@ -14,7 +17,6 @@ function exportCsv(arrayData: any[], filename: string, delimiter = ','): void { const csvData = new Blob([csv], {type: 'text/csv'}); const csvUrl = URL.createObjectURL(csvData); - // csvUrl = 'data:text/csv;charset=utf-8,' + encodeURI(csv); let downloadLink = document.createElement('a'); downloadLink.href = csvUrl; diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index b803775..fdbf300 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -176,4 +176,10 @@ export class UserService { this.accounts = []; this.accountsList.next(this.accounts); } + + searchAccountByName(name: string): any { return; } + + searchAccountByPhone(phoneNumber: string): any { return; } + + searchAccountByAddress(address: string): any { return; } } diff --git a/src/app/pages/accounts/account-search/account-search.component.html b/src/app/pages/accounts/account-search/account-search.component.html index 853c285..88fd21a 100644 --- a/src/app/pages/accounts/account-search/account-search.component.html +++ b/src/app/pages/accounts/account-search/account-search.component.html @@ -14,7 +14,7 @@
@@ -22,15 +22,44 @@ Accounts
-
- - Search - - name - Name - - -
+ + +
+ + Search + + Name is required. + face + Name + + +
+
+ +
+ + Search + + Phone Number is required. + phone + Phone Number + + +
+
+ +
+ + Search + + Account Address is required. + view_in_ar + Account Address + + +
+
+
diff --git a/src/app/pages/accounts/account-search/account-search.component.ts b/src/app/pages/accounts/account-search/account-search.component.ts index 3cfeacf..7b6accd 100644 --- a/src/app/pages/accounts/account-search/account-search.component.ts +++ b/src/app/pages/accounts/account-search/account-search.component.ts @@ -1,4 +1,7 @@ import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core'; +import {FormBuilder, FormGroup, Validators} from '@angular/forms'; +import {CustomErrorStateMatcher} from '@app/_helpers'; +import {UserService} from '@app/_services'; @Component({ selector: 'app-account-search', @@ -7,10 +10,59 @@ import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core'; changeDetection: ChangeDetectionStrategy.OnPush }) export class AccountSearchComponent implements OnInit { + nameSearchForm: FormGroup; + nameSearchSubmitted: boolean = false; + nameSearchLoading: boolean = false; + phoneSearchForm: FormGroup; + phoneSearchSubmitted: boolean = false; + phoneSearchLoading: boolean = false; + addressSearchForm: FormGroup; + addressSearchSubmitted: boolean = false; + addressSearchLoading: boolean = false; + matcher = new CustomErrorStateMatcher(); - constructor() { } + constructor( + private formBuilder: FormBuilder, + private userService: UserService, + ) { } ngOnInit(): void { + this.nameSearchForm = this.formBuilder.group({ + name: ['', Validators.required], + }); + this.phoneSearchForm = this.formBuilder.group({ + phoneNumber: ['', Validators.required], + }); + this.addressSearchForm = this.formBuilder.group({ + address: ['', Validators.required], + }); } + get nameSearchFormStub(): any { return this.nameSearchForm.controls; } + get phoneSearchFormStub(): any { return this.phoneSearchForm.controls; } + get addressSearchFormStub(): any { return this.addressSearchForm.controls; } + + onNameSearch(): void { + this.nameSearchSubmitted = true; + if (this.nameSearchForm.invalid) { return; } + this.nameSearchLoading = true; + this.userService.searchAccountByName(this.nameSearchFormStub.name.value); + this.nameSearchLoading = false; + } + + onPhoneSearch(): void { + this.phoneSearchSubmitted = true; + if (this.phoneSearchForm.invalid) { return; } + this.phoneSearchLoading = true; + this.userService.searchAccountByPhone(this.phoneSearchFormStub.phoneNumber.value); + this.phoneSearchLoading = false; + } + + onAddressSearch(): void { + this.addressSearchSubmitted = true; + if (this.addressSearchForm.invalid) { return; } + this.addressSearchLoading = true; + this.userService.searchAccountByAddress(this.addressSearchFormStub.address.value); + this.addressSearchLoading = false; + } } diff --git a/src/app/pages/accounts/accounts.component.html b/src/app/pages/accounts/accounts.component.html index 18f045e..460430d 100644 --- a/src/app/pages/accounts/accounts.component.html +++ b/src/app/pages/accounts/accounts.component.html @@ -33,7 +33,8 @@ GROUPACCOUNT - + +