From b254096c2c0d22806353129678f98b4277147ad2 Mon Sep 17 00:00:00 2001 From: Etienne ANNE Date: Tue, 5 Nov 2024 10:34:59 +0100 Subject: [PATCH] refacto --- otoroshi/javascript/src/components/window.js | 371 ++++++++---------- .../javascript/src/pages/DangerZonePage.js | 9 +- otoroshi/javascript/src/pages/DraftsPage.js | 50 +++ 3 files changed, 226 insertions(+), 204 deletions(-) create mode 100644 otoroshi/javascript/src/pages/DraftsPage.js diff --git a/otoroshi/javascript/src/components/window.js b/otoroshi/javascript/src/components/window.js index 0dc5280cd..2c9bdf135 100644 --- a/otoroshi/javascript/src/components/window.js +++ b/otoroshi/javascript/src/components/window.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react'; +import React, { Component, useEffect, useRef } from 'react'; import ReactDOM from 'react-dom'; import isFunction from 'lodash/isFunction'; import isString from 'lodash/isString'; @@ -7,243 +7,214 @@ import { WizardFrame } from './wizardframe'; const KEY_NAME_ESC = 'Escape'; const KEY_EVENT_TYPE = 'keyup'; -const handleEscKey = (event, onClose) => { - if (event.key === KEY_NAME_ESC) { - onClose(); - } -}; +function EscapeModalListener({ close, children }) { -const handleClickOutside = (event, ref, onClose) => { - if (ref.current && !ref.current.contains(event.target)) { - onClose(); - } -}; + const ref = useRef() -class Alert extends Component { - constructor(props) { - super(props); - this.modalRef = React.createRef(); - } + const handleEscKey = (event, onClose) => { + if (event.key === KEY_NAME_ESC) { + onClose(); + } + }; + + const handleClickOutside = (event, ref, onClose) => { + if (ref.current && !ref.current.contains(event.target)) { + onClose(); + } + }; + + useEffect(() => { + document.addEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, close), false); + document.addEventListener('mousedown', (event) => handleClickOutside(event, ref, close)); + + return () => { + document.removeEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, close), false); + document.removeEventListener('mousedown', (event) => handleClickOutside(event, ref, close)); + } + }, []) + + return React.cloneElement(children, { ref }) +} +class Alert extends Component { componentDidMount() { - document.addEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, this.props.close), false); - document.addEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.close)); this.okRef.focus(); } - componentWillUnmount() { - document.removeEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, this.props.close), false); - document.removeEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.close)); - } - render() { const res = isFunction(this.props.message) - ? this.props.message(this.props.close) + ? this.props.message(this.props.close) : this.props.message; return (
-
-
-
-

{this.props.title ? this.props.title : 'Alert'}

- -
-
- {isString(res) &&

{res}

} - {!isString(res) && !isFunction(res) && res} - {!isString(res) && isFunction(res) && res(this.props.close)} -
-
- {this.props.linkOpt && ( - +
+
+
+

{this.props.title ? this.props.title : 'Alert'}

+ +
+
+ {isString(res) &&

{res}

} + {!isString(res) && !isFunction(res) && res} + {!isString(res) && isFunction(res) && res(this.props.close)} +
+
+ {this.props.linkOpt && ( + + {this.props.linkOpt.title} + + )} + + Close + +
-
+
); } } class Confirm extends Component { - constructor(props) { - super(props); - this.modalRef = React.createRef(); - } - componentDidMount() { - document.addEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, this.props.cancel), false); - document.addEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.cancel)); - document.body.addEventListener('keydown', this.defaultButton); - this.okRef.focus(); + document.body.addEventListener('keydown', this.defaultButton); + this.okRef.focus(); } - componentWillUnmount() { - document.removeEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, this.props.close), false); - document.removeEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.cancel)); - - } - render() { return (
-
-
-
-

{this.props.title ? this.props.title : 'Confirm'}

- -
-
-

{this.props.message}

-
-
- - + +
+
+
+

{this.props.title ? this.props.title : 'Confirm'}

+ +
+
+

{this.props.message}

+
+
+ + +
-
+
); } } class Prompt extends Component { - constructor(props) { - super(props); - this.modalRef = React.createRef(); - } - state = { text: this.props.value || '', }; componentDidMount() { - document.addEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, this.props.cancel), false); - document.addEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.cancel)); this.okRef.focus(); if (this.ref) { this.ref.focus(); } } - - componentWillUnmount() { - document.removeEventListener(KEY_EVENT_TYPE, (event) => handleEscKey(event, this.props.close), false); - document.removeEventListener('mousedown', (event) => handleClickOutside(event, this.modalRef, this.props.cancel)); - } - + render() { return (
-
-
-
-

{this.props.title ? this.props.title : 'Prompt'}

- -
-
-

{this.props.message}

- {!this.props.textarea && ( - (this.ref = r)} - onChange={(e) => this.setState({ text: e.target.value })} - /> - )} - {this.props.textarea && ( -