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

Refactor: Utilisation de TypeScript #51

Merged
merged 42 commits into from
Aug 21, 2019
Merged

Refactor: Utilisation de TypeScript #51

merged 42 commits into from
Aug 21, 2019

Conversation

maxime-gendron
Copy link
Contributor

@maxime-gendron maxime-gendron commented Aug 12, 2019

J'ai intégré DE-55 dans ce PR.

Copy link
Contributor

@christian-roy christian-roy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il reste quelques erreurs à corriger qui sortent avec la compilation TypeScript et TS Lint.

Pour sortir l'ensemble des erreurs de compilation tu peux installer TypeScript globalement avec yarn global add typescript et ensuite rouler tsc (ou tsc -w pour avoir le watch). Quand je le roule dans le package React ça me dit qu'il reste 141 erreurs.

Même chose pour TS Lint, que tu peux installer globalement avec yarn global add tslint et ensuite tu peux le rouler dans le package React avec tslint --project . pour sortir les erreurs. Il y en a une trentaine présentement.

const [{ value }, setValue] = useState({ value: '' });

const handleChange = event => {
const handleChange = (event: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event: ChangeEvent<HTMLInputElement> (import { ChangeEvent} from 'react')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Et c'était déjà de même, mais pas besoin de faire onChange={event => handleChange(event)} dans l'Input. Tu pourrais faire directement handleChange={handleChange} (même chose pour onKeyDown)

@@ -98,7 +108,7 @@ const SearchInput = ({ disabled, hasButton, id, label, onChange, onSearch }) =>
}
};

const handleKeyDown = event => {
const handleKeyDown = (event: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event: KeyboardEvent<HTMLInputElement> (import { KeyboardEvent } from 'react')

disabled?: boolean;
id: string;
label: string;
onBlur?: ((...args: any[]) => void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onBlur?(value: string): void;

id: string;
label: string;
onBlur?: ((...args: any[]) => void);
onChange?: ((...args: any[]) => void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onChange?(value: string): void;

label: string;
onBlur?: ((...args: any[]) => void);
onChange?: ((...args: any[]) => void);
onFocus?: ((...args: any[]) => void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onFocus?(value: string): void;

minWidth?: number;
}

class MediaView extends Component<{}, {screenWidth: number, children?: Children}> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faire une interface pour le state et passer le type MediaViewProps comme type de props. Et children dans le state n'est pas utilisé.

interface State {
    screenWidth: number;
}

class MediaView extends Component<MediaViewProps, State> {
[...]
}

}

class MediaView extends Component<{}, {screenWidth: number, children?: Children}> {
constructor(props: any[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

props: MediaViewProps

@@ -37,3 +45,5 @@ export default class MediaView extends Component {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ligne 16 : Parenthèses inutiles, et espace de trop avant la virgule
Ligne 27 : Ça devrait être window.removeEventListener plutôt que window.addEventListener je pense.
componentDidMount, componentWillMount et handleScreeResize (typo) : devraient rien retourner. D'ailleurs le lint doit te dire de rajouter le type de retour : void et : ReactFragment | null pour la méthode render
Ligne 42 : children peut être undefined. Je suis pas certain que React aime ça

value: number;
}

const getStep = ({ step, max, value }: GetStepProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L'interface n'est pas nécessaire je pense.

function getStep(step: number, max: number, value: number) : ReactElement {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@@ -76,13 +83,13 @@ const getStep = (step, max, value) => {
return <StepComponent key={step} />;
};

const Progress = ({ max, value }) => (
const Progress = ({ max, value }: {max: number, value: number}) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface pour les Props plutôt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@@ -0,0 +1,6112 @@
{
"name": "@equisoft/design-elements-react",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supprime ce fichier, il faut utiliser yarn et non npm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@@ -32,6 +33,7 @@
"url": "https://github.com/kronostechnologies/design-elements/issues"
},
"dependencies": {
"@types/lodash-es": "^4.17.3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devrait être dans les devDependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

import abstractStyle from './styles/abstract';
import { styles } from './styles/abstract';

export type Child = ReactNode | ReactNode[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supprime ce type, ReactNode inclut déjà ReactNode[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@@ -143,7 +153,7 @@ const SearchInput = ({ disabled, hasButton, id, label, onChange, onSearch }) =>
(
<SearchSubmit
disabled={disabled}
type="submit"
className="primary"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est voulu le className?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui, il y a une propriété className sur le component searchButton.

<StyledTextArea
disabled={disabled}
id={id}
onBlur={(event: FocusEvent<HTMLTextAreaElement>) => {handleBlur(event); }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onBlur={handleBlur}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

disabled={disabled}
id={id}
onBlur={(event: FocusEvent<HTMLTextAreaElement>) => {handleBlur(event); }}
onChange={(event: ChangeEvent<HTMLTextAreaElement>) => {handleChange(event); }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onChange={handleChange}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

color?: string;
}

const Legend = ({ items }: any) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu devrais créér une interface pour les props :

interface Props {
    items: Item[];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

<List>
{items.map(item => (
{items.map((item: ItemProps) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En typant comme il faut les props et items, tu n'aurais pas besoin de spécifier item: ItemProps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

font-size: 0.875rem;
`;

const ProgressBar = ({ content }) => (
interface ElProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ça veut dire quoi El?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EL faisait référence à el(element) dans le map:

<React.Fragment>
        {content.map((el) => (
            <div>
                <Label secondary={el.secondary || false}>{el.descriptionLabel}</Label>
                <Bar
                    color={el.color}
                    endLabel={el.endLabel}
                    percent={el.percent}
                    secondary={el.secondary || false}
                />
            </div>
        ))}
 </React.Fragment>

J'ai opté pour une solution similaire à ce que tu as suggéré pour legend.tsx:

interface ProgressBarProps {
    content: {color: string, descriptionLabel: string, endLabel: string, percent: string, secondary?: boolean}[];
}

const ProgressBar = ({ content }: ProgressBarProps) => (

Cela permet d'éliminer ElProps.

secondary: boolean;
}

const ProgressBar = ({ content }: any) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fait une interface pour typer les props et content

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

"paths": {}
"paths": {},
"outDir": "./dist",
"declaration": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faurait changer le plugin rollup pour https://github.com/ezolenko/rollup-plugin-typescript2, celui utilisé (https://github.com/rollup/rollup-plugin-typescript) supporte pas declaration: true. Quand on fait yarn build, ça ne fait que builder le bundle js dans dist/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merci pour l'info! J'ai fait la conversion.

Copy link
Contributor

@christian-roy christian-roy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gros refactor! 👍

Quelques modifications et questions. Les commentaires restants du review de Max M seraient aussi à corriger.

@@ -58,10 +59,11 @@
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-react": "^7.12.4",
"husky": "^2.3.0",
"lodash-es": "^4.17.14",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devrait être dans dependencies (à inverser avec @types/lodash-es).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

@@ -4,3 +4,4 @@
dist/
node_modules/
yarn-error.log
.rpt2_cache/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce que c'est un dossier qui est créé automatiquement quand on génère les .d.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est le folder de cache généré par Rollup.

@@ -21,6 +21,8 @@ export default {
exclude: 'node_modules/**',
}),
svgr({ icon: true }),
typescript(),
typescript({
objectHashIgnoreUnknownHack: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce que c'est parce qu'on avait cette erreur : ezolenko/rollup-plugin-typescript2#105 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui c'est exactement ça.

import abstractStyle from './styles/abstract';
import { styles } from './styles/abstract';

export type Child = ReactNode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si on fait juste un équivalent syntaxique, on ne serait pas mieux de laisser ReactNode et nous éviter de faire l'import de l'alias ailleurs dans l'application?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je suis d'accord, je vais changer ça.

@@ -1,16 +1,18 @@
import { ReactNode } from 'react';
import styled from 'styled-components';

import abstractStyle from './styles/abstract';
import { styles } from './styles/abstract';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quand on importe le style de plusieurs fichiers différents ça crée des collision de noms de tous les nommes styles. J'ai commencé à leur donné des noms spécifiques, on pourrait probablement faire la même chose ici (quelque chose comme { abstractStyle }).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

screenWidth: number;
}

class MediaView extends Component<MediaViewProps, State> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devrait être refactored en Pure Function Component. Je crée une carte.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parfait!

@@ -6,7 +6,7 @@ const Container = styled.div `
display: flex;
margin-bottom: 1rem;
p {
color: ${props => (props.secondary ? 'rgb(87, 102, 110)' : 'rgb(0, 0, 0)')};
color: ${(props: {secondary: boolean}) => (props.secondary ? 'rgb(87, 102, 110)' : 'rgb(0, 0, 0)')};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce qu'on est obligé de re-spécifier le type ici considérant que l'interface des props est déjà déclarée plus bas? On ne pourrais pas juste faire props: BarProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est un problème généré par l'utilisation de styled-components avec TypeScript. Si je met BarProps comme sur la component, la styled-component Container s'attend à recevoir toutes les components de BarProps ce qui génère une erreur TypeScript.

`;

interface ProgressBarProps {
content: {color: string, descriptionLabel: string, endLabel: string, percent: string, secondary?: boolean}[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pense que ça serait plus clean de créer une interface séparée pour le content et l'assigner ici.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonne idée! J'aurais pas pensé mettre une interface dans une interface.

@@ -2,14 +2,17 @@
"extends": "./node_modules/@equisoft/tslint-config/tsconfig.standards.json",
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"module": "ESNext",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi est-ce qu'on fait cette modif?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nécessaire à Rollup quand il est utilisé avec https://github.com/ezolenko/rollup-plugin-typescript2.

@@ -1,5 +1,5 @@
import { configure } from '@storybook/react';
import '@equisoft/design-elements-web/style/body.css';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laisser le link vers le fichier CSS. Je pense qu'il manquerait juste à update la version de @equisoft/design-elements-web dans le package.json de Storybook pour la dernière version (0.5.0) et le fichier CSS va être là.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai update la version et ça fonctionne. J'ai remis le fichier .css dans config.js.

@christian-roy christian-roy merged commit 5371cb7 into master Aug 21, 2019
@christian-roy christian-roy deleted the dev/DE-54 branch August 22, 2019 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants