Skip to content

Commit 5799e8a

Browse files
committed
fix GoogleSheetsSettings form fields state management
1 parent c61e82c commit 5799e8a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/data-sources/google-sheets/GoogleSheetsSettings.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import {
1919
} from '@/data-sources/types';
2020
import { useForm, ValidationRules } from '@/hooks/useForm';
2121
import { GoogleSheetsIcon, GoogleSheetsIconWithText } from '@/settings/icons/GoogleSheetsIcon';
22-
import { StringIdName } from '@/types/common';
22+
import { NumberIdName } from '@/types/common';
2323
import { SelectOption } from '@/types/input';
24+
import { isPositiveIntegerString } from '@/utils/string';
2425

2526
const SERVICE_CONFIG_VERSION = 1;
2627

@@ -94,7 +95,7 @@ export const GoogleSheetsSettings = ( {
9495
};
9596

9697
const onCredentialsChange = ( nextValue: string ) => {
97-
handleOnChange( 'credentials', nextValue );
98+
handleOnChange( 'credentials', JSON.parse( nextValue ) );
9899
};
99100

100101
const onSelectChange = (
@@ -103,15 +104,16 @@ export const GoogleSheetsSettings = ( {
103104
) => {
104105
if ( extra?.event ) {
105106
const { id } = extra.event.target;
106-
let newValue: StringIdName | null = null;
107+
let newValue: NumberIdName | null = null;
107108
if ( id === 'spreadsheet' ) {
108109
const selectedSpreadsheet = spreadsheets?.find(
109110
spreadsheet => spreadsheet.value === value
110111
);
111112
newValue = { id: value, name: selectedSpreadsheet?.label ?? '' };
112-
} else if ( id === 'sheet' ) {
113+
} else if ( id === 'sheet' && isPositiveIntegerString( value ) ) {
114+
const parsedValue = parseInt( value, 10 );
113115
const selectedSheet = sheets?.find( sheet => sheet.value === value );
114-
newValue = { id: value, name: selectedSheet?.label ?? '' };
116+
newValue = { id: parsedValue, name: selectedSheet?.label ?? '' };
115117
}
116118
handleOnChange( id, newValue );
117119
}
@@ -221,7 +223,7 @@ export const GoogleSheetsSettings = ( {
221223
>
222224
<TextareaControl
223225
label={ __( 'Credentials', 'remote-data-blocks' ) }
224-
value={ state.credentials ? JSON.stringify( state.credentials ) : '' }
226+
value={ state.credentials ? JSON.stringify( state.credentials, null, 2 ) : '' }
225227
onChange={ onCredentialsChange }
226228
help={ credentialsHelpText }
227229
rows={ 10 }

src/utils/string.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ export function safeParseJSON< T = unknown >( value: unknown ): T | null {
5252

5353
return null;
5454
}
55+
56+
/**
57+
* Checks if a string is a positive integer value.
58+
*
59+
* @param value string to check
60+
* @returns true if the string is a positive integer value, false otherwise
61+
*/
62+
export function isPositiveIntegerString( value: string ): boolean {
63+
return /^\d+$/.test( value ) && parseInt( value, 10 ) > 0;
64+
}

0 commit comments

Comments
 (0)