From d844078ed7ff95e0d41b42ec95e3f4a291711cd6 Mon Sep 17 00:00:00 2001 From: Shemy Gan Date: Tue, 24 Dec 2024 14:46:54 -0500 Subject: [PATCH] - Update validation - Switch to active tab when there are errors on it upon submission - Add createStatusPage function - Updated JSDOC for functions --- .../TabPanels/Status/ContentPanel.jsx | 135 ++++++++---------- .../TabPanels/Status/GeneralSettingsPanel.jsx | 3 +- .../src/Pages/Status/CreateStatus/index.jsx | 40 +++--- Client/src/Utils/NetworkService.js | 10 ++ Client/src/Validation/error.js | 26 +++- Client/src/Validation/validation.js | 18 +-- 6 files changed, 128 insertions(+), 104 deletions(-) diff --git a/Client/src/Components/TabPanels/Status/ContentPanel.jsx b/Client/src/Components/TabPanels/Status/ContentPanel.jsx index 0563a5022..3d5fef0e3 100644 --- a/Client/src/Components/TabPanels/Status/ContentPanel.jsx +++ b/Client/src/Components/TabPanels/Status/ContentPanel.jsx @@ -3,16 +3,13 @@ import { Button, Box, Stack, Typography } from "@mui/material"; import ConfigBox from "../../../Components/ConfigBox"; import { useTheme } from "@emotion/react"; import TabPanel from "@mui/lab/TabPanel"; -import { publicPageGeneralSettingsValidation } from "../../../Validation/validation"; -import { buildErrors } from "../../../Validation/error"; import Card from "./Card"; import update from "immutability-helper"; -import Checkbox from "../../Inputs/Checkbox"; import { StatusFormContext } from "../../../Pages/Status/CreateStatusContext"; const ContentPanel = () => { const theme = useTheme(); - const { form, setForm, errors, setErrors } = useContext(StatusFormContext); + const { form, setForm, errors, setErrors } = useContext(StatusFormContext); const [cards, setCards] = useState(form.monitors); const moveCard = useCallback( @@ -30,44 +27,31 @@ const ContentPanel = () => { [cards] ); - const handleChange = (event) => { - event.preventDefault(); - const { value, id } = event.target; - setForm((prev) => ({ - ...prev, - [id]: value, - })); - }; - const handleCardChange = (event) => { event.preventDefault(); const { value, id } = event.target; let idx = cards.findIndex((a) => a.id == id); + let newCards; if (idx >= 0) { let x = JSON.parse(JSON.stringify(cards[idx])); x.url = value; - setCards(update(cards, { $splice: [[idx, 1, x]] })); - } else setCards(update(cards, { $push: [{ id: id, url: value }] })); - } + newCards = update(cards, { $splice: [[idx, 1, x]] }); + setCards(newCards); + } else { + newCards = update(cards, { $push: [{ id: id, url: value }] }); + setCards(newCards); + } + setForm({ ...form, monitors: newCards }); + }; const handleAddNew = () => { - setCards([...cards, { id: "" + Math.random() }]); + const newCards = [...cards, { id: "" + Math.random() }]; + setCards(newCards); + setForm({ ...form, monitors: newCards }); }; const removeCard = (id) => { - setCards(cards.filter((c) => c?.id != id)); - }; - const handleBlur = (event) => { - event.preventDefault(); - const { value, id } = event.target; - const { error } = publicPageGeneralSettingsValidation.validate( - { [id]: value }, - { - abortEarly: false, - } - ); - - setErrors((prev) => { - return buildErrors(prev, id, error); - }); + const newCards = cards.filter((c) => c?.id != id); + setCards(newCards); + setForm({ ...form, monitors: newCards }); }; return ( @@ -101,7 +85,6 @@ const ContentPanel = () => { className="status-contents-server-list" sx={{ margin: theme.spacing(20), - //padding: theme.spacing(20), border: "solid", borderRadius: theme.shape.borderRadius, borderColor: theme.palette.border.light, @@ -113,48 +96,54 @@ const ContentPanel = () => { }, }} > - - + - - {" "} - Servers list{" "} - - + {" "} + Servers list{" "} + + + + {cards.length > 0 && ( + + {cards.map((card, idx) => ( + + ))} - {cards.length > 0 && ( - - {cards.map((card, idx) => ( - - ))} - - )} - + )} + {errors["monitors"] && ( + + {errors["monitors"]} + + )} + @@ -162,7 +151,7 @@ const ContentPanel = () => { Show more details on the status page - { onChange={handleChange} onBlur={handleBlur} /> - + */} diff --git a/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx b/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx index 994deffef..95478477b 100644 --- a/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx +++ b/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx @@ -64,7 +64,6 @@ const GeneralSettingsPanel = () => { abortEarly: false, } ); - setErrors((prev) => { return buildErrors(prev, id, error); }); @@ -186,7 +185,7 @@ const GeneralSettingsPanel = () => {