From 8db52e04074da14c52965a97a876abc4ca5f262b Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Sat, 25 Sep 2021 08:52:29 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9E=96=20Added=20an=20object=20diff=20check?= =?UTF-8?q?=20to=20search=5Fresults=5Fupdated=20reducer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/components/AcquisitionPanel.tsx | 2 +- src/client/reducers/airdcpp.reducer.ts | 11 +++++++++-- src/client/shared/utils/object.utils.ts | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/client/shared/utils/object.utils.ts diff --git a/src/client/components/AcquisitionPanel.tsx b/src/client/components/AcquisitionPanel.tsx index 6bff0f2..336afe1 100644 --- a/src/client/components/AcquisitionPanel.tsx +++ b/src/client/components/AcquisitionPanel.tsx @@ -117,7 +117,7 @@ export const AcquisitionPanel = ( {/* AirDC++ results */}
{!isNil(airDCPPSearchResults) && !isEmpty(airDCPPSearchResults) ? ( - +
diff --git a/src/client/reducers/airdcpp.reducer.ts b/src/client/reducers/airdcpp.reducer.ts index 6dfb791..c9e43c9 100644 --- a/src/client/reducers/airdcpp.reducer.ts +++ b/src/client/reducers/airdcpp.reducer.ts @@ -8,6 +8,7 @@ import { AIRDCPP_BUNDLES_FETCHED, } from "../constants/action-types"; import { LOCATION_CHANGE } from "connected-react-router"; +import { difference } from "../shared/utils/object.utils"; const initialState = { searchResults: [], @@ -29,10 +30,16 @@ function airdcppReducer(state = initialState, action) { }; case AIRDCPP_SEARCH_RESULTS_UPDATED: const bundleToUpdateIndex = state.searchResults.findIndex( - (bundle) => bundle.id === action.groupedResult.result.id, + (bundle) => bundle.result.id === action.groupedResult.result.id, ); const updatedState = [...state.searchResults]; - updatedState[bundleToUpdateIndex] = action.groupedResult; + + if ( + difference(updatedState[bundleToUpdateIndex], action.groupedResult) !== + {} + ) { + updatedState[bundleToUpdateIndex] = action.groupedResult; + } return { ...state, diff --git a/src/client/shared/utils/object.utils.ts b/src/client/shared/utils/object.utils.ts new file mode 100644 index 0000000..53a26ee --- /dev/null +++ b/src/client/shared/utils/object.utils.ts @@ -0,0 +1,22 @@ +import { transform, isEqual, isObject } from "lodash"; + +/** + * Deep diff between two object, using lodash + * @param {Object} object Object compared + * @param {Object} base Object to compare with + * @return {Object} Return a new object who represent the diff + */ +export const difference = (object, base) => { + return changes(object, base); +}; + +const changes = (object, base) => { + return transform(object, (result, value, key) => { + if (!isEqual(value, base[key])) { + result[key] = + isObject(value) && isObject(base[key]) + ? changes(value, base[key]) + : value; + } + }); +};
Name