From eee8f2f1c6b8308479574b2b12501b88046ea4e1 Mon Sep 17 00:00:00 2001 From: Balaharisankar Lakshmanaperumal <114602603+BHS-Harish@users.noreply.github.com> Date: Fri, 26 Jul 2024 23:38:37 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=80=20Refactor:=20Manage=20Broadcast?= =?UTF-8?q?=20(#1076)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor:Manage Broadcast * Edit broadcast fixed --- frontend/package.json | 1 + .../Broadcast/AddBroadcasts/AddBroadcasts.jsx | 316 +++++++++--------- .../AddBroadcasts/add-broadcasts.module.scss | 6 +- .../Broadcast/ManageBroadcasts/Card/Card.jsx | 5 +- .../ManageBroadcasts/ManageBroadcasts.jsx | 10 +- .../Component/AllBroadcasts/Edit/Edit.jsx | 291 +++++++++++++--- .../AllBroadcasts/Edit/edit.module.scss | 2 + 7 files changed, 420 insertions(+), 211 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index fd51a960..e481ae99 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -16,6 +16,7 @@ "joi-browser": "^13.4.0", "jwt-decode": "^3.1.2", "mdbreact": "^5.0.1", + "moment": "^2.30.1", "node-sass": "^4.14.1", "prop-types": "^15.7.2", "react": "^17.0.1", diff --git a/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx b/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx index 23239deb..56c81267 100644 --- a/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx +++ b/frontend/src/pages/Admin/Components/Broadcast/AddBroadcasts/AddBroadcasts.jsx @@ -1,11 +1,11 @@ -import React, { useRef, useState } from "react"; +import React, { useEffect, useRef, useState } from "react"; import SunEditor from "suneditor-react"; 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 { formatTag } from "../../../../../components/util/Tags"; +import Loader from "../../../../../components/util/Loader/index"; import { SimpleToast } from "../../../../../components/util/Toast/index"; import { postBoardcast } from "../../../../../service/Broadcast"; @@ -30,12 +30,13 @@ export function AddBroadcasts() { const [formData, setFormData] = useState(schema); const [errorObj, setErrorObj] = useState({}); const [isUploadingData, setIsUploadingData] = useState(false); + const [broadcastContent, setBroadcasetContent] = useState(""); const handleCloseToast = (event, reason) => { if (reason === "clickaway") { return; } setToast({ ...toast, toastStatus: false }); - } + }; const validationSchema = { title: Joi.string().required(), @@ -52,7 +53,7 @@ export function AddBroadcasts() { }); if (!check.error) return true; const errors = {}; - check.error.details.map(item => { + check.error.details.map((item) => { if (!errors[item.path[0]]) errors[item.path[0]] = item.message; return 0; }); @@ -60,7 +61,7 @@ export function AddBroadcasts() { return false; }; - const validateField = input => { + const validateField = (input) => { const { name, value } = input; const obj = { [name]: value }; const obj_schema = { [name]: validationSchema[name] }; @@ -69,7 +70,7 @@ export function AddBroadcasts() { return result.error ? result.error.details[0].message : null; }; - const handleChange = e => { + const handleChange = (e) => { const { currentTarget: input } = e; const errors = { ...errorObj }; const errorMessage = validateField(input); @@ -83,7 +84,7 @@ export function AddBroadcasts() { const addTag = () => { const tag = formatTag(tagRef.current.value.trim(), "Hyphens"); if (tag) { - setTags(prevTags => [...prevTags, tag]); + setTags((prevTags) => [...prevTags, tag]); setFormData({ ...formData, tags: [...formData.tags, tag] }); tagRef.current.value = ""; } @@ -91,185 +92,194 @@ export function AddBroadcasts() { const addImageUrl = () => { const url = imageRef.current.value; if (url.trim()) { - setImageUrls(prevUrl => [...prevUrl, url.trim()]); - setFormData({ ...formData, imageUrl: [...formData.imageUrl, url.trim()] }); + setImageUrls((prevUrl) => [...prevUrl, url.trim()]); + setFormData({ + ...formData, + imageUrl: [...formData.imageUrl, url.trim()], + }); imageRef.current.value = ""; } - } + }; - const removeTag = tag => { - setTags(tags.filter(t => t !== tag)); - setFormData({ ...formData, tags: formData.tags.filter(t => t !== tag) }); + const removeTag = (tag) => { + setTags(tags.filter((t) => t !== tag)); + setFormData({ ...formData, tags: formData.tags.filter((t) => t !== tag) }); + }; + const removeImageUrl = (url) => { + setImageUrls(imageUrls.filter((u) => u !== url)); + setFormData({ + ...formData, + imageUrl: formData.imageUrl.filter((u) => u !== url), + }); }; - const removeImageUrl = url => { - setImageUrls(imageUrls.filter(u => u !== url)); - setFormData({ ...formData, imageUrl: formData.imageUrl.filter(u => u !== url) }); - } async function uploadData(formData) { const response = await postBoardcast(formData, setToast, toast); - if(response) { - const schema = { - title: "", - content: "", - tags: [], - expiresOn: "dd-mm-yyyy", - imageUrl: [], - link: "", - }; - setTags([]) - setImageUrls([]) - setFormData(schema) - } + if (response) { + const schema = { + title: "", + content: "", + tags: [], + expiresOn: "dd-mm-yyyy", + imageUrl: [], + link: "", + }; + setTags([]); + setImageUrls([]); + setFormData(schema); + } } const onSubmit = async (e) => { e.preventDefault(); + setFormData({ ...formData, content: broadcastContent }); if (isFormValid()) { // TODO: send data to server - setIsUploadingData(true) + setIsUploadingData(true); await uploadData(formData); - setIsUploadingData(false) + setIsUploadingData(false); } }; - const onContentChange = content => { + const onContentChange = (content) => { let value = ""; if (content !== "
" && content !== "