Skip to content

Commit 2bdae4c

Browse files
committed
perf(common): CHECKOUT-4272 Update country reducer to transform state only when necessary
1 parent 83d9ca3 commit 2bdae4c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/geography/country-reducer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { combineReducers, composeReducers, Action } from '@bigcommerce/data-store';
22

33
import { clearErrorReducer } from '../common/error';
4+
import { arrayReplace, objectSet } from '../common/utility';
45

56
import Country from './country';
67
import { CountryActionType, LoadCountriesAction } from './country-actions';
@@ -25,7 +26,7 @@ function dataReducer(
2526
): Country[] | undefined {
2627
switch (action.type) {
2728
case CountryActionType.LoadCountriesSucceeded:
28-
return action.payload || [];
29+
return arrayReplace(data, action.payload);
2930

3031
default:
3132
return data;
@@ -39,10 +40,10 @@ function errorsReducer(
3940
switch (action.type) {
4041
case CountryActionType.LoadCountriesRequested:
4142
case CountryActionType.LoadCountriesSucceeded:
42-
return { ...errors, loadError: undefined };
43+
return objectSet(errors, 'loadError', undefined);
4344

4445
case CountryActionType.LoadCountriesFailed:
45-
return { ...errors, loadError: action.payload };
46+
return objectSet(errors, 'loadError', action.payload);
4647

4748
default:
4849
return errors;
@@ -55,11 +56,11 @@ function statusesReducer(
5556
): CountryStatusesState {
5657
switch (action.type) {
5758
case CountryActionType.LoadCountriesRequested:
58-
return { ...statuses, isLoading: true };
59+
return objectSet(statuses, 'isLoading', true);
5960

6061
case CountryActionType.LoadCountriesSucceeded:
6162
case CountryActionType.LoadCountriesFailed:
62-
return { ...statuses, isLoading: false };
63+
return objectSet(statuses, 'isLoading', false);
6364

6465
default:
6566
return statuses;

0 commit comments

Comments
 (0)