From 5045eafb8eb0d3d22ee27e49bc3e0345ad80467c Mon Sep 17 00:00:00 2001 From: Shemy Gan Date: Fri, 13 Dec 2024 15:59:54 -0500 Subject: [PATCH] - useContext from parent page for form fields --- .../TabPanels/Status/ContentPanel.jsx | 42 ++--------- .../TabPanels/Status/GeneralSettingsPanel.jsx | 63 ++++------------ .../src/Pages/Status/CreateStatus/index.jsx | 71 ++++++++++++++++--- 3 files changed, 80 insertions(+), 96 deletions(-) diff --git a/Client/src/Components/TabPanels/Status/ContentPanel.jsx b/Client/src/Components/TabPanels/Status/ContentPanel.jsx index 9607f7c33..26d74658a 100644 --- a/Client/src/Components/TabPanels/Status/ContentPanel.jsx +++ b/Client/src/Components/TabPanels/Status/ContentPanel.jsx @@ -5,23 +5,13 @@ import { useTheme } from "@emotion/react"; import TabPanel from "@mui/lab/TabPanel"; import { publicPageGeneralSettingsValidation } from "../../../Validation/validation"; import { buildErrors } from "../../../Validation/error"; -import { hasValidationErrors } from "../../../Validation/error"; import Card from "./Card"; import update from "immutability-helper"; import Checkbox from "../../Inputs/Checkbox"; -const ContentPanel = () => { +const ContentPanel = ({errors, setErrors}) => { const theme = useTheme(); - const [errors, setErrors] = useState({}); - // Local state for form data, errors, and file handling - const [localData, setLocalData] = useState({ - monitors: [], - showUptimePercentage: false, - showBarcode: false - - }); - - const [cards, setCards] = useState(localData.monitors); + const [cards, setCards] = useState(form.monitors); // Clears specific error from errors state const clearError = (err) => { @@ -50,7 +40,7 @@ const ContentPanel = () => { const handleChange = (event) => { event.preventDefault(); const { value, id } = event.target; - setLocalData((prev) => ({ + setForm((prev) => ({ ...prev, [id]: value, })); @@ -66,13 +56,6 @@ const ContentPanel = () => { setCards(update(cards, { $splice: [[idx, 1, x]] })); } else setCards(update(cards, { $push: [{ id: id, url: value }] })); } - - const handleSubmit = () => { - if (hasValidationErrors(localData, publicPageGeneralSettingsValidation, setErrors)) { - return; - } - }; - const handleAddNew = () => { setCards([...cards, { id: "" + Math.random() }]); }; @@ -190,34 +173,19 @@ const ContentPanel = () => { - - - - ); diff --git a/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx b/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx index 230822447..bd18c23bb 100644 --- a/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx +++ b/Client/src/Components/TabPanels/Status/GeneralSettingsPanel.jsx @@ -1,4 +1,4 @@ -import { useState, useRef } from "react"; +import { useState, useRef, useContext } from "react"; import { Button, Box, Stack, Typography } from "@mui/material"; import { ConfigBox } from "../../../Pages/Settings/styled"; import Checkbox from "../../Inputs/Checkbox"; @@ -18,23 +18,15 @@ import ProgressUpload from "../../ProgressBars"; import ImageIcon from "@mui/icons-material/Image"; import { HttpAdornment } from "../../Inputs/TextInput/Adornments"; import { hasValidationErrors } from "../../../Validation/error"; +import { StatusFormContext } from "../../../Pages/Status/TabContext"; -const GeneralSettingsPanel = () => { - const theme = useTheme(); - const [errors, setErrors] = useState({}); +const GeneralSettingsPanel = ({errors, setErrors}) => { + const theme = useTheme(); const [logo, setLogo] = useState(); + const { form, setForm } = useContext(StatusFormContext); const [progress, setProgress] = useState({ value: 0, isLoading: false }); const intervalRef = useRef(null); - // Local state for form data, errors, and file handling - const [localData, setLocalData] = useState({ - companyName: "", - url: "", - timezone: "America/Toronto", - color: "#4169E1", - //which fields matching below? - publish: false, - logo: null, - }); + // Clears specific error from errors state const clearError = (err) => { setErrors((prev) => { @@ -46,7 +38,7 @@ const GeneralSettingsPanel = () => { const removeLogo = () => { errors["logo"] && clearError("logo"); setLogo({}); - setLocalData((prev) => ({ + setForm((prev) => ({ ...prev, logo: logo?.src, })); @@ -58,27 +50,12 @@ const GeneralSettingsPanel = () => { const handleChange = (event) => { event.preventDefault(); const { value, id } = event.target; - setLocalData((prev) => ({ + setForm((prev) => ({ ...prev, [id]: value, })); }; - const handleSubmit = () => { - //validate rest of the form - delete localData.logo; - if (hasValidationErrors(localData, publicPageGeneralSettingsValidation, setErrors)) { - return; - } - //validate image field - let error = validateField( - { type: logo?.type ?? null, size: logo?.size ?? null }, - logoImageValidation - ); - if (error) return; - localData.logo = { ...logo, size: formatBytes(logo?.size) }; - }; - const handleBlur = (event) => { event.preventDefault(); const { value, id } = event.target; @@ -159,7 +136,7 @@ const GeneralSettingsPanel = () => { @@ -180,7 +157,7 @@ const GeneralSettingsPanel = () => { id="companyName" type="text" label="Company name" - value={localData.companyName} + value={form.companyName} onChange={handleChange} onBlur={handleBlur} helperText={errors["companyName"]} @@ -190,7 +167,7 @@ const GeneralSettingsPanel = () => { id="url" type="url" label="SubURL" - value={localData.url} + value={form.url} startAdornment={} onChange={handleChange} onBlur={handleBlur} @@ -212,7 +189,7 @@ const GeneralSettingsPanel = () => {