Skip to content

Commit

Permalink
Feature/domain autocomplete getters (#368)
Browse files Browse the repository at this point in the history
* domain autocomplete container skeleton

* Added domain autocomplete getters

* added test for combineDomainList

* adding test for filterTopDomainList

* test for filterVisitedDomainList

* adding tests for formatDomainLabel

* adding test for sortDomainList
  • Loading branch information
just-at-uber authored Jul 26, 2021
1 parent 43fb31e commit 9e1cc44
Show file tree
Hide file tree
Showing 21 changed files with 715 additions and 2 deletions.
13 changes: 12 additions & 1 deletion client/containers/domain-autocomplete/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@
// THE SOFTWARE.

import { connect } from 'vuex-connect';
import { ROUTE_PARAMS_DOMAIN } from '../route/getter-types';
import {
DOMAIN_AUTOCOMPLETE_COMBINED_DOMAIN_LIST,
DOMAIN_AUTOCOMPLETE_IS_LOADING,
DOMAIN_AUTOCOMPLETE_NAVIGATE_TO_DOMAIN_URL,
DOMAIN_AUTOCOMPLETE_SEARCH,
} from './getter-types';
import { DOMAIN_AUTOCOMPLETE_ON_MOUNTED } from './mutation-types';

const actionsToEvents = {
// TODO - updated in future PR
};

const gettersToProps = {
// TODO - updated in future PR
isLoading: DOMAIN_AUTOCOMPLETE_IS_LOADING,
domain: ROUTE_PARAMS_DOMAIN,
domainList: DOMAIN_AUTOCOMPLETE_COMBINED_DOMAIN_LIST,
navigateToDomainUrl: DOMAIN_AUTOCOMPLETE_NAVIGATE_TO_DOMAIN_URL,
search: DOMAIN_AUTOCOMPLETE_SEARCH,
};

const lifecycle = {
Expand Down
1 change: 1 addition & 0 deletions client/containers/domain-autocomplete/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

export const DOMAIN_AUTOCOMPLETE_STATE_PREFIX = 'domainAutocomplete';
export const DOMAIN_AUTOCOMPLETE_TYPE_PREFIX = 'DOMAIN_AUTOCOMPLETE';
export const TOP_DOMAIN_LIST_LIMIT = 15;
39 changes: 39 additions & 0 deletions client/containers/domain-autocomplete/getter-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import { typePrefix } from './helpers';

export const DOMAIN_AUTOCOMPLETE_COMBINED_DOMAIN_LIST = typePrefix(
'COMBINED_DOMAIN_LIST'
);
export const DOMAIN_AUTOCOMPLETE_DOMAIN_LIST = typePrefix('DOMAIN_LIST');
export const DOMAIN_AUTOCOMPLETE_FILTERED_VISITED_DOMAIN_LIST = typePrefix(
'FILTERED_VISITED_DOMAIN_LIST'
);
export const DOMAIN_AUTOCOMPLETE_IS_LOADING = typePrefix('IS_LOADING');
export const DOMAIN_AUTOCOMPLETE_NAVIGATE_TO_DOMAIN_URL = typePrefix(
'NAVIGATE_TO_DOMAIN_URL'
);
export const DOMAIN_AUTOCOMPLETE_SEARCH = typePrefix('SEARCH');
export const DOMAIN_AUTOCOMPLETE_SEARCH_URL = typePrefix('SEARCH_URL');
export const DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST = typePrefix(
'VISITED_DOMAIN_LIST'
);
85 changes: 85 additions & 0 deletions client/containers/domain-autocomplete/getters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import { get } from 'lodash-es';
import {
DOMAIN_AUTOCOMPLETE_COMBINED_DOMAIN_LIST,
DOMAIN_AUTOCOMPLETE_DOMAIN_LIST,
DOMAIN_AUTOCOMPLETE_FILTERED_VISITED_DOMAIN_LIST,
DOMAIN_AUTOCOMPLETE_IS_LOADING,
DOMAIN_AUTOCOMPLETE_NAVIGATE_TO_DOMAIN_URL,
DOMAIN_AUTOCOMPLETE_SEARCH,
DOMAIN_AUTOCOMPLETE_SEARCH_URL,
DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST,
} from './getter-types';
import {
combineDomainList,
filterVisitedDomainList,
filterTopDomainList,
formatDomainList,
sortDomainList,
statePrefix,
} from './helpers';
import { combine } from '~helpers';

const getters = {
[DOMAIN_AUTOCOMPLETE_COMBINED_DOMAIN_LIST]: (_, getters) => {
const visitedDomainList =
getters[DOMAIN_AUTOCOMPLETE_FILTERED_VISITED_DOMAIN_LIST];
const domainList = getters[DOMAIN_AUTOCOMPLETE_DOMAIN_LIST];
const combinedDomainList = combineDomainList({
visitedDomainList,
domainList,
});

return combine(combinedDomainList)(
sortDomainList,
filterTopDomainList,
formatDomainList
);
},
[DOMAIN_AUTOCOMPLETE_DOMAIN_LIST]: state =>
sortDomainList(get(state, statePrefix('domainList')) || []),
[DOMAIN_AUTOCOMPLETE_FILTERED_VISITED_DOMAIN_LIST]: (_, getters) =>
filterVisitedDomainList({
visitedDomainList: getters[DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST],
search: getters[DOMAIN_AUTOCOMPLETE_SEARCH],
}),
[DOMAIN_AUTOCOMPLETE_IS_LOADING]: state =>
get(state, statePrefix('isLoading')) || false,
[DOMAIN_AUTOCOMPLETE_NAVIGATE_TO_DOMAIN_URL]: (_, getters) => {
const search = getters[DOMAIN_AUTOCOMPLETE_SEARCH];

if (!search) {
return '';
}

return `/domains/${search}`;
},
[DOMAIN_AUTOCOMPLETE_SEARCH]: state =>
get(state, statePrefix('search')) || '',
[DOMAIN_AUTOCOMPLETE_SEARCH_URL]: (_, getters) =>
`/api/domains?querystring=${getters[DOMAIN_AUTOCOMPLETE_SEARCH]}`,
[DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST]: state =>
sortDomainList(get(state, statePrefix('visitedDomainList')) || []),
};

export default getters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const combineDomainList = ({ domainList, visitedDomainList }) => {
const domainListUuidList = domainList.map(domain => domain.domainInfo.uuid);
const domainNameList = domainList.map(domain => domain.domainInfo.name);

return [
...domainList,
...visitedDomainList.filter(domain =>
typeof domain === 'string'
? domainNameList.indexOf(domain) === -1
: domainListUuidList.indexOf(domain.domainInfo.uuid) === -1
),
];
};

export default combineDomainList;
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import combineDomainList from './combine-domain-list';

describe('combineDomainList', () => {
describe('when passed domainList & visitedDomainList', () => {
const visitedDomainList = [
'domain1',
{
domainInfo: {
name: 'domain2',
uuid: 2,
},
},
'domain3',
{
domainInfo: {
name: 'domain4',
uuid: 4,
},
},
];

const domainList = [
{
domainInfo: {
name: 'domain0',
uuid: 0,
},
},
{
domainInfo: {
name: 'domain1',
uuid: 1,
},
},
{
domainInfo: {
name: 'domain2',
uuid: 2,
},
},
];

it('should combine the two lists into a single list by removing any duplicates from the visitedDomainList.', () => {
const output = combineDomainList({ domainList, visitedDomainList });

expect(output).toEqual([
{
domainInfo: {
name: 'domain0',
uuid: 0,
},
},
{
domainInfo: {
name: 'domain1',
uuid: 1,
},
},
{
domainInfo: {
name: 'domain2',
uuid: 2,
},
},
'domain3',
{
domainInfo: {
name: 'domain4',
uuid: 4,
},
},
]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import { TOP_DOMAIN_LIST_LIMIT } from '../constants';

const filterTopDomainList = domainList =>
domainList.slice(0, TOP_DOMAIN_LIST_LIMIT);

export default filterTopDomainList;
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2021 Uber Technologies Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import { TOP_DOMAIN_LIST_LIMIT } from '../constants';
import filterTopDomainList from './filter-top-domain-list';

describe('filterTopDomainList', () => {
const createDomainList = size => {
const newDomainList = [];

for (let i = 1; i < size + 1; i++) {
newDomainList.push(`domain${i}`);
}

return newDomainList;
};

describe('when domainList is greater than TOP_DOMAIN_LIST_LIMIT', () => {
let domainList;

beforeEach(() => {
domainList = createDomainList(TOP_DOMAIN_LIST_LIMIT * 2);
});

it('should return only the first domains of size = TOP_DOMAIN_LIST_LIMIT', () => {
const output = filterTopDomainList(domainList);

expect(output.length).toEqual(TOP_DOMAIN_LIST_LIMIT);
expect(output[0]).toEqual('domain1');
expect(output[output.length - 1]).toEqual(
`domain${TOP_DOMAIN_LIST_LIMIT}`
);
});
});
});
Loading

0 comments on commit 9e1cc44

Please sign in to comment.