Skip to content

Commit

Permalink
Add formatTag utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamgaur99 committed Jun 4, 2024
1 parent 5ab56ce commit a9e800c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
14 changes: 14 additions & 0 deletions frontend/src/components/util/Tags.jsx
Original file line number Diff line number Diff line change
@@ -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;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 = "";
}
};
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/Admin/Components/Faq/AddFaq/AddFaq.jsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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] });
Expand Down

0 comments on commit a9e800c

Please sign in to comment.