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