From a9e800cf2ee1326f19eefcb89c9a4038c932fbed Mon Sep 17 00:00:00 2001 From: Shivam Gaur Date: Tue, 4 Jun 2024 19:18:31 +0530 Subject: [PATCH] Add formatTag utility function --- frontend/src/components/util/Tags.jsx | 14 ++++++++++++++ .../Broadcast/AddBroadcasts/AddBroadcasts.jsx | 9 +++++---- .../pages/Admin/Components/Faq/AddFaq/AddFaq.jsx | 3 ++- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 frontend/src/components/util/Tags.jsx diff --git a/frontend/src/components/util/Tags.jsx b/frontend/src/components/util/Tags.jsx new file mode 100644 index 00000000..3bd500a7 --- /dev/null +++ b/frontend/src/components/util/Tags.jsx @@ -0,0 +1,14 @@ +export const formatTag = (tag, format) => { + switch (format) { + case "CamelCase": + return tag.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index === 0 ? word.toLowerCase() : word.toUpperCase()).replace(/\s+/g, ''); + case "Underscores": + return tag.replace(/\s+/g, '_').toLowerCase(); + case "Hyphens": + return tag.replace(/\s+/g, '-').toLowerCase(); + case "Concatenation": + return tag.replace(/\s+/g, '').toLowerCase(); + default: + return tag; + } + }; \ No newline at end of file diff --git a/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx b/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx index ce112a93..23239deb 100644 --- a/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx +++ b/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx @@ -4,6 +4,7 @@ import "suneditor/dist/css/suneditor.min.css"; import styles from "./add-broadcasts.module.scss"; import Joi from "joi-browser"; import { Button2 } from "../../../../../components/util/Button/index"; +import { formatTag } from '../../../../../components/util/Tags'; import Loader from "../../../../../components/util/Loader/index"; import { SimpleToast } from "../../../../../components/util/Toast/index"; import { postBoardcast } from "../../../../../service/Broadcast"; @@ -80,10 +81,10 @@ export function AddBroadcasts() { }; const addTag = () => { - const tag = tagRef.current.value; - if (tag.trim()) { - setTags(prevTags => [...prevTags, tag.trim()]); - setFormData({ ...formData, tags: [...formData.tags, tag.trim()] }); + const tag = formatTag(tagRef.current.value.trim(), "Hyphens"); + if (tag) { + setTags(prevTags => [...prevTags, tag]); + setFormData({ ...formData, tags: [...formData.tags, tag] }); tagRef.current.value = ""; } }; diff --git a/frontend/src/pages/Admin/Components/Faq/AddFaq/AddFaq.jsx b/frontend/src/pages/Admin/Components/Faq/AddFaq/AddFaq.jsx index edf536f7..807807db 100644 --- a/frontend/src/pages/Admin/Components/Faq/AddFaq/AddFaq.jsx +++ b/frontend/src/pages/Admin/Components/Faq/AddFaq/AddFaq.jsx @@ -1,6 +1,7 @@ import React, { useRef, useState } from "react"; import styles from "./add-faq.module.scss"; import { Button2 } from "../../../../../components/util/Button/index"; +import { formatTag } from '../../../../../components/util/Tags'; import { SimpleToast } from "../../../../../components/util/Toast/Toast"; import { postFaq } from "../../../../../service/Faq"; @@ -60,7 +61,7 @@ export function AddFaq() { }; const addTag = () => { - const tag = tagRef.current.value.trim(); + const tag = formatTag(tagRef.current.value.trim(), "Hyphens"); if (tag && !tags.includes(tag)) { setTags([...tags, tag]); setFormData({ ...formData, tags: [...tags, tag] });