From 7170d84095b2e9cd9d00392556681d361d4b9db2 Mon Sep 17 00:00:00 2001 From: Piv94165 <106757110+Piv94165@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:56:49 +0200 Subject: [PATCH] fix(frontend): Replace white spaces in branch name proposition and allow dashes in owner name (#466) * fix(frontend): Replace white spaces in branch name proposition * fix(frontend): Allow dashes in owner names to support GitHub usernames * refactor(frontend): remove useless comments * fix checks tests * feat(frontend): readable date in branch name proposition * lint * feat(frontend): Implement constant date formatting * lint --------- Co-authored-by: alice.juan --- .../src/pages/startproject/index.tsx | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/taxonomy-editor-frontend/src/pages/startproject/index.tsx b/taxonomy-editor-frontend/src/pages/startproject/index.tsx index 12329132..d32b0e80 100644 --- a/taxonomy-editor-frontend/src/pages/startproject/index.tsx +++ b/taxonomy-editor-frontend/src/pages/startproject/index.tsx @@ -22,6 +22,18 @@ import { createBaseURL, toSnakeCase } from "@/utils"; const branchNameRegEx = /[^a-z0-9_]+/; +function formatDate(date) { + const map = { + mm: ("0" + (date.getMonth() + 1)).slice(-2), + dd: ("0" + date.getDate()).slice(-2), + yy: ("0" + date.getFullYear()).slice(-2), + HH: ("0" + date.getHours()).slice(-2), + MinMin: ("0" + date.getMinutes()).slice(-2), + }; + + return `${map.mm}${map.dd}${map.yy}_${map.HH}_${map.MinMin}`; +} + export const StartProject = () => { const [ownerName, setOwnerName] = useState(""); const [taxonomyName, setTaxonomyName] = useState(""); @@ -32,9 +44,9 @@ export const StartProject = () => { const findDefaultBranchName = useCallback(() => { if (taxonomyName === "" || ownerName === "") return ""; - return `${toSnakeCase(taxonomyName.toLowerCase())}_${ownerName - .replace(" ", "") - .toLowerCase()}_${Math.floor(Date.now() / 1000)}`; + return `${formatDate(new Date())}_${taxonomyName}_${ownerName}` + .replace(/[\s-]+/g, "_") + .toLowerCase(); }, [ownerName, taxonomyName]); const [branchName, setBranchName] = useState(findDefaultBranchName()); @@ -78,7 +90,7 @@ export const StartProject = () => { const isOwnerNameInvalid = (name: string) => { if (name === "") return false; - const pattern = /^[a-zA-Z0-9 _]+$/; + const pattern = /^[a-zA-Z0-9 _-]+$/; if (!pattern.test(name)) { return true; }