Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search UI will handle dates for queries. #7

Merged
merged 4 commits into from
Nov 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/client/mtna/searchbar/searchbar-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
export class Searchbar {
public searching: boolean = false;
public searchString: string = '';
public after: Date;
public before: Date;

constructor(
private _searchService: SearchService,
Expand All @@ -14,7 +16,9 @@ export class Searchbar {

search() {
const searchQuery: ISearchQuery = {
query: this.searchString
query: this.searchString,
before: this.before,
after: this.after
};
this._searchService.search(searchQuery).then(this._$log.info);
}
Expand Down
37 changes: 34 additions & 3 deletions src/client/mtna/searchbar/searchbar-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
expect
} from 'chai';
import {
SearchService
SearchService, ISearchQuery
} from './searchbar-service';

const mock = angular.mock;
Expand All @@ -11,6 +11,8 @@ describe('SearchbarService', () => {
let sut: SearchService;
let $httpBackend: ng.IHttpBackendService;

const apiUrl = '/api/search';

beforeEach(mock.module(SearchService.module.name));
beforeEach(mock.inject((
mtnaSearchService: SearchService,
Expand All @@ -27,11 +29,40 @@ describe('SearchbarService', () => {
describe('search', () => {

it('should call the correct url with the correct parameters', () => {
$httpBackend.expectGET('/api/search?query=test-search').respond(200);
sut.search({query: 'test-search'});
$httpBackend.expectGET(`${apiUrl}?query=test-search`).respond(200);
sut.search({query: 'test-search', before: null, after: null});
$httpBackend.flush();
});

it('should convert the before date to a string', () => {
const searchParams: ISearchQuery = {
query: 'test-search',
before: new Date(),
after: null
};
const beforeDateUTC = encodeURI(searchParams.before.toUTCString())
.replace(/%20/g, '+');
$httpBackend.expectGET(
`${apiUrl}?before=${beforeDateUTC}&query=test-search`
).respond(200);
sut.search(searchParams);
$httpBackend.flush();
});

it('should convert the after date to a string', () => {
const searchParams: ISearchQuery = {
query: 'test-search',
before: null,
after: new Date()
};
const afterDateUTC = encodeURI(searchParams.after.toUTCString())
.replace(/%20/g, '+');
$httpBackend.expectGET(
`${apiUrl}?after=${afterDateUTC}&query=test-search`
).respond(200);
sut.search(searchParams);
$httpBackend.flush();
});
});

});
35 changes: 33 additions & 2 deletions src/client/mtna/searchbar/searchbar-service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
export interface ISearchQuery {
query: string;
before: Date;
after: Date;
}

interface ISearchParams {
query: string;
before: string;
after: string;
}

class SearchQuery implements ISearchQuery {
public query: string = '';
public before: Date = null;
public after: Date = null;

constructor(
sq: ISearchQuery
) {
this.query = sq.query;
this.before = sq.before;
this.after = sq.after;
}
toJSON(): ISearchParams {
return {
query: this.query,
before: this.before ? this.before.toUTCString() : null,
after: this.after ? this.after.toUTCString() : null,
};
}
}

export class SearchService {

constructor(private _http: angular.IHttpService) {
}

search(searchQuery: ISearchQuery): angular.IPromise<any> {
search(sq: ISearchQuery): angular.IPromise<any> {
const searchQuery = new SearchQuery(sq);

return this._http({
url: '/api/search',
method: 'GET',
params: searchQuery
params: searchQuery.toJSON()
});
}

Expand Down
13 changes: 13 additions & 0 deletions src/client/mtna/searchbar/searchbar-style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$material-dark-blue: rgb(40, 53, 147);

mtna-searchbar {

// Bug with the datepicker styles, hence why we are
// overriding here.
// https://github.com/angular/material/issues/4871
md-datepicker,
.md-hue-2 input.md-datepicker-input {
background: $material-dark-blue;
}

}
6 changes: 4 additions & 2 deletions src/client/mtna/searchbar/searchbar-template.jade
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
md-toolbar(ng-show="!state.searching")
md-toolbar(ng-show="!state.searching" ng-click="state.searching = !state.searching")
.md-toolbar-tools
h1 MT News Archive
span(flex)
md-button(aria-label="search" ng-click="state.searching = !state.searching")
md-button(aria-label="search")
md-icon search
md-toolbar.md-hue-2(ng-show="state.searching")
.md-toolbar-tools
Expand All @@ -12,5 +12,7 @@ md-toolbar.md-hue-2(ng-show="state.searching")
md-input-container(md-theme="input" flex)
label &nbsp;
input.search(ng-model="state.searchString" placeholder="enter search")
md-datepicker(ng-model="state.after" md-placeholder="After")
md-datepicker(ng-model="state.before" md-placeholder="Before")
md-button(aria-label="search" ng-click="state.search()")
md-icon search