import React, { Component } from 'react'; import { Box, Typography, TextField, Button, Paper, Alert, CircularProgress, FormControl, FormLabel, RadioGroup, FormControlLabel, Radio } from '@mui/material'; import { withI18n } from '../i18n/withTranslation.js'; class ArticleAvailabilityForm extends Component { constructor(props) { super(props); this.state = { name: '', email: '', telegramId: '', notificationMethod: 'email', message: '', loading: false, success: false, error: null }; } handleInputChange = (field) => (event) => { this.setState({ [field]: event.target.value }); }; handleNotificationMethodChange = (event) => { this.setState({ notificationMethod: event.target.value, // Clear the other field when switching methods email: event.target.value === 'email' ? this.state.email : '', telegramId: event.target.value === 'telegram' ? this.state.telegramId : '' }); }; handleSubmit = (event) => { event.preventDefault(); // Prepare data for API emission const availabilityData = { type: 'availability_inquiry', productId: this.props.productId, productName: this.props.productName, name: this.state.name, notificationMethod: this.state.notificationMethod, email: this.state.notificationMethod === 'email' ? this.state.email : '', telegramId: this.state.notificationMethod === 'telegram' ? this.state.telegramId : '', message: this.state.message, timestamp: new Date().toISOString() }; // Emit data via socket console.log('Availability Inquiry Data to emit:', availabilityData); window.socketManager.emit('availability_inquiry_submit', availabilityData); // Set up response handler window.socketManager.once('availability_inquiry_response', (response) => { if (response.success) { this.setState({ loading: false, success: true, name: '', email: '', telegramId: '', notificationMethod: 'email', message: '' }); } 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); }); this.setState({ loading: true }); // Fallback timeout in case backend doesn't respond setTimeout(() => { if (this.state.loading) { this.setState({ loading: false, success: true, name: '', email: '', telegramId: '', notificationMethod: 'email', message: '' }); // Clear success message after 3 seconds setTimeout(() => { this.setState({ success: false }); }, 3000); } }, 5000); }; render() { const { name, email, telegramId, notificationMethod, message, loading, success, error } = this.state; return ( Verfügbarkeit anfragen Dieser Artikel ist derzeit nicht verfügbar. Gerne informieren wir Sie, sobald er wieder lieferbar ist. {success && ( Vielen Dank für Ihre Anfrage! Wir werden Sie {notificationMethod === 'email' ? 'per E-Mail' : 'über Telegram'} informieren, sobald der Artikel wieder verfügbar ist. )} {error && ( {error} )} Wie möchten Sie benachrichtigt werden? } label="E-Mail" /> } label="Telegram Bot" /> {notificationMethod === 'email' && ( )} {notificationMethod === 'telegram' && ( )} ); } } export default withI18n()(ArticleAvailabilityForm);