|
| 1 | +// src/ODNC/index.js |
| 2 | +import React, { useState } from 'react'; |
| 3 | +import axios from 'axios'; |
| 4 | +import { Container, Typography, Button, TextField, Snackbar, Alert, Card, CardContent, Grid, CircularProgress } from '@mui/material'; |
| 5 | + |
| 6 | +function ODNC() { |
| 7 | + const [energyType, setEnergyType] = useState(''); |
| 8 | + const [amount, setAmount] = useState(''); |
| 9 | + const [predictionData, setPredictionData] = useState([]); |
| 10 | + const [conversionHistory, setConversionHistory] = useState([]); |
| 11 | + const [error, setError] = useState(null); |
| 12 | + const [successMessage, setSuccessMessage] = useState(''); |
| 13 | + const [loading, setLoading] = useState(false); |
| 14 | + |
| 15 | + const handleEnergyConversion = async () => { |
| 16 | + if (!energyType || !amount) { |
| 17 | + setError('Please enter both energy type and amount.'); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + setLoading(true); |
| 22 | + try { |
| 23 | + const response = await axios.post('/api/convert-energy', { energyType, amount: parseFloat(amount) }); |
| 24 | + const convertedAmount = response.data.result; |
| 25 | + setSuccessMessage(`Converted Amount: ${convertedAmount}`); |
| 26 | + // Update conversion history |
| 27 | + setConversionHistory([...conversionHistory, { energyType, amount: parseFloat(amount), convertedAmount }]); |
| 28 | + } catch (err) { |
| 29 | + setError(err.response?.data?.message || 'Energy conversion failed.'); |
| 30 | + } finally { |
| 31 | + setLoading(false); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const handleResourcePrediction = async () => { |
| 36 | + if (!energyType || !amount) { |
| 37 | + setError('Please enter both energy type and amount.'); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + setLoading(true); |
| 42 | + try { |
| 43 | + const response = await axios.post('/api/predict-extraction', { energyData: [{ type: energyType, amount: parseFloat(amount) }] }); |
| 44 | + setPredictionData(response.data.prediction); |
| 45 | + setSuccessMessage('Prediction generated successfully!'); |
| 46 | + } catch (err) { |
| 47 | + setError(err.response?.data?.message || 'Resource prediction failed.'); |
| 48 | + } finally { |
| 49 | + setLoading(false); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Container maxWidth="lg"> |
| 55 | + <Typography variant="h4" align="center" gutterBottom> |
| 56 | + Omni-Dimensional Nexus Converter |
| 57 | + </Typography> |
| 58 | + <TextField |
| 59 | + label="Energy Type" |
| 60 | + variant="outlined" |
| 61 | + fullWidth |
| 62 | + margin="normal" |
| 63 | + value={energyType} |
| 64 | + onChange={(e) => setEnergyType(e.target.value)} |
| 65 | + /> |
| 66 | + <TextField |
| 67 | + label="Amount" |
| 68 | + type="number" |
| 69 | + variant="outlined" |
| 70 | + fullWidth |
| 71 | + margin="normal" |
| 72 | + value={amount} |
| 73 | + onChange={(e) => setAmount(e.target.value)} |
| 74 | + /> |
| 75 | + <Button variant="contained" color="primary" onClick={handleEnergyConversion} disabled={loading}> |
| 76 | + Convert Energy |
| 77 | + </Button> |
| 78 | + <Button variant="outlined" color="secondary" onClick={handleResourcePrediction} style={{ marginTop: '10px' }} disabled={loading}> |
| 79 | + Predict Resource Extraction |
| 80 | + </Button> |
| 81 | + {loading && <CircularProgress style={{ marginTop: '20px' }} />} |
| 82 | + <Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError(null)}> |
| 83 | + <Alert onClose={() => setError(null)} severity="error"> |
| 84 | + {error} |
| 85 | + </Alert> |
| 86 | + </Snackbar> |
| 87 | + <Snackbar open={!!successMessage} autoHideDuration={6000} onClose={() => setSuccessMessage('')}> |
| 88 | + <Alert onClose={() => setSuccessMessage('')} severity="success"> |
| 89 | + {successMessage} |
| 90 | + </Alert> |
| 91 | + </Snackbar> |
| 92 | + {predictionData.length > 0 && ( |
| 93 | + <Card style={{ marginTop: '20px' }}> |
| 94 | + <CardContent> |
| 95 | + <Typography variant="h6">Predicted Resource Extraction:</Typography> |
| 96 | + <ul> |
| 97 | + {predictionData.map((item, index) => ( |
| 98 | + <li key={index}> |
| 99 | + {item.type}: {item.predictedValue.toFixed(2)} |
| 100 | + </li> |
| 101 | + ))} |
| 102 | + </ul> |
| 103 | + </CardContent> |
| 104 | + </Card> |
| 105 | + )} |
| 106 | + {conversionHistory.length > 0 && ( |
| 107 | + <div style={{ marginTop: '20px' }}> |
| 108 | + <Typography variant="h6">Conversion History:</Typography> |
| 109 | + <Grid container spacing={2}> |
| 110 | + {conversionHistory.map((entry, index) => ( |
| 111 | + <Grid item xs={12} sm={6} md={4} key={index}> |
| 112 | + <Card> |
| 113 | + <CardContent> |
| 114 | + <Typography variant="body1">Energy Type: {entry.energyType}</Typography> |
| 115 | + <Typography variant="body2">Amount: {entry.amount}</Typography> |
| 116 | + <Typography variant="body2">Converted Amount: {entry.convertedAmount}</Typography> |
| 117 | + </CardContent> |
| 118 | + </Card> |
| 119 | + </Grid> |
| 120 | + ))} |
| 121 | + </Grid> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + </Container> |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +export default ODNC; |
0 commit comments