import React, { useEffect, useState } from 'react' import { CCard, CCardBody, CCardHeader, CCol, CRow, CForm, CFormLabel, CFormInput } from '@coreui/react' import { TextField, Dialog, Snackbar, Alert, Fade, DialogTitle, DialogContent, DialogActions, TablePagination, TableContainer, Box, Table, TableHead, TableRow, TableCell, TableBody, Button } from '@mui/material'; import Paper from '@mui/material/Paper'; import VisibilityIcon from '@mui/icons-material/Visibility'; import EditIcon from '@mui/icons-material/Edit'; import DeleteIcon from '@mui/icons-material/Delete'; import axios from 'axios'; export default function Brand() { const [brands, setBrands] = useState([]) const [page, setPage] = useState(0); // Current page index const [rowsPerPage, setRowsPerPage] = useState(5); // Number of rows per page const [filterText, setFilterText] = useState(""); const [deleteId, setDeleteId] = useState(null) const [snackbarOpen, setSnackbarOpen] = useState(false); const [snackbarMessage, setSnackbarMessage] = useState(''); const [snackbarSeverity, setSnackbarSeverity] = useState('success'); // 'success' or 'warning' const [confirmDeleteOpen, setConfirmDeleteOpen] = useState(false) const [openAddEdit, setOpenAddEdit] = useState(false) const [openDetails, setOpenDetails] = useState(false) const [currentBrand, setCurrentBrand] = useState({ title: '', summary: '', content: '', created_at: new Date().toISOString(), // Set current datetime updated_at: new Date().toISOString() }) const [formMode, setFormMode] = useState('') // Display the initial list useEffect(() => { axios.get('http://localhost:8080/api/v1/brands').then((response) => { setBrands(response.data) }) .catch((error) => { console.error("There was an error fetching the Brands:", error); }); }, []) const handleConfirmDeleteOpen = (id) => { setDeleteId(id) setConfirmDeleteOpen(true) } const handleConfirmDeleteClose = (id) => { setDeleteId(null) setConfirmDeleteOpen(false) } const handleSnackbarClose = () => { setSnackbarOpen(false); }; const handleDeleteBrand = async (id) => { try { setSnackbarOpen(true); await axios.delete(`http://localhost:8080/api/v1/brand/${id}`); setBrands((brands) => brands.filter((brand) => brand.id !== id)); setSnackbarMessage('The selected brand has been deleted successfully!'); setSnackbarSeverity('success'); handleConfirmDeleteClose(); } catch (error) { console.error('Error occurred deleting the brand: ', error); setSnackbarMessage('There was an error deleting the brand! Please try again.'); setSnackbarSeverity('warning'); setSnackbarOpen(true); } }; const clearForm = () => { setCurrentBrand({ title: '', summary: '', content: '', created_at: new Date().toISOString() // Set current datetime }) } const handleOpenAddEdit = () => { setFormMode('new') clearForm() setOpenAddEdit(true) } const handleCloseAddEdit = () => { setOpenAddEdit(false) } const handleCloseDetails = () => { setOpenDetails(false) } const handleOpenEdit = (brand) => { setCurrentBrand(brand); setFormMode('edit'); setOpenAddEdit(true); }; const handleOpenDetails = (brand) => { setCurrentBrand(brand); setOpenDetails(true); }; const handleChangeAdd = (e) => { setCurrentBrand({...currentBrand, [e.target.id]: e.target.value}); } const handleAddBrand = async () => { try { setSnackbarOpen(true) const response = await axios.post('http://localhost:8080/api/v1/brands', { ...currentBrand, createdAt: new Date().toISOString() // Set current datetime }) setBrands([...brands, response.data]) clearForm(); setSnackbarMessage('Brand was added successfully!'); setSnackbarSeverity('success'); handleCloseAddEdit(); }catch(error){ console.log('There was an error adding the brand!', error) setSnackbarMessage('There was an error deleting the brand! Please try again.'); setSnackbarSeverity('warning'); setSnackbarOpen(true); } } const handleEditBrand = async () => { setSnackbarOpen(true) try { const response = await axios.put(`http://localhost:8080/api/v1/brand/${currentBrand.id}`, { ...currentBrand, updatedAt: new Date().toISOString(), // Set current datetime }) setBrands(brands.map(brand => brand.id === currentBrand.id ? response.data : brand )); clearForm(); setSnackbarMessage('Brand was updated successfully!'); setSnackbarSeverity('success'); handleCloseAddEdit(); }catch(error){ console.log('There was an error adding the Brand!', error) setSnackbarMessage('There was an error updating the brand! Please try again.'); setSnackbarSeverity('warning'); } } // Handle page change const handleChangePage = (event, newPage) => { setPage(newPage); }; // Handle rows per page change const handleChangeRowsPerPage = (event) => { setRowsPerPage(parseInt(event.target.value, 10)); setPage(0); // Reset to the first page whenever rows per page changes }; const handleFilterChange = (event) => { setFilterText(event.target.value); setPage(0); // Reset to the first page when filtering }; // Ensure brands is an array before filtering const filteredBrands = brands.filter(brand => brand.title.toLowerCase().includes(filterText.toLowerCase()) || brand.summary.toLowerCase().includes(filterText.toLowerCase()) ); return (