import React, { useState } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import { Container, Typography, Card, CardMedia, CardContent, Button, Checkbox, FormControlLabel, TextField, Dialog, DialogTitle, DialogContent, DialogActions, Paper } from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import DeleteIcon from '@material-ui/icons/Delete'; import GetAppIcon from '@material-ui/icons/GetApp'; import ShareIcon from '@material-ui/icons/Share'; import EditIcon from '@material-ui/icons/Edit'; import { removePoster } from '../services/action'; import axios from "axios"; const useStyles = makeStyles((theme) => ({ root: { marginTop: theme.spacing(4), marginBottom: theme.spacing(8), display: 'flex', }, posterList: { flex: 2, marginRight: theme.spacing(2), maxHeight: 'calc(100vh - 200px)', overflowY: 'auto', }, actionPanel: { flex: 1, position: 'sticky', top: theme.spacing(4), height: 'fit-content', }, card: { display: 'flex', marginBottom: theme.spacing(2), backgroundColor: '#2c3e50', }, cardMedia: { width: 100, }, cardContent: { flex: '1 0 auto', }, actions: { display: 'flex', flexDirection: 'column', gap: theme.spacing(2), }, slider: { width: '100%', marginTop: theme.spacing(2), }, button: { width: '100%', }, typography: { color: '#ecf0f1', }, formControl: { marginBottom: theme.spacing(2), }, checkbox: { color: '#3498db', }, paper: { padding: theme.spacing(3), backgroundColor: '#34495e', }, })); const Cart = () => { const classes = useStyles(); const dispatch = useDispatch(); const selectedPosters = useSelector((state) => { const selections = state.posterSelections; return Object.entries(selections).flatMap(([movieId, posters]) => posters.map(posterId => ({ movieId, posterId })) ); }); const [selectedForDownload, setSelectedForDownload] = useState(selectedPosters.map(() => true)); const [downloadFormat, setDownloadFormat] = useState('zip'); const [renameDialogOpen, setRenameDialogOpen] = useState(false); const [currentPosterIndex, setCurrentPosterIndex] = useState(null); const [newPosterName, setNewPosterName] = useState(''); const handleRemovePoster = (index) => { dispatch(removePoster(selectedPosters[index].movieId, selectedPosters[index].posterId)); }; const handleDownload = async () => { const postersToDownload = selectedPosters .filter((_, index) => selectedForDownload[index]) .map((poster) => ({ movieId: poster.movieId, posterId: poster.posterId, movieName: poster.movieName, movieYear: poster.movieYear })); console.log('Downloading posters:', postersToDownload); try { const response = await axios.post('http://localhost:5000/api/download-posters', { posters: postersToDownload, format: downloadFormat, }, { responseType: 'blob', }); const blob = new Blob([response.data], { type: response.headers['content-type'] }); const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = `posters.${downloadFormat}`; link.click(); } catch (error) { console.error('Failed to download posters:', error); } }; const handleShare = () => { console.log('Sharing selected posters'); }; const handleRename = (index) => { setCurrentPosterIndex(index); setNewPosterName(selectedPosters[index].posterId); setRenameDialogOpen(true); }; const handleRenameConfirm = () => { console.log(`Renaming poster ${currentPosterIndex} to ${newPosterName}`); setRenameDialogOpen(false); }; return (
Your Cart {selectedPosters.map((poster, index) => ( {`Movie ID: ${poster.movieId}`} {poster.posterId} { const newSelected = [...selectedForDownload]; newSelected[index] = e.target.checked; setSelectedForDownload(newSelected); }} className={classes.checkbox} /> } label="Select for download" className={classes.typography} /> ))}
Download Format setDownloadFormat(e.target.value)} SelectProps={{ native: true, }} className={classes.formControl} >
setRenameDialogOpen(false)}> Rename Poster setNewPosterName(e.target.value)} />
); }; export default Cart;