import { Container, Row, Col, Card, Form, Image, Alert, Button } from "react-bootstrap"; import AdminTemplate from "../../templates/AdminTemplate"; import { useSearchParams } from 'react-router-dom'; import axios from "axios"; import DataStorage from "../../organisms/DataStorage"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; export default function AdminBranchEditPage() { const [error, setError] = useState(""); const [searchParams] = useSearchParams(); const storeId = searchParams.get('id') || null; const [storeDetail, setStoreItem] = useState({ name:'', location:'', image_url:'' }); const [initialUrl, setInitialUrl] = useState(""); const [initialTitle, setInitialTitle] = useState(""); const [initialDesc, setInitialDesc] = useState(""); // const [loading, setLoading] = useState(true); const [isChanged, setChanged] = useState(false); const navigate = useNavigate(); useEffect(() => { if (!DataStorage.get('isLoggedInAdmin')) { navigate('/admin-login'); } }, [navigate]); const checkChange = () => { if (storeDetail.name.trim() !== initialTitle.trim() || storeDetail.location.trim() !== initialDesc.trim() || storeDetail.image_url.trim() !== initialUrl.trim()) { setChanged(true); } else { setChanged(false); } } const handleChange = (e) => { const { name, value } = e.target; setStoreItem({ ...storeDetail, [name]: value }); checkChange(); }; const handleAvatarUrlChange = (e) => { const url = e.target.value; setStoreItem((prevData) => ({ ...prevData, image_url: url // Cập nhật URL ảnh })); setChanged(true); }; useEffect(() => { if (storeId) { axios.get(process.env.REACT_APP_API_URL + `/branchs/${storeId}`) .then((response) => { setStoreItem(response.data); // setLoading(false); setInitialTitle(response.data.name); setInitialDesc(response.data.location); setInitialUrl(response.data.image_url); }) .catch((error) => { setError(JSON.stringify(error)); }) } }, [storeId]); const handleSubmit = () => { const submit_data = { 'name': storeDetail.name, 'location': storeDetail.location, 'image_url': storeDetail.image_url } if (storeId) { axios.patch(process.env.REACT_APP_API_URL + `/branchs/${storeId}`, submit_data) .then((response) => { // setFeedItem(response.data); // // setLoading(false); // setInitialDesc(response.data.description); // setInitialTitle(response.data.title); // setChanged(false); navigate('/admin-branchs-list'); // window.location.reload(); }) .catch((error) => { setError(JSON.stringify(error)); }) } else { axios.post(process.env.REACT_APP_API_URL + `/branchs`, submit_data) .then((response) => { navigate('/admin-branchs-list'); }) .catch((error) => { setError(JSON.stringify(error)); }) } } return ( {/* */} {storeId ? 'Chỉnh sửa thông tin chi nhánh' : 'Tạo chi nhánh'}
{error && {error}} {/* Hiển thị ảnh từ URL */} Ảnh mô tả URL ảnh mô tả Tên chi nhánh Địa chỉ
{/* */}
) } /> ) }