import React, { Component } from 'react'; import { Box, Typography, TextField, Button, Paper, Alert, CircularProgress, Rating } from '@mui/material'; import StarIcon from '@mui/icons-material/Star'; import { withI18n } from '../i18n/withTranslation.js'; import PhotoUpload from './PhotoUpload.js'; class ArticleRatingForm extends Component { constructor(props) { super(props); this.state = { name: '', email: '', rating: 0, review: '', photos: [], loading: false, success: false, error: null }; this.photoUploadRef = React.createRef(); } handleInputChange = (field) => (event) => { this.setState({ [field]: event.target.value }); }; handleRatingChange = (event, newValue) => { this.setState({ rating: newValue }); }; handlePhotosChange = (files) => { this.setState({ photos: files }); }; convertPhotosToBase64 = (photos) => { return Promise.all( photos.map(photo => { return new Promise((resolve, _reject) => { const reader = new FileReader(); reader.onload = (e) => { resolve({ name: photo.name, type: photo.type, size: photo.size, data: e.target.result // base64 string }); }; reader.readAsDataURL(photo); }); }) ); }; handleSubmit = async (event) => { event.preventDefault(); this.setState({ loading: true }); try { // Convert photos to base64 const photosBase64 = await this.convertPhotosToBase64(this.state.photos); // Prepare data for API emission const ratingData = { type: 'article_rating', productId: this.props.productId, productName: this.props.productName, name: this.state.name, email: this.state.email, rating: this.state.rating, review: this.state.review, photos: photosBase64, timestamp: new Date().toISOString() }; // Emit data via socket console.log('Article Rating Data to emit:', ratingData); if (this.props.socket) { this.props.socket.emit('article_rating_submit', ratingData); // Set up response handler this.props.socket.once('article_rating_response', (response) => { if (response.success) { this.setState({ loading: false, success: true, name: '', email: '', rating: 0, review: '', photos: [] }); // Reset the photo upload component if (this.photoUploadRef.current) { this.photoUploadRef.current.reset(); } } else { this.setState({ loading: false, error: response.error || 'Ein Fehler ist aufgetreten' }); } // Clear messages after 3 seconds setTimeout(() => { this.setState({ success: false, error: null }); }, 3000); }); } } catch { this.setState({ loading: false, error: 'Fehler beim Verarbeiten der Fotos' }); } // Fallback timeout in case backend doesn't respond setTimeout(() => { if (this.state.loading) { this.setState({ loading: false, success: true, name: '', email: '', rating: 0, review: '', photos: [] }); // Reset the photo upload component if (this.photoUploadRef.current) { this.photoUploadRef.current.reset(); } // Clear success message after 3 seconds setTimeout(() => { this.setState({ success: false }); }, 3000); } }, 5000); }; render() { const { name, email, rating, review, loading, success, error } = this.state; return ( Artikel Bewerten Teilen Sie Ihre Erfahrungen mit diesem Artikel und helfen Sie anderen Kunden bei der Entscheidung. {success && ( Vielen Dank für Ihre Bewertung! Sie wird nach Prüfung veröffentlicht. )} {error && ( {error} )} Bewertung * } /> {rating > 0 ? `${rating} von 5 Sternen` : 'Bitte bewerten'} ); } } export default withI18n()(ArticleRatingForm);