From cd0ad56afd3cb7ac40d4a813f1eac4c9dbeda965 Mon Sep 17 00:00:00 2001 From: Anthony Geourjon <anthony.geourjon@tetras-libre.fr> Date: Tue, 6 Feb 2024 10:52:33 +0100 Subject: [PATCH] Simplify AnnotationFormManifestNetwork.js component --- .../AnnotationFormManifestNetwork.js | 26 +++---------------- src/utils.js | 12 +++++++++ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/annotationForm/AnnotationFormManifestNetwork.js b/src/annotationForm/AnnotationFormManifestNetwork.js index 9ece368..03afe40 100644 --- a/src/annotationForm/AnnotationFormManifestNetwork.js +++ b/src/annotationForm/AnnotationFormManifestNetwork.js @@ -3,25 +3,10 @@ import { Grid, Paper, TextField, Typography, Button, Link, } from '@mui/material'; import PropTypes from 'prop-types'; +import { isValidUrl } from '../utils'; /** Form part for edit annotation content and body */ function AnnotationFormNetwork({ manifestNetwork, updateManifestNetwork }) { - const isValidUrl = (string) => { - if (string === '') { - return true; - } - try { - new URL(string); - return true; - } catch (_) { - return false; - } - }; - - const onChangeManifestNetworkInput = (event) => { - updateManifestNetwork(event.target.value.trim()); - }; - return ( <Paper style={{ padding: '5px' }}> <Typography variant="overline"> @@ -30,22 +15,19 @@ function AnnotationFormNetwork({ manifestNetwork, updateManifestNetwork }) { <Grid> <TextField value={manifestNetwork} - onChange={onChangeManifestNetworkInput} + onChange={(event) => updateManifestNetwork(event.target.value.trim())} label="Manifest URL" type="url" /> { - isValidUrl(manifestNetwork) && ( + isValidUrl(manifestNetwork) ? ( <Link href={manifestNetwork} target="_blank" > {manifestNetwork} </Link> - ) - } - { - !isValidUrl(manifestNetwork) && ( + ) : ( <Typography variant="caption"> Not a valid URL </Typography> diff --git a/src/utils.js b/src/utils.js index b894d0b..f30b3da 100644 --- a/src/utils.js +++ b/src/utils.js @@ -26,3 +26,15 @@ export function secondsToHMSarray(secs) { seconds: secs % 60, }; } + +export const isValidUrl = (string) => { + if (string === '') { + return true; + } + try { + new URL(string); + return true; + } catch (_) { + return false; + } +}; -- GitLab