Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable save changes button unless settings have changed #9386

Merged
merged 14 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/update-9305-settings-disable-save-changes-button
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Disable save changes button until a setting has changed.
1 change: 1 addition & 0 deletions client/components/csv-export-modal/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe( 'RefundModal', () => {

mockUseSettings.mockReturnValue( {
isLoading: false,
isDirty: false,
isSaving: false,
saveSettings: ( a ) => a,
} );
Expand Down
2 changes: 2 additions & 0 deletions client/data/settings/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ export const useSettings = () => {
const isSaving = useSelect( ( select ) =>
select( STORE_NAME ).isSavingSettings()
);
const isDirty = useSelect( ( select ) => select( STORE_NAME ).isDirty() );

const isLoading = useSelect( ( select ) => {
select( STORE_NAME ).getSettings();
Expand All @@ -342,6 +343,7 @@ export const useSettings = () => {
isLoading,
saveSettings,
isSaving,
isDirty,
};
};

Expand Down
7 changes: 7 additions & 0 deletions client/data/settings/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ACTION_TYPES from './action-types';

const defaultState = {
isDirty: false,
isSaving: false,
savingError: null,
data: {},
Expand All @@ -20,12 +21,14 @@ export const receiveSettings = (
return {
...state,
data: action.data,
isDirty: false,
};

case ACTION_TYPES.SET_SETTINGS_VALUES:
return {
...state,
savingError: null,
isDirty: true,
data: {
...state.data,
...action.payload,
Expand All @@ -35,13 +38,16 @@ export const receiveSettings = (
case ACTION_TYPES.SET_IS_SAVING_SETTINGS:
return {
...state,
isDirty:
action.isSaving || action.error ? state.isDirty : false,
isSaving: action.isSaving,
savingError: action.error,
};

case ACTION_TYPES.SET_SELECTED_PAYMENT_METHOD:
return {
...state,
isDirty: true,
data: {
...state.data,
enabled_payment_method_ids: state.data.enabled_payment_method_ids.concat(
Expand All @@ -53,6 +59,7 @@ export const receiveSettings = (
case ACTION_TYPES.SET_UNSELECTED_PAYMENT_METHOD:
return {
...state,
isDirty: true,
data: {
...state.data,
enabled_payment_method_ids: state.data.enabled_payment_method_ids.filter(
Expand Down
4 changes: 4 additions & 0 deletions client/data/settings/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const isSavingSettings = ( state ) => {
return getSettingsState( state ).isSaving || false;
};

export const isDirty = ( state ) => {
return getSettingsState( state ).isDirty || false;
};

export const getAccountStatementDescriptor = ( state ) => {
return getSettings( state ).account_statement_descriptor || '';
};
Expand Down
1 change: 1 addition & 0 deletions client/data/settings/test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ describe( 'Settings hooks tests', () => {
hasFinishedResolution: jest.fn(),
isResolving: jest.fn(),
isSavingSettings: jest.fn(),
isDirty: jest.fn(),
};
} );

Expand Down
22 changes: 22 additions & 0 deletions client/data/settings/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe( 'Settings reducer tests', () => {
isSaving: false,
data: {},
savingError: null,
isDirty: false,
} );
} );

Expand Down Expand Up @@ -60,6 +61,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves fields other than `data` unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
baz: 'quux',
Expand All @@ -74,6 +76,7 @@ describe( 'Settings reducer tests', () => {
const state = reducer( oldState, updateSettings( newSettings ) );

expect( state ).toEqual( {
isDirty: false,
foo: 'bar',
data: {
quuz: 'corge',
Expand Down Expand Up @@ -101,6 +104,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
isSaving: false,
savingError: {},
Expand All @@ -112,6 +116,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: false,
foo: 'bar',
savingError: null,
isSaving: true,
Expand All @@ -137,6 +142,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
is_manual_capture_enabled: false,
Expand All @@ -151,6 +157,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: true,
savingError: null,
foo: 'bar',
data: {
Expand Down Expand Up @@ -181,6 +188,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
account_statement_descriptor: 'Statement',
Expand All @@ -195,6 +203,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
savingError: null,
data: {
Expand Down Expand Up @@ -224,6 +233,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
is_payment_request_enabled: false,
Expand All @@ -238,6 +248,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
savingError: null,
data: {
Expand Down Expand Up @@ -271,6 +282,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
payment_request_enabled_locations: initPaymentRequestState,
Expand All @@ -285,6 +297,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
data: {
payment_request_enabled_locations: enableAllpaymentRequestState,
Expand Down Expand Up @@ -350,6 +363,7 @@ describe( 'Settings reducer tests', () => {
'leaves other fields unchanged `%j`',
( setting ) => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
[ setting.stateKey ]: setting.settingValue,
Expand All @@ -364,6 +378,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
savingError: null,
data: {
Expand All @@ -378,6 +393,7 @@ describe( 'Settings reducer tests', () => {
describe( 'SET_IS_WOOPAY_ENABLED', () => {
test( 'toggles `data.is_woopay_enabled`', () => {
const oldState = {
isDirty: true,
data: {
is_woopay_enabled: false,
},
Expand All @@ -391,6 +407,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
is_woopay_enabled: false,
Expand All @@ -402,6 +419,7 @@ describe( 'Settings reducer tests', () => {
const state = reducer( oldState, updateIsWooPayEnabled( true ) );

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
savingError: null,
data: {
Expand Down Expand Up @@ -430,6 +448,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
woopay_custom_message: '',
Expand All @@ -444,6 +463,7 @@ describe( 'Settings reducer tests', () => {
);

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
data: {
woopay_custom_message: 'test',
Expand All @@ -469,6 +489,7 @@ describe( 'Settings reducer tests', () => {

test( 'leaves other fields unchanged', () => {
const oldState = {
isDirty: false,
foo: 'bar',
data: {
woopay_store_logo: '',
Expand All @@ -480,6 +501,7 @@ describe( 'Settings reducer tests', () => {
const state = reducer( oldState, updateWooPayStoreLogo( 'test' ) );

expect( state ).toEqual( {
isDirty: true,
foo: 'bar',
data: {
woopay_store_logo: 'test',
Expand Down
1 change: 1 addition & 0 deletions client/disputes/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ describe( 'Disputes list', () => {
isLoading: false,
isSaving: false,
saveSettings: ( a ) => a,
isDirty: false,
} );

global.wcpaySettings = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const OrderItemsThresholdCustomForm: React.FC< OrderItemsThresholdCustomFormProp
protectionSettingsUI,
setProtectionSettingsUI,
setProtectionSettingsChanged,
setIsDirty,
} = useContext( FraudPreventionSettingsContext );

const settingUI = useMemo(
Expand Down Expand Up @@ -97,7 +98,10 @@ const OrderItemsThresholdCustomForm: React.FC< OrderItemsThresholdCustomFormProp
placeholder={ '0' }
value={ minItemsCount }
type="number"
onChange={ setMinItemsCount }
onChange={ ( value ) => {
setMinItemsCount( value );
setIsDirty( true );
} }
onKeyDown={ ( e ) =>
/^[+-.,e]$/m.test( e.key ) && e.preventDefault()
}
Expand All @@ -121,7 +125,10 @@ const OrderItemsThresholdCustomForm: React.FC< OrderItemsThresholdCustomFormProp
placeholder={ '0' }
type="number"
value={ maxItemsCount }
onChange={ setMaxItemsCount }
onChange={ ( value ) => {
setMaxItemsCount( value );
setIsDirty( true );
} }
onKeyDown={ ( e ) =>
/^[+-.,e]$/m.test( e.key ) && e.preventDefault()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const PurchasePriceThresholdCustomForm: React.FC< PurchasePriceThresholdCustomFo
protectionSettingsUI,
setProtectionSettingsUI,
setProtectionSettingsChanged,
setIsDirty,
} = useContext( FraudPreventionSettingsContext );

const settingUI = useMemo(
Expand Down Expand Up @@ -111,7 +112,10 @@ const PurchasePriceThresholdCustomForm: React.FC< PurchasePriceThresholdCustomFo
prefix={ currencySymbol }
placeholder={ '0.00' }
value={ minAmount.toString() }
onChange={ ( val ) => setMinAmount( Number( val ) ) }
onChange={ ( val ) => {
setMinAmount( Number( val ) );
setIsDirty( true );
} }
help={ __(
'Leave blank for no limit',
'woocommerce-payments'
Expand All @@ -130,7 +134,10 @@ const PurchasePriceThresholdCustomForm: React.FC< PurchasePriceThresholdCustomFo
prefix={ currencySymbol }
placeholder={ '0.00' }
value={ maxAmount.toString() }
onChange={ ( val ) => setMaxAmount( Number( val ) ) }
onChange={ ( val ) => {
setMaxAmount( Number( val ) );
setIsDirty( true );
} }
help={ __(
'Leave blank for no limit',
'woocommerce-payments'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe( 'Address mismatch card', () => {
setProtectionSettingsUI: setSettings,
protectionSettingsChanged: false,
setProtectionSettingsChanged: jest.fn(),
setIsDirty: jest.fn(),
};
test( 'renders correctly', () => {
settings.address_mismatch.enabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe( 'AVS mismatch card', () => {
setProtectionSettingsUI: setSettings,
protectionSettingsChanged: false,
setProtectionSettingsChanged: jest.fn(),
setIsDirty: jest.fn(),
};
const { container } = render(
<FraudPreventionSettingsContext.Provider value={ contextValue }>
Expand Down Expand Up @@ -70,6 +71,7 @@ describe( 'AVS mismatch card', () => {
setProtectionSettingsUI: setSettings,
protectionSettingsChanged: false,
setProtectionSettingsChanged: jest.fn(),
setIsDirty: jest.fn(),
};
const { container } = render(
<FraudPreventionSettingsContext.Provider value={ contextValue }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe( 'CVC verification card', () => {
setProtectionSettingsUI: setSettings,
protectionSettingsChanged: false,
setProtectionSettingsChanged: jest.fn(),
setIsDirty: jest.fn(),
};
const { container } = render(
<FraudPreventionSettingsContext.Provider value={ contextValue }>
Expand Down Expand Up @@ -73,6 +74,7 @@ describe( 'CVC verification card', () => {
setProtectionSettingsUI: setSettings,
protectionSettingsChanged: false,
setProtectionSettingsChanged: jest.fn(),
setIsDirty: jest.fn(),
};
const { container } = render(
<FraudPreventionSettingsContext.Provider value={ contextValue }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ describe( 'International IP address card', () => {
setProtectionSettingsUI: setSettings,
protectionSettingsChanged: false,
setProtectionSettingsChanged: jest.fn(),
isDirty: false,
setIsDirty: jest.fn(),
};
global.wcSettings = {
admin: {
Expand Down
Loading
Loading