From ea0fc471c7f97f029cc044dba0c0b0db6b7f8798 Mon Sep 17 00:00:00 2001 From: Sam Park Date: Mon, 17 Jun 2024 16:21:43 -0400 Subject: [PATCH 01/13] added regex checking (alphanumeric, _, -) for updating project name and tag for metadata modal. added conditional helper text that appears if user enters invalid character. --- .../components/forms/edit-project-meta.tsx | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/web/src/components/forms/edit-project-meta.tsx b/web/src/components/forms/edit-project-meta.tsx index a087596e..f0bbd512 100644 --- a/web/src/components/forms/edit-project-meta.tsx +++ b/web/src/components/forms/edit-project-meta.tsx @@ -1,4 +1,4 @@ -import { FC, useEffect } from 'react'; +import { FC, useEffect, useState, setState } from 'react'; import { Controller, useForm } from 'react-hook-form'; import toast from 'react-hot-toast'; @@ -119,6 +119,20 @@ export const ProjectMetaEditForm: FC = ({ } }, [newPop]); + const [checkProjectNameText, setCheckProjectNameText] = useState(false); + const [checkProjectTagText, setCheckProjectTagText] = useState(false); + const checkChar = (e) => { + if (!/[0-9a-zA-Z_-]/.test(e.key)) { + e.preventDefault(); + if (e.target.id === 'project-name') { + setCheckProjectNameText(true); + } + if (e.target.id === 'project-tag') { + setCheckProjectTagText(true); + } + } + }; + return (
@@ -150,7 +164,9 @@ export const ProjectMetaEditForm: FC = ({ className="form-control" id="project-name" aria-describedby="pep-name-help" + onKeyDown={(e)=>checkChar(e)} /> + { checkProjectNameText ?

Project Name must contain only alphanumeric characters, '-', or '_'.

: null }
@@ -201,6 +215,7 @@ export const ProjectMetaEditForm: FC = ({ id="project-tag" aria-describedby="pep-name-help" onKeyDown={(e)=>checkChar(e)} + onPaste={(e)=>checkPaste(e)} /> { checkProjectTagText ?

Project Tag must contain only alphanumeric characters, '-', or '_'.

: null } From cfb32cde36a54cf9496f397dbde5aac032a18c6a Mon Sep 17 00:00:00 2001 From: Sam Park Date: Tue, 18 Jun 2024 12:56:30 -0400 Subject: [PATCH 03/13] switch to useEffect() --- .../components/forms/edit-project-meta.tsx | 50 +++++++------------ 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/web/src/components/forms/edit-project-meta.tsx b/web/src/components/forms/edit-project-meta.tsx index c158fb69..2a5a0d46 100644 --- a/web/src/components/forms/edit-project-meta.tsx +++ b/web/src/components/forms/edit-project-meta.tsx @@ -119,32 +119,24 @@ export const ProjectMetaEditForm: FC = ({ } }, [newPop]); - const [checkProjectNameText, setCheckProjectNameText] = useState(false); - const [checkProjectTagText, setCheckProjectTagText] = useState(false); - const checkChar = (e) => { - console.log(e.key) - if (!/[0-9a-zA-Z_-]/.test(e.key)) { - e.preventDefault(); - if (e.target.id === 'project-name') { - setCheckProjectNameText(true); - } - if (e.target.id === 'project-tag') { - setCheckProjectTagText(true); - } + const [badName, setBadName] = useState(false); + const [badTag, setBadTag] = useState(false); + + useEffect(() => { + if (/[^0-9a-zA-Z_-]/.test(newName)) { + setBadName(true); + } else { + setBadName(false); } - }; - const checkPaste = (e) => { - console.log(e.clipboardData.getData('text')) - if (/[^0-9a-zA-Z_-]/.test(e.clipboardData.getData('text'))) { - e.preventDefault(); - if (e.target.id === 'project-name') { - setCheckProjectNameText(true); - } - if (e.target.id === 'project-tag') { - setCheckProjectTagText(true); - } + }, [newName]); + + useEffect(() => { + if (/[^0-9a-zA-Z_-]/.test(newTag)) { + setBadTag(true); + } else { + setBadTag(false); } - }; + }, [newTag]); return ( @@ -177,10 +169,8 @@ export const ProjectMetaEditForm: FC = ({ className="form-control" id="project-name" aria-describedby="pep-name-help" - onKeyDown={(e)=>checkChar(e)} - onPaste={(e)=>checkPaste(e)} /> - { checkProjectNameText ?

Project Name must contain only alphanumeric characters, '-', or '_'.

: null } + { badName ?

Project Name must contain only alphanumeric characters, '-', or '_'.

: null }