diff --git a/FRONTEND/src/pages/CreatePage.jsx b/FRONTEND/src/pages/CreatePage.jsx index d675e2b..c7a85ef 100644 --- a/FRONTEND/src/pages/CreatePage.jsx +++ b/FRONTEND/src/pages/CreatePage.jsx @@ -1,17 +1,71 @@ import { useProductStore } from '../store/product'; -import { Box, Button, Container, Heading, Input, useColorModeValue, useToast, VStack } from '@chakra-ui/react'; -import React, { useState } from 'react'; +import { + Box, Button, Collapse, Container, Divider, Heading, HStack, Icon, + Image, Input, Select, Text, Textarea, useColorModeValue, useToast, VStack +} from '@chakra-ui/react'; +import React, { useEffect, useRef, useState } from 'react'; +import { FaChevronDown, FaChevronUp, FaInfoCircle } from 'react-icons/fa'; +import { useNavigate } from 'react-router-dom'; const CreatePage = () => { + const { t } = useTranslation(); + const navigate = useNavigate(); const [newProduct, setNewProduct] = useState({ name: "", price: "", image: "", tags: [], }); + const [preview, setPreview] = useState(null); + const [extraImageInput, setExtraImageInput] = useState(""); + const [showExtraDetails, setShowExtraDetails] = useState(false); + const [isDirty, setIsDirty] = useState(false); + const fileInputRef = useRef(null); const toast = useToast(); - const { createProduct } = useProductStore(); + const { createProduct, isSubmitting } = useProductStore(); + + useEffect(() => { + const handleBeforeUnload = (e) => { + if (isDirty) { + e.preventDefault(); + e.returnValue = ''; + } + }; + window.addEventListener('beforeunload', handleBeforeUnload); + return () => window.removeEventListener('beforeunload', handleBeforeUnload); + }, [isDirty]); + + useEffect(() => { + const handleClick = (e) => { + const link = e.target.closest('a'); + if (link && isDirty) { + e.preventDefault(); + const confirmed = window.confirm('You have unsaved changes. Are you sure you want to leave?'); + if (confirmed) { + setIsDirty(false); + navigate(link.getAttribute('href')); + } + } + }; + document.addEventListener('click', handleClick, true); + return () => document.removeEventListener('click', handleClick, true); + }, [isDirty, navigate]); + + useEffect(() => { + const url = preview; + return () => { + if (url && url.startsWith('blob:')) URL.revokeObjectURL(url); + }; + }, [preview]); + + const handleFileChange = (e) => { + const file = e.target.files[0]; + if (!file) return; + setNewProduct({ ...newProduct, imageFile: file, image: "" }); + setPreview(URL.createObjectURL(file)); + setIsDirty(true); + }; const handleAddProduct = async () => { const { success, message } = await createProduct(newProduct); @@ -29,11 +83,23 @@ const CreatePage = () => { status: "success", isClosable: true }); + setPreview(null); + setExtraImageInput(""); + setShowExtraDetails(false); + setIsDirty(false); + if (fileInputRef.current) fileInputRef.current.value = ""; } setNewProduct({ name: "", price: "", image: "", tags: [] }); }; - const cardBg = useColorModeValue("white", "gray.800"); + const handleChange = (field, value) => { + setNewProduct((prev) => ({ ...prev, [field]: value })); + setIsDirty(true); + }; + + const borderColor = useColorModeValue("gray.200", "gray.600"); + const toggleBg = useColorModeValue("blue.50", "blue.900"); + const infoColor = useColorModeValue("gray.700", "gray.300"); return ( @@ -41,48 +107,202 @@ const CreatePage = () => { Create New Product - - setNewProduct({ ...newProduct, name: e.target.value })} - /> - setNewProduct({ ...newProduct, price: e.target.value })} - /> - setNewProduct({ ...newProduct, image: e.target.value })} - /> - - {/* ─── TAGS INPUT ────────────────────────────────────────── */} - { - const tagsArray = e.target.value - .split(',') - .map(tag => tag.trim()) - .filter(tag => tag && tag.length >= 2 && tag.length <= 30); - setNewProduct({ ...newProduct, tags: tagsArray }); - }} - /> - - + + {newProduct.images.length > 0 && ( + + {newProduct.images.map((url, idx) => ( + + {url} + + + ))} + {newProduct.images.length}/4 additional images + + )} + + + + + + + + + + + Adding extra details helps customers make informed decisions and improves product visibility. + + +