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 !== "