From 2ce9c842a1e20c27708e7c68b6d5b8adcf4fa3ed Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 7 Apr 2022 17:48:20 +0200 Subject: [PATCH 01/16] refactor(type_de_champs): use typescript in type de champs editor --- .../TypesDeChampEditor/{Flash.js => Flash.ts} | 18 +- ...{OperationsQueue.js => OperationsQueue.ts} | 34 +++- ...criptionInput.jsx => DescriptionInput.tsx} | 18 +- .../{LibelleInput.jsx => LibelleInput.tsx} | 18 +- ...{MandatoryInput.jsx => MandatoryInput.tsx} | 18 +- .../{MoveButton.jsx => MoveButton.tsx} | 24 +-- .../{TypeDeChamp.jsx => TypeDeChamp.tsx} | 113 +++++++----- ...eOption.jsx => TypeDeChampCarteOption.tsx} | 18 +- .../components/TypeDeChampCarteOptions.jsx | 21 --- .../components/TypeDeChampCarteOptions.tsx | 19 ++ ...ons.jsx => TypeDeChampDropDownOptions.tsx} | 19 +- ...Other.jsx => TypeDeChampDropDownOther.tsx} | 18 +- ...y.jsx => TypeDeChampDropDownSecondary.tsx} | 15 +- ....jsx => TypeDeChampPieceJustificative.tsx} | 62 ++++--- ...s.jsx => TypeDeChampRepetitionOptions.tsx} | 20 +-- ...sSelect.jsx => TypeDeChampTypesSelect.tsx} | 18 +- .../{TypeDeChamps.jsx => TypeDeChamps.tsx} | 23 +-- .../components/TypesDeChampEditor/index.jsx | 46 ----- .../components/TypesDeChampEditor/index.tsx | 54 ++++++ .../TypesDeChampEditor/operations.js | 51 ------ .../TypesDeChampEditor/operations.ts | 71 ++++++++ ...hampsReducer.js => typeDeChampsReducer.ts} | 169 ++++++++++++++---- .../components/TypesDeChampEditor/types.ts | 27 +++ .../components/TypesDeChampEditor/utils.jsx | 14 -- .../components/TypesDeChampEditor/utils.tsx | 16 ++ ...e-upload-error.js => file-upload-error.ts} | 17 +- .../{progress-bar.js => progress-bar.ts} | 55 +++--- .../{uploader.js => uploader.ts} | 39 ++-- package.json | 3 +- yarn.lock | 13 +- 30 files changed, 640 insertions(+), 411 deletions(-) rename app/javascript/components/TypesDeChampEditor/{Flash.js => Flash.ts} (61%) rename app/javascript/components/TypesDeChampEditor/{OperationsQueue.js => OperationsQueue.ts} (58%) rename app/javascript/components/TypesDeChampEditor/components/{DescriptionInput.jsx => DescriptionInput.tsx} (66%) rename app/javascript/components/TypesDeChampEditor/components/{LibelleInput.jsx => LibelleInput.tsx} (66%) rename app/javascript/components/TypesDeChampEditor/components/{MandatoryInput.jsx => MandatoryInput.tsx} (66%) rename app/javascript/components/TypesDeChampEditor/components/{MoveButton.jsx => MoveButton.tsx} (56%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChamp.jsx => TypeDeChamp.tsx} (77%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampCarteOption.jsx => TypeDeChampCarteOption.tsx} (58%) delete mode 100644 app/javascript/components/TypesDeChampEditor/components/TypeDeChampCarteOptions.jsx create mode 100644 app/javascript/components/TypesDeChampEditor/components/TypeDeChampCarteOptions.tsx rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampDropDownOptions.jsx => TypeDeChampDropDownOptions.tsx} (65%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampDropDownOther.jsx => TypeDeChampDropDownOther.tsx} (67%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampDropDownSecondary.jsx => TypeDeChampDropDownSecondary.tsx} (78%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampPieceJustificative.jsx => TypeDeChampPieceJustificative.tsx} (59%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampRepetitionOptions.jsx => TypeDeChampRepetitionOptions.tsx} (81%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChampTypesSelect.jsx => TypeDeChampTypesSelect.tsx} (64%) rename app/javascript/components/TypesDeChampEditor/components/{TypeDeChamps.jsx => TypeDeChamps.tsx} (86%) delete mode 100644 app/javascript/components/TypesDeChampEditor/index.jsx create mode 100644 app/javascript/components/TypesDeChampEditor/index.tsx delete mode 100644 app/javascript/components/TypesDeChampEditor/operations.js create mode 100644 app/javascript/components/TypesDeChampEditor/operations.ts rename app/javascript/components/TypesDeChampEditor/{typeDeChampsReducer.js => typeDeChampsReducer.ts} (55%) create mode 100644 app/javascript/components/TypesDeChampEditor/types.ts delete mode 100644 app/javascript/components/TypesDeChampEditor/utils.jsx create mode 100644 app/javascript/components/TypesDeChampEditor/utils.tsx rename app/javascript/shared/activestorage/{file-upload-error.js => file-upload-error.ts} (84%) rename app/javascript/shared/activestorage/{progress-bar.js => progress-bar.ts} (63%) rename app/javascript/shared/activestorage/{uploader.js => uploader.ts} (70%) diff --git a/app/javascript/components/TypesDeChampEditor/Flash.js b/app/javascript/components/TypesDeChampEditor/Flash.ts similarity index 61% rename from app/javascript/components/TypesDeChampEditor/Flash.js rename to app/javascript/components/TypesDeChampEditor/Flash.ts index 945c6efd8..302c88c9f 100644 --- a/app/javascript/components/TypesDeChampEditor/Flash.js +++ b/app/javascript/components/TypesDeChampEditor/Flash.ts @@ -1,6 +1,14 @@ -export default class Flash { - constructor(isAnnotation) { - this.element = document.querySelector('#flash_messages'); +import invariant from 'tiny-invariant'; + +export class Flash { + element: HTMLDivElement; + isAnnotation: boolean; + timeout?: number; + + constructor(isAnnotation: boolean) { + const element = document.querySelector('#flash_messages'); + invariant(element, 'Flash element is required'); + this.element = element; this.isAnnotation = isAnnotation; } success() { @@ -10,13 +18,13 @@ export default class Flash { this.add('Formulaire enregistré.'); } } - error(message) { + error(message: string) { this.add(message, true); } clear() { this.element.innerHTML = ''; } - add(message, isError) { + add(message: string, isError = false) { const html = `
@@ -20,10 +27,3 @@ function DescriptionInput({ isVisible, handler }) { } return null; } - -DescriptionInput.propTypes = { - isVisible: PropTypes.bool, - handler: PropTypes.object -}; - -export default DescriptionInput; diff --git a/app/javascript/components/TypesDeChampEditor/components/LibelleInput.jsx b/app/javascript/components/TypesDeChampEditor/components/LibelleInput.tsx similarity index 66% rename from app/javascript/components/TypesDeChampEditor/components/LibelleInput.jsx rename to app/javascript/components/TypesDeChampEditor/components/LibelleInput.tsx index f1ca7c07c..c7490cec6 100644 --- a/app/javascript/components/TypesDeChampEditor/components/LibelleInput.jsx +++ b/app/javascript/components/TypesDeChampEditor/components/LibelleInput.tsx @@ -1,7 +1,14 @@ import React from 'react'; -import PropTypes from 'prop-types'; -function LibelleInput({ isVisible, handler }) { +import type { Handler } from '../types'; + +export function LibelleInput({ + isVisible, + handler +}: { + isVisible: boolean; + handler: Handler; +}) { if (isVisible) { return (
@@ -19,10 +26,3 @@ function LibelleInput({ isVisible, handler }) { } return null; } - -LibelleInput.propTypes = { - handler: PropTypes.object, - isVisible: PropTypes.bool -}; - -export default LibelleInput; diff --git a/app/javascript/components/TypesDeChampEditor/components/MandatoryInput.jsx b/app/javascript/components/TypesDeChampEditor/components/MandatoryInput.tsx similarity index 66% rename from app/javascript/components/TypesDeChampEditor/components/MandatoryInput.jsx rename to app/javascript/components/TypesDeChampEditor/components/MandatoryInput.tsx index 436a23345..9a13cc161 100644 --- a/app/javascript/components/TypesDeChampEditor/components/MandatoryInput.jsx +++ b/app/javascript/components/TypesDeChampEditor/components/MandatoryInput.tsx @@ -1,7 +1,14 @@ import React from 'react'; -import PropTypes from 'prop-types'; -function MandatoryInput({ isVisible, handler }) { +import type { Handler } from '../types'; + +export function MandatoryInput({ + isVisible, + handler +}: { + isVisible: boolean; + handler: Handler; +}) { if (isVisible) { return (
@@ -19,10 +26,3 @@ function MandatoryInput({ isVisible, handler }) { } return null; } - -MandatoryInput.propTypes = { - handler: PropTypes.object, - isVisible: PropTypes.bool -}; - -export default MandatoryInput; diff --git a/app/javascript/components/TypesDeChampEditor/components/MoveButton.jsx b/app/javascript/components/TypesDeChampEditor/components/MoveButton.tsx similarity index 56% rename from app/javascript/components/TypesDeChampEditor/components/MoveButton.jsx rename to app/javascript/components/TypesDeChampEditor/components/MoveButton.tsx index c8ec442f9..15796767a 100644 --- a/app/javascript/components/TypesDeChampEditor/components/MoveButton.jsx +++ b/app/javascript/components/TypesDeChampEditor/components/MoveButton.tsx @@ -1,8 +1,17 @@ -import React from 'react'; -import PropTypes from 'prop-types'; +import React, { MouseEventHandler } from 'react'; import { ArrowDownIcon, ArrowUpIcon } from '@heroicons/react/solid'; -function MoveButton({ isEnabled, icon, title, onClick }) { +export function MoveButton({ + isEnabled, + icon, + title, + onClick +}: { + isEnabled: boolean; + icon: string; + title: string; + onClick: MouseEventHandler; +}) { return (