import React, { Component } from 'react'; import { Box, Typography, TextField, Button, Paper, Alert, CircularProgress } from '@mui/material'; import { withI18n } from '../i18n/withTranslation.js'; import PhotoUpload from './PhotoUpload.js'; class ArticleQuestionForm extends Component { constructor(props) { super(props); this.state = { name: '', email: '', question: '', photos: [], loading: false, success: false, error: null }; this.photoUploadRef = React.createRef(); } handleInputChange = (field) => (event) => { this.setState({ [field]: event.target.value }); }; 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 questionData = { type: 'article_question', productId: this.props.productId, productName: this.props.productName, name: this.state.name, email: this.state.email, question: this.state.question, photos: photosBase64, timestamp: new Date().toISOString() }; // Emit data via socket console.log('Article Question Data to emit:', questionData); window.socketManager.emit('article_question_submit', questionData); // Set up response handler window.socketManager.once('article_question_response', (response) => { if (response.success) { this.setState({ loading: false, success: true, name: '', email: '', question: '', 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: '', question: '', 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, question, loading, success, error } = this.state; return ( Frage zum Artikel Haben Sie eine Frage zu diesem Artikel? Wir helfen Ihnen gerne weiter. {success && ( Vielen Dank für Ihre Frage! Wir werden uns schnellstmöglich bei Ihnen melden. )} {error && ( {error} )} ); } } export default withI18n()(ArticleQuestionForm);