Skip to content

Commit

Permalink
fix GoogleSheetsSettings form fields state management
Browse files Browse the repository at this point in the history
  • Loading branch information
shekharnwagh committed Dec 20, 2024
1 parent c61e82c commit 5799e8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/data-sources/google-sheets/GoogleSheetsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
} from '@/data-sources/types';
import { useForm, ValidationRules } from '@/hooks/useForm';
import { GoogleSheetsIcon, GoogleSheetsIconWithText } from '@/settings/icons/GoogleSheetsIcon';
import { StringIdName } from '@/types/common';
import { NumberIdName } from '@/types/common';
import { SelectOption } from '@/types/input';
import { isPositiveIntegerString } from '@/utils/string';

const SERVICE_CONFIG_VERSION = 1;

Expand Down Expand Up @@ -94,7 +95,7 @@ export const GoogleSheetsSettings = ( {
};

const onCredentialsChange = ( nextValue: string ) => {
handleOnChange( 'credentials', nextValue );
handleOnChange( 'credentials', JSON.parse( nextValue ) );
};

const onSelectChange = (
Expand All @@ -103,15 +104,16 @@ export const GoogleSheetsSettings = ( {
) => {
if ( extra?.event ) {
const { id } = extra.event.target;
let newValue: StringIdName | null = null;
let newValue: NumberIdName | null = null;
if ( id === 'spreadsheet' ) {
const selectedSpreadsheet = spreadsheets?.find(
spreadsheet => spreadsheet.value === value
);
newValue = { id: value, name: selectedSpreadsheet?.label ?? '' };

Check failure on line 112 in src/data-sources/google-sheets/GoogleSheetsSettings.tsx

View workflow job for this annotation

GitHub Actions / eslint, prettier, wp-scripts

Type 'string' is not assignable to type 'number'.
} else if ( id === 'sheet' ) {
} else if ( id === 'sheet' && isPositiveIntegerString( value ) ) {
const parsedValue = parseInt( value, 10 );
const selectedSheet = sheets?.find( sheet => sheet.value === value );
newValue = { id: value, name: selectedSheet?.label ?? '' };
newValue = { id: parsedValue, name: selectedSheet?.label ?? '' };
}
handleOnChange( id, newValue );
}
Expand Down Expand Up @@ -221,7 +223,7 @@ export const GoogleSheetsSettings = ( {
>
<TextareaControl
label={ __( 'Credentials', 'remote-data-blocks' ) }
value={ state.credentials ? JSON.stringify( state.credentials ) : '' }
value={ state.credentials ? JSON.stringify( state.credentials, null, 2 ) : '' }
onChange={ onCredentialsChange }
help={ credentialsHelpText }
rows={ 10 }
Expand Down
10 changes: 10 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ export function safeParseJSON< T = unknown >( value: unknown ): T | null {

return null;
}

/**
* Checks if a string is a positive integer value.
*
* @param value string to check
* @returns true if the string is a positive integer value, false otherwise
*/
export function isPositiveIntegerString( value: string ): boolean {
return /^\d+$/.test( value ) && parseInt( value, 10 ) > 0;
}

0 comments on commit 5799e8a

Please sign in to comment.