Skip to content

Commit

Permalink
Create NewsletterForm.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
zksquirrel authored Jan 3, 2025
1 parent 7883a80 commit 70a1107
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/components/Newsletter/NewsletterForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { useState } from 'react';

export default function NewsletterForm() {
const [address, setAddress] = useState('');
const [activeNewsletter, setActiveNewsletter] = useState(null);

const handleButtonClick = (newsletterType) => {
setActiveNewsletter(
activeNewsletter === newsletterType ? null : newsletterType
);
setAddress('');
};

const handleSubmit = async (e, type) => {
e.preventDefault();

if (!address) {
alert('Please enter a Zcash Unified Address');
return;
}

const apiEndpoint =
type === 'networkstats'
? '/api/saveNetworkStatsAddress'
: '/api/saveEcosystemNewsAddress';

try {
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ address }),
});

if (response.ok) {
alert('Your address has been saved successfully!');
setAddress('');
setActiveNewsletter(null);
} else {
alert('There was an error saving your address. Please try again.');
}
} catch (error) {
console.error(error);
alert('An unexpected error occurred.');
}
};

return (
<div style={{ textAlign: 'center' }}>
<div style={{ marginBottom: '20px' }}>
<button
onClick={() => handleButtonClick('networkstats')}
className={`newsletter-button ${
activeNewsletter === 'networkstats' ? 'active' : ''
}`}
>
Sign Up for Network Stats
</button>
<button
onClick={() => handleButtonClick('ecosystemnews')}
className={`newsletter-button ${
activeNewsletter === 'ecosystemnews' ? 'active' : ''
}`}
style={{ marginLeft: '10px' }}
>
Sign Up for Ecosystem News
</button>
</div>

{activeNewsletter && (
<form
onSubmit={(e) =>
handleSubmit(
e,
activeNewsletter === 'networkstats' ? 'networkstats' : 'ecosystemnews'
)
}
style={{ margin: '20px auto', maxWidth: '400px' }}
>
<label>
Enter your Zcash Unified Address:
<input
type="text"
value={address}
onChange={(e) => setAddress(e.target.value)}
style={{
width: '100%',
margin: '10px 0',
padding: '10px',
border: '1px solid #ccc',
borderRadius: '5px',
}}
placeholder="Unified Address..."
/>
</label>
<button
type="submit"
style={{
padding: '10px 20px',
backgroundColor: '#007bff',
color: '#fff',
border: 'none',
cursor: 'pointer',
borderRadius: '5px',
}}
>
Submit
</button>
</form>
)}
</div>
);
}

0 comments on commit 70a1107

Please sign in to comment.