Skip to content

Commit

Permalink
Fix on searching certain cities
Browse files Browse the repository at this point in the history
Calling Search City API with certain cities (ex: Sydney) returned duplicated results so React did not render them correctly
  • Loading branch information
Brugarolas committed Jan 30, 2019
1 parent 306db1f commit fb6faa0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/api/utils/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ const removeById = (array, id) => {
return found;
}

export default { removeById };
const removeDuplicatesById = (array) => {
const set = new Set();

return array.filter((element) => {
if (set.has(element.id)) {
return false;
}

set.add(element.id);
return true;
});
}

export default { removeById, removeDuplicatesById };
3 changes: 2 additions & 1 deletion src/api/weather/cities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Adapt from './adapt.js';
import ArrayUtils from '@/api/utils/array.js';
import fetch from '@/api/utils/fetch.js';

const API_URL = 'https://openweathermap.org/data/2.5/find';
Expand All @@ -25,7 +26,7 @@ const search = async (url) => {

let json = await response.json();

return json.list.map(Adapt.transformCity);
return ArrayUtils.removeDuplicatesById(json.list).map(Adapt.transformCity);
}

const searchCatchErrors = async (url) => {
Expand Down

0 comments on commit fb6faa0

Please sign in to comment.