|
| 1 | +import { useState } from 'react' |
| 2 | + |
| 3 | +import Alert from '@mui/material/Alert' |
| 4 | +import Box from '@mui/material/Box' |
| 5 | +import Button from '@mui/material/Button' |
| 6 | +import CircularProgress from '@mui/material/CircularProgress' |
| 7 | +import Container from '@mui/material/Container' |
| 8 | +import Snackbar from '@mui/material/Snackbar' |
| 9 | +import TextField from '@mui/material/TextField' |
| 10 | +import Typography from '@mui/material/Typography' |
| 11 | + |
| 12 | + |
| 13 | +const EmailSubscription = () => { |
| 14 | + const [email, setEmail] = useState('') |
| 15 | + const [loading, setLoading] = useState(false) |
| 16 | + const [success, setSuccess] = useState(false) |
| 17 | + const [error, setError] = useState('') |
| 18 | + |
| 19 | + const handleSubmit = async (e: React.FormEvent) => { |
| 20 | + e.preventDefault() |
| 21 | + if (!email) return |
| 22 | + |
| 23 | + setLoading(true) |
| 24 | + setError('') |
| 25 | + |
| 26 | + try { |
| 27 | + const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080' |
| 28 | + const response = await fetch(`${apiUrl}/api/subscribe`, { |
| 29 | + method: 'POST', |
| 30 | + headers: { |
| 31 | + 'Content-Type': 'application/json', |
| 32 | + }, |
| 33 | + body: JSON.stringify({ email }), |
| 34 | + }) |
| 35 | + |
| 36 | + if (!response.ok) { |
| 37 | + throw new Error('Subscription failed') |
| 38 | + } |
| 39 | + |
| 40 | + setSuccess(true) |
| 41 | + setEmail('') |
| 42 | + } catch (err) { |
| 43 | + setError('Something went wrong. Please try again.') |
| 44 | + } finally { |
| 45 | + setLoading(false) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return ( |
| 50 | + <Container id="subscription" sx={{ py: 8, textAlign: 'center' }}> |
| 51 | + <Box |
| 52 | + // @ts-ignore |
| 53 | + sx={{ |
| 54 | + background: 'rgba(255, 255, 255, 0.05)', |
| 55 | + backdropFilter: 'blur(10px)', |
| 56 | + borderRadius: 4, |
| 57 | + p: { xs: 3, md: 6 }, |
| 58 | + maxWidth: 'md', |
| 59 | + mx: 'auto', |
| 60 | + border: '1px solid rgba(255, 255, 255, 0.1)', |
| 61 | + }} |
| 62 | + data-aos="fade-up" |
| 63 | + > |
| 64 | + <Typography variant="h3" gutterBottom color="text.primary" sx={{ fontWeight: 'bold' }}> |
| 65 | + Stay in the Loop |
| 66 | + </Typography> |
| 67 | + <Typography variant="body1" color="text.secondary" sx={{ mb: 4 }}> |
| 68 | + Subscribe to our newsletter for the latest updates, announcements, and opportunities! |
| 69 | + </Typography> |
| 70 | + |
| 71 | + <Box |
| 72 | + component="form" |
| 73 | + onSubmit={handleSubmit} |
| 74 | + sx={{ |
| 75 | + display: 'flex', |
| 76 | + flexDirection: { xs: 'column', sm: 'row' }, |
| 77 | + gap: 2, |
| 78 | + justifyContent: 'center', |
| 79 | + alignItems: 'center', |
| 80 | + }} |
| 81 | + > |
| 82 | + <TextField |
| 83 | + variant="outlined" |
| 84 | + placeholder="Enter your email" |
| 85 | + type="email" |
| 86 | + value={email} |
| 87 | + onChange={(e) => setEmail(e.target.value)} |
| 88 | + required |
| 89 | + fullWidth |
| 90 | + sx={{ |
| 91 | + maxWidth: 400, |
| 92 | + '& .MuiOutlinedInput-root': { |
| 93 | + bgcolor: 'background.paper', |
| 94 | + borderRadius: 2, |
| 95 | + }, |
| 96 | + }} |
| 97 | + /> |
| 98 | + <Button |
| 99 | + variant="contained" |
| 100 | + size="large" |
| 101 | + type="submit" |
| 102 | + disabled={loading} |
| 103 | + sx={{ |
| 104 | + borderRadius: 2, |
| 105 | + px: 4, |
| 106 | + py: 1.8, |
| 107 | + background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)', |
| 108 | + color: 'white', |
| 109 | + fontWeight: 'bold', |
| 110 | + boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)', |
| 111 | + '&:hover': { |
| 112 | + background: 'linear-gradient(45deg, #FE6B8B 60%, #FF8E53 90%)', |
| 113 | + }, |
| 114 | + }} |
| 115 | + > |
| 116 | + {loading ? <CircularProgress size={24} color="inherit" /> : 'Subscribe'} |
| 117 | + </Button> |
| 118 | + </Box> |
| 119 | + </Box> |
| 120 | + |
| 121 | + <Snackbar open={success} autoHideDuration={6000} onClose={() => setSuccess(false)}> |
| 122 | + <Alert onClose={() => setSuccess(false)} severity="success" sx={{ width: '100%' }}> |
| 123 | + Successfully subscribed! Check your inbox for a welcome email. |
| 124 | + </Alert> |
| 125 | + </Snackbar> |
| 126 | + <Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('')}> |
| 127 | + <Alert onClose={() => setError('')} severity="error" sx={{ width: '100%' }}> |
| 128 | + {error} |
| 129 | + </Alert> |
| 130 | + </Snackbar> |
| 131 | + </Container> |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +export default EmailSubscription |
0 commit comments