Skip to content

Commit

Permalink
feat: Send platform in search and autocomplete (#3063)
Browse files Browse the repository at this point in the history
* chore: Send appversion regardless of addonType
Works now that https://github.com/mozilla/addons-server/issues/6206 is closed
* feat: Send platform param with search (fix #2706)
  • Loading branch information
tofumatt committed Sep 11, 2017
1 parent 8d2a024 commit fa21471
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 165 deletions.
34 changes: 22 additions & 12 deletions src/amo/components/SearchForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import { withErrorHandler } from 'core/errorHandler';
import translate from 'core/i18n/translate';
import { getAddonIconUrl } from 'core/imageUtils';
import log from 'core/logger';
import { convertFiltersToQueryParams } from 'core/searchUtils';
import {
autocompleteCancel,
autocompleteStart,
} from 'core/reducers/autocomplete';
import {
convertOSToFilterValue,
convertFiltersToQueryParams,
} from 'core/searchUtils';
import Icon from 'ui/components/Icon';

import './styles.scss';
Expand All @@ -49,6 +52,7 @@ export class SearchFormBase extends React.Component {
url: PropTypes.string.isRequired,
iconUrl: PropTypes.string.isRequired,
})).isRequired,
userAgentInfo: PropTypes.object.isRequired,
}

static defaultProps = {
Expand Down Expand Up @@ -89,14 +93,24 @@ export class SearchFormBase extends React.Component {
}));
}

goToSearch(query) {
const { addonType, api, pathname, router } = this.props;
const filters = { query };
createFiltersFromQuery(newFilters) {
const { addonType, userAgentInfo } = this.props;
const filters = { ...newFilters };

if (addonType) {
filters.addonType = addonType;
}

filters.operatingSystem = convertOSToFilterValue(
userAgentInfo.os.name);

return filters;
}

goToSearch(query) {
const { api, pathname, router } = this.props;
const filters = this.createFiltersFromQuery({ query });

router.push({
pathname: `/${api.lang}/${api.clientApp}${pathname}`,
query: convertFiltersToQueryParams(filters),
Expand Down Expand Up @@ -130,12 +144,7 @@ export class SearchFormBase extends React.Component {
return;
}

const { addonType } = this.props;
const filters = { query: value };

if (addonType) {
filters.addonType = addonType;
}
const filters = this.createFiltersFromQuery({ query: value });

this.setState({
autocompleteIsOpen: true,
Expand Down Expand Up @@ -217,8 +226,8 @@ export class SearchFormBase extends React.Component {
};

const autocompleteIsOpen = this.state.autocompleteIsOpen &&
// This prevents the input to look like Autosuggest is open when there is
// no result coming from the API.
// This prevents the input to look like Autosuggest is open when
// there is no result coming from the API.
this.getSuggestions().length > 0;

return (
Expand Down Expand Up @@ -283,6 +292,7 @@ export function mapStateToProps(state) {
api: state.api,
suggestions: state.autocomplete.suggestions,
loadingSuggestions: state.autocomplete.loading,
userAgentInfo: state.api.userAgentInfo,
};
}

Expand Down
16 changes: 1 addition & 15 deletions src/core/api/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { oneLine } from 'common-tags';

import { addon, callApi } from 'core/api';
import {
ADDON_TYPE_EXTENSION,
ADDON_TYPE_THEME,
CLIENT_APP_ANDROID,
} from 'core/constants';
Expand Down Expand Up @@ -58,25 +57,12 @@ export function search(
_filters.clientApp = 'firefox';
}

// Themes are cross-platform and searching for them with an operatingSystem
// filter will result in no results, so we delete it for now.
// Can be deleted once https://github.com/mozilla/addons-server/issues/6206
// is fixed.
if (_filters.addonType === ADDON_TYPE_THEME) {
delete _filters.operatingSystem;
}

// If the browser is Firefox or Firefox for Android and we're searching for
// extensions, send the appversion param to get extensions marked as
// compatible with this version.
if (
api.userAgentInfo.browser.name === 'Firefox' &&
api.userAgentInfo.os.name !== 'iOS' &&
// TODO: Remove this check once
// https://github.com/mozilla/addons-server/issues/6206 is fixed.
// Right now sending the `appversion` param to search will result
// in no themes being returned.
(!_filters.addonType || _filters.addonType === ADDON_TYPE_EXTENSION)
api.userAgentInfo.os.name !== 'iOS'
) {
const browserVersion = parseInt(api.userAgentInfo.browser.version, 10);

Expand Down
20 changes: 20 additions & 0 deletions src/core/searchUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import log from 'core/logger';


export const operatingSystems = {
Linux: 'linux',
'Mac OS': 'mac',
Windows: 'windows',
};

export const paramsToFilter = {
app: 'clientApp',
appversion: 'compatibleWithVersion',
Expand Down Expand Up @@ -36,6 +45,17 @@ export function convertQueryParamsToFilters(params) {
}, {});
}

export function convertOSToFilterValue(name) {
if (name in operatingSystems) {
return operatingSystems[name];
}

log.info(
`operatingSystem "${name}" not recognized so falling back to no OS.`);

return undefined;
}

export function hasSearchFilters(filters) {
const filtersSubset = { ...filters };
delete filtersSubset.clientApp;
Expand Down

0 comments on commit fa21471

Please sign in to comment.