-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #554 from fecgov/feature/471-app-contact-lookup-pr…
…ototype Feature/471 app contact lookup prototype
- Loading branch information
Showing
11 changed files
with
364 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
front-end/src/app/shared/components/contact-lookup/contact-lookup.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<form [formGroup]="contactLookupForm"> | ||
<p-dropdown | ||
styleClass="gray" | ||
formControlName="selectedContactType" | ||
[options]="contactTypeOptions" | ||
optionLabel="name" | ||
optionValue="code"> | ||
</p-dropdown> | ||
<p-dropdown | ||
#lookupDropdown | ||
class="mr-2" | ||
(input)="onDropdownSearch($event)" | ||
[editable]="true" | ||
dropdownIcon="pi pi-search" | ||
[options]="contactLookupList" | ||
formControlName="selectedContact" | ||
placeholder="Search for a contact" | ||
[group]="true" | ||
(onChange)="onContactSelect($event)"> | ||
</p-dropdown> | ||
<span class="mr-2">or</span> | ||
<button | ||
pButton | ||
pRipple | ||
label="Create a new contact" | ||
icon="pi pi-plus-circle" | ||
class="p-button-info mr-2"> | ||
</button> | ||
</form> |
89 changes: 89 additions & 0 deletions
89
front-end/src/app/shared/components/contact-lookup/contact-lookup.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { EventEmitter } from '@angular/core'; | ||
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; | ||
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
import { CommitteeLookupResponse } from 'app/shared/models/contact.model'; | ||
import { UserLoginData } from 'app/shared/models/user.model'; | ||
import { ContactService } from 'app/shared/services/contact.service'; | ||
import { selectUserLoginData } from 'app/store/login.selectors'; | ||
import { DropdownModule } from 'primeng/dropdown'; | ||
import { of } from 'rxjs'; | ||
|
||
import { ContactLookupComponent } from './contact-lookup.component'; | ||
|
||
describe('ContactLookupComponent', () => { | ||
let component: ContactLookupComponent; | ||
let fixture: ComponentFixture<ContactLookupComponent>; | ||
|
||
let testContactService: ContactService; | ||
|
||
beforeEach(async () => { | ||
const userLoginData: UserLoginData = { | ||
committee_id: 'C00000000', | ||
email: '[email protected]', | ||
is_allowed: true, | ||
token: 'jwttokenstring', | ||
}; | ||
|
||
await TestBed.configureTestingModule({ | ||
declarations: [ContactLookupComponent], | ||
imports: [FormsModule, ReactiveFormsModule, HttpClientTestingModule, DropdownModule], | ||
providers: [ | ||
FormBuilder, | ||
ContactService, | ||
ContactService, | ||
EventEmitter, | ||
provideMockStore({ | ||
initialState: { fecfile_online_userLoginData: userLoginData }, | ||
selectors: [{ selector: selectUserLoginData, value: userLoginData }], | ||
}), | ||
], | ||
}).compileComponents(); | ||
|
||
testContactService = TestBed.inject(ContactService); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ContactLookupComponent); | ||
component = fixture.componentInstance; | ||
|
||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('#onDropdownSearch empty search', fakeAsync(() => { | ||
const testEvent = { target: { value: null } }; | ||
component.onDropdownSearch(testEvent); | ||
tick(500); | ||
expect(component.contactLookupList.length === 0).toBeTrue(); | ||
})); | ||
|
||
it('#onDropdownSearch happy path', fakeAsync(() => { | ||
const testCommitteeLookupResponse = new CommitteeLookupResponse(); | ||
testCommitteeLookupResponse.fec_api_committees = [{ id: 'testId', name: 'testName' }]; | ||
testCommitteeLookupResponse.fecfile_committees = [{ id: 'testId', name: 'testName' }]; | ||
spyOn(testContactService, 'committeeLookup').and.returnValue(of(testCommitteeLookupResponse)); | ||
const testEvent = { target: { value: 'hi' } }; | ||
component.onDropdownSearch(testEvent); | ||
tick(500); | ||
expect(component.lookupDropdown?.overlayVisible === true).toBeTrue(); | ||
})); | ||
|
||
it('#onContactSelect happy path', fakeAsync(() => { | ||
const eventEmitterEmitSpy = spyOn(component.contactSelect, 'emit'); | ||
const testValue = 'testValue'; | ||
const testEvent = { | ||
originalEvent: { | ||
type: 'click', | ||
}, | ||
value: testValue, | ||
}; | ||
component.onContactSelect(testEvent); | ||
tick(500); | ||
expect(eventEmitterEmitSpy).toHaveBeenCalledOnceWith(testValue); | ||
})); | ||
}); |
69 changes: 69 additions & 0 deletions
69
front-end/src/app/shared/components/contact-lookup/contact-lookup.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Component, EventEmitter, Input, Output, ViewChild } from '@angular/core'; | ||
import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; | ||
import { ContactType } from 'app/shared/models/contact.model'; | ||
import { ContactService } from 'app/shared/services/contact.service'; | ||
import { PrimeOptions } from 'app/shared/utils/label.utils'; | ||
import { debounce } from 'lodash'; | ||
import { SelectItem, SelectItemGroup } from 'primeng/api'; | ||
import { Dropdown } from 'primeng/dropdown'; | ||
|
||
@Component({ | ||
selector: 'app-contact-lookup', | ||
templateUrl: './contact-lookup.component.html', | ||
}) | ||
export class ContactLookupComponent { | ||
@Input() contactTypeOptions: PrimeOptions = []; | ||
@Input() maxFecResults = 10; | ||
@Input() maxFecfileResults = 10; | ||
@Input() searchDelayMillis = 250; | ||
|
||
@Output() contactSelect = new EventEmitter<string>(); | ||
|
||
@ViewChild('lookupDropdown') lookupDropdown: Dropdown | null = null; | ||
|
||
selectedContactType = | ||
new FormControl<ContactType>({} as ContactType); | ||
selectedContact: FormControl<SelectItem> | null = null; | ||
|
||
contactLookupForm: FormGroup = this.formBuilder.group({ | ||
selectedContactType: this.selectedContactType, | ||
selectedContact: this.selectedContact | ||
}) | ||
|
||
contactLookupList: SelectItemGroup[] = []; | ||
|
||
constructor( | ||
private formBuilder: FormBuilder, | ||
private contactService: ContactService | ||
) { } | ||
|
||
onDropdownSearch = debounce((event) => { | ||
const searchTerm = event?.target?.value; | ||
if (searchTerm) { | ||
this.contactService.committeeLookup( | ||
searchTerm, this.maxFecResults, | ||
this.maxFecfileResults).subscribe((response) => { | ||
this.contactLookupList = response && | ||
response.toSelectItemGroups(); | ||
if (this.lookupDropdown) { | ||
this.lookupDropdown.overlayVisible = true; | ||
} | ||
}); | ||
} else { | ||
this.contactLookupList = []; | ||
} | ||
}, this.searchDelayMillis); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
onContactSelect(event: any) { | ||
if (event) { | ||
if (event.originalEvent?.type === 'click') { | ||
const value: string = event.value; | ||
if (value) { | ||
this.contactSelect.emit(value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.