Updates search route with support for queryParams, and initial refactor for

This commit is contained in:
Seyi Adebajo 2017-03-24 21:49:08 -07:00 committed by Mars Lan
parent 48a96d519b
commit 005f3f55bd

View File

@ -1,64 +1,86 @@
import Ember from 'ember'; import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin'; import AuthenticatedRouteMixin
from 'ember-simple-auth/mixins/authenticated-route-mixin';
import buildUrl from 'wherehows-web/utils/build-url';
export default Ember.Route.extend(AuthenticatedRouteMixin, { const {
queryParams: { Route,
category: { isBlank,
refreshModel: true $: { getJSON }
} = Ember;
const queryParams = ['keyword', 'category', 'page', 'source'];
// TODO: DSS-6581 Create URL retrieval module
const urlRoot = '/api/v1/search';
export default Route.extend(AuthenticatedRouteMixin, {
// Set `refreshModel` for each queryParam to true
// so each url state change results in a full transition
queryParams: (() =>
queryParams.reduce(
(queryParams, param) => {
queryParams[param] = { refreshModel: true };
return queryParams;
}, },
keywords: { {}
refreshModel: true ))(),
/**
* Applies the returned results object as the route model and sets
* keyword property on the route controller
* @param {Object} controller search route controller
* @param {Object} model search results
*/
setupController(controller, model) {
const { keywords } = model;
controller.setProperties({
model,
keyword: keywords
});
}, },
source: {
refreshModel: true /**
}, *
page: { * @param params
refreshModel: true */
} model(params = {}) {
}, const searchUrl = queryParams.reduce(
model: function (params) { (url, queryParam) => {
this.controller.set('loading', true); const queryValue = params[queryParam];
var q; if (!isBlank(queryValue)) {
if (params) { return buildUrl(url, queryParam, queryValue);
q = params;
}
else {
q = convertQueryStringToObject();
} }
var keyword = atob(q.keywords); return url;
var url = 'api/v1/search' + '?page=' + params.page + "&keyword=" + keyword; },
if (q.category) { urlRoot
url += ("&category=" + q.category.toLowerCase()); );
currentTab = q.category.toProperCase();
updateActiveTab(); return Promise.resolve(getJSON(searchUrl))
} .then(({ status, result }) => {
if (q.source) { if (status === 'ok') {
url += '&source=' + q.source; const {
} keywords,
$.get(url, data => { data
if (data && data.status == "ok") { } = result;
var result = data.result;
var keywords = result.keywords; data.forEach((datum, index, data) => {
window.g_currentCategory = result.category; const { schema } = datum;
updateSearchCategories(result.category);
for (var index = 0; index < result.data.length; index++) {
var schema = result.data[index].schema;
if (schema) { if (schema) {
result.data[index].originalSchema = schema; datum.originalSchema = schema;
highlightResults(result.data, index, keyword); // TODO: DSS-6122 refactor global reference and function
} window.highlightResults(data, index, keywords);
}
this.controller.set('model', result);
this.controller.set('keyword', keyword);
this.controller.set('isMetric', false);
if (result.data.length > 0) {
this.controller.set('showNoResult', false);
} else {
this.controller.set('showNoResult', true);
}
this.controller.set('loading', false)
} }
}); });
return result;
}
return Promise.reject(
new Error(`Request for ${searchUrl} failed with: ${status}`)
);
})
.catch(() => ({}));
} }
}); });