➖ Added an object diff check to search_results_updated reducer
This commit is contained in:
@@ -117,7 +117,7 @@ export const AcquisitionPanel = (
|
|||||||
{/* AirDC++ results */}
|
{/* AirDC++ results */}
|
||||||
<div className="columns">
|
<div className="columns">
|
||||||
{!isNil(airDCPPSearchResults) && !isEmpty(airDCPPSearchResults) ? (
|
{!isNil(airDCPPSearchResults) && !isEmpty(airDCPPSearchResults) ? (
|
||||||
<table className="table is-striped">
|
<table className="table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
AIRDCPP_BUNDLES_FETCHED,
|
AIRDCPP_BUNDLES_FETCHED,
|
||||||
} from "../constants/action-types";
|
} from "../constants/action-types";
|
||||||
import { LOCATION_CHANGE } from "connected-react-router";
|
import { LOCATION_CHANGE } from "connected-react-router";
|
||||||
|
import { difference } from "../shared/utils/object.utils";
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
searchResults: [],
|
searchResults: [],
|
||||||
@@ -29,10 +30,16 @@ function airdcppReducer(state = initialState, action) {
|
|||||||
};
|
};
|
||||||
case AIRDCPP_SEARCH_RESULTS_UPDATED:
|
case AIRDCPP_SEARCH_RESULTS_UPDATED:
|
||||||
const bundleToUpdateIndex = state.searchResults.findIndex(
|
const bundleToUpdateIndex = state.searchResults.findIndex(
|
||||||
(bundle) => bundle.id === action.groupedResult.result.id,
|
(bundle) => bundle.result.id === action.groupedResult.result.id,
|
||||||
);
|
);
|
||||||
const updatedState = [...state.searchResults];
|
const updatedState = [...state.searchResults];
|
||||||
updatedState[bundleToUpdateIndex] = action.groupedResult;
|
|
||||||
|
if (
|
||||||
|
difference(updatedState[bundleToUpdateIndex], action.groupedResult) !==
|
||||||
|
{}
|
||||||
|
) {
|
||||||
|
updatedState[bundleToUpdateIndex] = action.groupedResult;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
|||||||
22
src/client/shared/utils/object.utils.ts
Normal file
22
src/client/shared/utils/object.utils.ts
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user