Skip to content

Commit

Permalink
refactor: TS fixes & ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Jan 26, 2024
1 parent ea2e3b1 commit 1c5c716
Show file tree
Hide file tree
Showing 15 changed files with 1,168 additions and 1,291 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
},
rules: {
'no-console': 'off',
'@typescript-eslint/ban-ts-comment': 'warn',
},
ignorePatterns: [ 'dist/', 'node_modules/', 'assets/components/node_modules' ],
};
1 change: 1 addition & 0 deletions assets/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@automattic/calypso-build": "^10.0.0",
"@babel/cli": "^7.15.0",
"@babel/core": "^7.15.0",
"@types/wordpress__components": "^23.0.11",
"recursive-copy": "^2.0.13"
},
"babel": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@ import './style.scss';

const { useHistory } = Router;

const Button = ( { href, onClick, ...otherProps } ) => {
type OriginalButtonProps = typeof BaseComponent.defaultProps;
type Props = OriginalButtonProps & {
href?: string;
onClick?: () => void;
};

const Button = ( { href, onClick, ...otherProps }: Props ) => {
const history = useHistory();
const [ isAwaitingOnClick, setIsAwaitingOnClick ] = useState( false );

// If both onClick and href are present, await the onClick action an then redirect.
if ( href && onClick ) {
otherProps.onClick = async () => {
( otherProps as Props ).onClick = async () => {
setIsAwaitingOnClick( true );
await onClick();
setIsAwaitingOnClick( false );
history.push( href.replace( '#', '' ) );
history.push( ( href || '' ).replace( '#', '' ) );
};
} else {
otherProps.href = href;
otherProps.onClick = onClick;
( otherProps as Props ).href = href;
( otherProps as Props ).onClick = onClick;
}
if ( isAwaitingOnClick ) {
otherProps.disabled = true;
}
// @ts-ignore - @wordpress/components' Button can only have either href or onClick, not both.
return <BaseComponent { ...otherProps } />;
};

Expand Down
44 changes: 0 additions & 44 deletions assets/components/src/grid/index.js

This file was deleted.

34 changes: 34 additions & 0 deletions assets/components/src/grid/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Grid
*/

/**
* Internal dependencies
*/
import './style.scss';

/**
* External dependencies
*/
import classnames from 'classnames';

const Grid = ( {
className = '',
columns = 2,
gutter = 32,
noMargin = false,
rowGap = 0,
...otherProps
} ) => {
const classes = classnames(
'newspack-grid',
noMargin && 'newspack-grid--no-margin',
columns && 'newspack-grid__columns-' + columns,
gutter && 'newspack-grid__gutter-' + gutter,
rowGap && 'newspack-grid__row-gap-' + rowGap,
className
);
return <div className={ classes } { ...otherProps } />;
};

export default Grid;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import usePrompt from './usePrompt';
import useObjectState from './useObjectState';
import useOnClickOutside from './useOnClickOutside';

const useUniqueId = prefix => {
const useUniqueId = ( prefix: string ) => {
const id = useRef( `${ prefix }-${ Math.round( Math.random() * 99999 ) }` );
return id.current;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import Router from '../proxied-imports/router';

const { useHistory } = Router;

export default ( when, message ) => {
export default ( when: boolean, message: string ): ( () => void ) => {
const history = useHistory();
const self = useRef( null );

const onWindowOrTabClose = event => {
const onWindowOrTabClose = ( event: Event | undefined ) => {
if ( ! when ) {
return;
}
Expand All @@ -25,10 +25,6 @@ export default ( when, message ) => {
event = window.event;
}

if ( event ) {
event.returnValue = message;
}

return message;
};

Expand All @@ -40,11 +36,11 @@ export default ( when, message ) => {

return () => {
if ( self.current ) {
self.current();
( self.current as () => void )();
}
window.removeEventListener( 'beforeunload', onWindowOrTabClose );
};
}, [ message, when ] );

return self.current;
return self.current || ( () => {} );
};
56 changes: 0 additions & 56 deletions assets/components/src/text-control/index.js

This file was deleted.

65 changes: 65 additions & 0 deletions assets/components/src/text-control/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Text Control
*/

/**
* WordPress dependencies
*/
import { TextControl as BaseComponent } from '@wordpress/components';
import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import './style.scss';

/**
* External dependencies
*/
import classNames from 'classnames';

type BaseComponentProps = {
onChange: ( value: string ) => void;
value: string | number;
};

const TextControl = ( {
className = '',
required = false,
isWide = false,
withMargin = true,
...otherProps
} ) => {
const wrapperRef = useRef< HTMLDivElement >( null );
useEffect( () => {
if ( wrapperRef.current === null ) {
return;
}
const labelEl = wrapperRef.current.querySelector( 'label' );
if ( labelEl ) {
labelEl.setAttribute( 'data-required-text', __( '(required)', 'newspack' ) );
}
}, [ wrapperRef.current ] );
const classes = classNames(
'newspack-text-control',
{
'newspack-text-control--wide': isWide,
'newspack-text-control--with-margin': withMargin,
},
className
);
return required ? (
<div ref={ wrapperRef } className="newspack-text-control--required">
<BaseComponent
className={ classes }
required={ required }
{ ...( otherProps as BaseComponentProps ) }
/>
</div>
) : (
<BaseComponent className={ classes } { ...( otherProps as BaseComponentProps ) } />
);
};

export default TextControl;
43 changes: 26 additions & 17 deletions assets/wizards/engagement/components/prerequisite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ExternalLink } from '@wordpress/components';
import { PrequisiteProps } from './types';
import { ActionCard, Button, Grid, TextControl } from '../../../components/src';
import { HANDOFF_KEY } from '../../../components/src/consts';
import type { Config, ConfigKey } from './types';

/**
* Expandable ActionCard for RAS prerequisites checklist.
Expand All @@ -27,7 +28,7 @@ export default function Prerequisite( {
const hasEmptyFields = () => {
if ( prerequisite.active && prerequisite.fields && prerequisite.warning ) {
const emptyValues = Object.keys( prerequisite.fields ).filter(
fieldName => '' === config[ fieldName ]
fieldName => '' === config[ fieldName as keyof Config ]
);
if ( emptyValues.length ) {
return prerequisite.warning;
Expand All @@ -36,6 +37,8 @@ export default function Prerequisite( {
return null;
};

const fieldKeys = Object.keys( prerequisite.fields || {} ) as ConfigKey[];

const renderInnerContent = () => (
// Inner card content.
<>
Expand All @@ -57,24 +60,30 @@ export default function Prerequisite( {
prerequisite.fields && (
<Grid columns={ 2 } gutter={ 16 }>
<div>
{ Object.keys( prerequisite.fields ).map( fieldName => (
<TextControl
key={ fieldName }
label={ prerequisite.fields[ fieldName ].label }
help={ prerequisite.fields[ fieldName ].description }
{ ...getSharedProps( fieldName, 'text' ) }
/>
) ) }
{ fieldKeys.map( fieldName => {
if ( ! prerequisite.fields || ! prerequisite.fields[ fieldName ] ) {
return undefined;
}
return (
<TextControl
key={ fieldName }
label={ prerequisite.fields[ fieldName ].label }
help={ prerequisite.fields[ fieldName ].description }
{ ...getSharedProps( fieldName, 'text' ) }
/>
);
} ) }

<Button
isPrimary
variant={ 'primary' }
onClick={ () => {
const dataToSave = {};

Object.keys( prerequisite.fields ).forEach( fieldName => {
dataToSave[ fieldName ] = config[ fieldName ];
const dataToSave: Partial< Config > = {};
fieldKeys.forEach( fieldName => {
if ( config[ fieldName ] ) {
// @ts-ignore - not sure what's the issue here.
dataToSave[ fieldName ] = config[ fieldName ];
}
} );

saveConfig( dataToSave );
} }
disabled={ inFlight }
Expand All @@ -99,7 +108,7 @@ export default function Prerequisite( {
{ ( ! prerequisite.hasOwnProperty( 'action_enabled' ) ||
prerequisite.action_enabled ) && (
<Button
isPrimary
variant={ 'primary' }
onClick={ () => {
// Set up a handoff to indicate that the user is coming from the RAS wizard page.
if ( prerequisite.instructions ) {
Expand Down Expand Up @@ -135,7 +144,7 @@ export default function Prerequisite( {
</Button>
) }
{ prerequisite.hasOwnProperty( 'action_enabled' ) && ! prerequisite.action_enabled && (
<Button isSecondary disabled>
<Button variant={ 'secondary' } disabled>
{ prerequisite.disabled_text || prerequisite.action_text }
</Button>
) }
Expand Down
Loading

0 comments on commit 1c5c716

Please sign in to comment.