// WelcomePageEditor.js import React, { useEffect, useState } from "react"; import WelcomePage from "./WelcomePage"; import { saveWelcomePageConfig } from "../helpers/cafeHelpers"; // Import the API function import Switch from "react-switch"; // Import react-switch import "./WelcomePageEditor.css"; import { getImageUrl } from "../helpers/itemHelper.js"; const WelcomePageEditor = ({ cafeId, welcomePageConfig }) => { const [image, setImage] = useState(""); const [welcomingText, setWelcomingText] = useState("Enjoy your coffee!"); const [backgroundColor, setBackgroundColor] = useState("#ffffff"); const [textColor, setTextColor] = useState("#000000"); const [isWelcomePageActive, setIsWelcomePageActive] = useState(false); // State for the switch const [loading, setLoading] = useState(false); // Loading state const [isFullscreen, setIsFullscreen] = useState(false); // Load existing welcome page configuration when component mounts useEffect(() => { if (welcomePageConfig) { const config = JSON.parse(welcomePageConfig); setImage(getImageUrl(config.image) || ""); // Load image path setWelcomingText(config.welcomingText || "Enjoy your coffee!"); setBackgroundColor(config.backgroundColor || "#ffffff"); setTextColor(config.textColor || "#000000"); setIsWelcomePageActive(config.isWelcomePageActive === "true"); // Convert string to boolean } console.log(welcomePageConfig); }, [welcomePageConfig]); const handleImageChange = (e) => { const file = e.target.files[0]; if (file) { const reader = new FileReader(); reader.onloadend = () => { setImage(reader.result); // Store the base64 image }; reader.readAsDataURL(file); } }; const handleTextChange = (e) => { setWelcomingText(e.target.value); }; const handleColorChange = (e) => { setBackgroundColor(e.target.value); }; const handleTextColorChange = (e) => { setTextColor(e.target.value); }; const handleSave = async () => { setLoading(true); const details = { image, welcomingText, backgroundColor, textColor, isWelcomePageActive, // Include the isWelcomePageActive state }; try { const result = await saveWelcomePageConfig(cafeId, details); console.log("Configuration saved:", result); } catch (error) { console.error("Error saving configuration:", error); } finally { setLoading(false); } }; return (