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 || this.props.t("productDialogs.errorGeneric") }); } // 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; const { t } = this.props; return ( {t("productDialogs.availabilityTitle")} {t("productDialogs.availabilitySubtitle")} {success && ( {notificationMethod === 'email' ? t("productDialogs.availabilitySuccessEmail") : t("productDialogs.availabilitySuccessTelegram")} )} {error && ( {error} )} {t("productDialogs.notificationMethodLabel")} } label={t("productDialogs.emailLabel")} /> } label={t("productDialogs.telegramBotLabel")} /> {notificationMethod === 'email' && ( )} {notificationMethod === 'telegram' && ( )} ); } } export default withI18n()(ArticleAvailabilityForm);