Skip to content

Commit 0f79127

Browse files
authored
Feature/domain autocomplete cross region (#415)
* Added domain autocomplete to include cluster name on select of domain * logging error object
1 parent db5dc00 commit 0f79127

File tree

5 files changed

+115
-9
lines changed

5 files changed

+115
-9
lines changed

client/containers/domain-autocomplete/actions.js

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
import { debounce } from 'lodash-es';
2323
import { ROUTE_PUSH } from '../route/action-types';
24+
import {
25+
CROSS_REGION_ALLOWED_CROSS_ORIGIN,
26+
CROSS_REGION_CLUSTER_ORIGIN_LIST,
27+
} from '../cross-region/getter-types';
2428
import {
2529
DOMAIN_AUTOCOMPLETE_FETCH_DOMAIN_LIST,
2630
DOMAIN_AUTOCOMPLETE_ON_CHANGE,
@@ -33,33 +37,75 @@ import {
3337
DOMAIN_AUTOCOMPLETE_SET_VISITED_DOMAIN_LIST,
3438
} from './mutation-types';
3539
import {
36-
DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST,
3740
DOMAIN_AUTOCOMPLETE_SEARCH_URL,
41+
DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST,
3842
} from './getter-types';
3943
import { DEBOUNCE_WAIT } from './constants';
40-
import { updateVisitedDomainList } from './helpers';
44+
import {
45+
updateVisitedDomainList,
46+
filterDuplicatesFromDomainList,
47+
} from './helpers';
4148
import { httpService } from '~services';
4249

4350
const actions = {
4451
[DOMAIN_AUTOCOMPLETE_FETCH_DOMAIN_LIST]: debounce(
4552
async ({ commit, getters }) => {
4653
const searchUrl = getters[DOMAIN_AUTOCOMPLETE_SEARCH_URL];
54+
const allowedCrossOrigin = getters[CROSS_REGION_ALLOWED_CROSS_ORIGIN];
55+
const clusterOriginList = getters[CROSS_REGION_CLUSTER_ORIGIN_LIST];
56+
57+
const fetchDomainForOrigin = async ({ clusterName = '', origin }) => {
58+
try {
59+
const result = await httpService.get(
60+
`${origin}${searchUrl}`,
61+
origin && {
62+
credentials: 'include',
63+
mode: 'cors',
64+
}
65+
);
4766

48-
const domainList = await httpService.get(searchUrl);
67+
return result;
68+
} catch (error) {
69+
console.warn(
70+
`Failed to fetch result from cluster "${clusterName}" with origin "${origin}" with error:`,
71+
error
72+
);
73+
}
74+
};
4975

50-
commit(DOMAIN_AUTOCOMPLETE_SET_IS_LOADING, false);
76+
if (allowedCrossOrigin && clusterOriginList) {
77+
const domainListSet = await Promise.all(
78+
clusterOriginList.map(fetchDomainForOrigin)
79+
);
5180

52-
commit(DOMAIN_AUTOCOMPLETE_SET_DOMAIN_LIST, domainList);
81+
const domainList = filterDuplicatesFromDomainList(
82+
domainListSet.filter(result => !!result).flat()
83+
);
84+
85+
commit(DOMAIN_AUTOCOMPLETE_SET_IS_LOADING, false);
86+
commit(DOMAIN_AUTOCOMPLETE_SET_DOMAIN_LIST, domainList);
87+
} else {
88+
const domainList = await fetchDomainForOrigin({ origin: '' });
89+
90+
commit(DOMAIN_AUTOCOMPLETE_SET_IS_LOADING, false);
91+
commit(DOMAIN_AUTOCOMPLETE_SET_DOMAIN_LIST, domainList);
92+
}
5393
},
5494
DEBOUNCE_WAIT
5595
),
56-
[DOMAIN_AUTOCOMPLETE_ON_CHANGE]: ({ commit, dispatch, getters }, payload) => {
96+
[DOMAIN_AUTOCOMPLETE_ON_CHANGE]: async (
97+
{ commit, dispatch, getters },
98+
payload
99+
) => {
57100
if (!payload) {
58101
return;
59102
}
60103

61104
const { value } = payload;
62105
const visitedDomainList = getters[DOMAIN_AUTOCOMPLETE_VISITED_DOMAIN_LIST];
106+
const allowedCrossOrigin = getters[CROSS_REGION_ALLOWED_CROSS_ORIGIN];
107+
const dispatchToGlobalRoute = () =>
108+
dispatch(ROUTE_PUSH, `/domains/${value.domainInfo.name}`);
63109

64110
const updatedVisitedDomainList = updateVisitedDomainList({
65111
value,
@@ -71,7 +117,18 @@ const actions = {
71117
updatedVisitedDomainList
72118
);
73119

74-
dispatch(ROUTE_PUSH, `/domains/${value.domainInfo.name}`);
120+
if (
121+
value.isGlobalDomain ||
122+
!allowedCrossOrigin ||
123+
!value.replicationConfiguration
124+
) {
125+
return dispatchToGlobalRoute();
126+
}
127+
128+
dispatch(
129+
ROUTE_PUSH,
130+
`/domains/${value.domainInfo.name}/${value.replicationConfiguration.activeClusterName}`
131+
);
75132
},
76133
[DOMAIN_AUTOCOMPLETE_ON_SEARCH]: async ({ commit, dispatch }, payload) => {
77134
commit(DOMAIN_AUTOCOMPLETE_SET_SEARCH, payload);

client/containers/domain-autocomplete/connector.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ const gettersToProps = {
4747
};
4848

4949
const lifecycle = {
50-
mounted: ({ commit }) => commit(DOMAIN_AUTOCOMPLETE_ON_MOUNTED),
50+
mounted: ({ commit }) => {
51+
commit(DOMAIN_AUTOCOMPLETE_ON_MOUNTED);
52+
},
5153
};
5254

5355
export default connect({
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
const filterDuplicatesFromDomainList = domainList => {
23+
return domainList.reduce(
24+
(accumulator, domain) => {
25+
const domainName = domain.domainInfo.name;
26+
27+
if (
28+
!domain.isGlobalDomain ||
29+
!accumulator.domainNameList.includes(domainName)
30+
) {
31+
if (domain.isGlobalDomain) {
32+
accumulator.domainNameList.push(domainName);
33+
}
34+
35+
accumulator.result.push(domain);
36+
}
37+
38+
return accumulator;
39+
},
40+
{ domainNameList: [], result: [] }
41+
).result;
42+
};
43+
44+
export default filterDuplicatesFromDomainList;

client/containers/domain-autocomplete/helpers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// THE SOFTWARE.
2121

2222
export { default as combineDomainList } from './combine-domain-list';
23+
export { default as filterDuplicatesFromDomainList } from './filter-duplicates-from-domain-list';
2324
export { default as filterTopDomainList } from './filter-top-domain-list';
2425
export { default as filterVisitedDomainList } from './filter-visited-domain-list';
2526
export { default as formatDomainLabel } from './format-domain-label';

client/containers/domain-autocomplete/helpers/update-visited-domain-list.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const updateVisitedDomainList = ({ value, visitedDomainList }) => {
2626
const uuid = value.domainInfo.uuid || null;
2727

2828
const matchedDomainIndex = visitedDomainList.findIndex(
29-
domain => domain.domainInfo.uuid === uuid || domain.domainInfo.name === name
29+
domain =>
30+
domain.domainInfo.uuid === uuid ||
31+
((!uuid || !domain.domainInfo.uuid) && domain.domainInfo.name === name)
3032
);
3133

3234
if (matchedDomainIndex === -1) {

0 commit comments

Comments
 (0)