From 70a110743d7970f25546607733330440ba9a790e Mon Sep 17 00:00:00 2001 From: squirrel <47972284+zksquirrel@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:05:46 +0000 Subject: [PATCH] Create NewsletterForm.tsx --- src/components/Newsletter/NewsletterForm.tsx | 112 +++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/components/Newsletter/NewsletterForm.tsx diff --git a/src/components/Newsletter/NewsletterForm.tsx b/src/components/Newsletter/NewsletterForm.tsx new file mode 100644 index 0000000..f02734f --- /dev/null +++ b/src/components/Newsletter/NewsletterForm.tsx @@ -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 ( +
+
+ + +
+ + {activeNewsletter && ( +
+ handleSubmit( + e, + activeNewsletter === 'networkstats' ? 'networkstats' : 'ecosystemnews' + ) + } + style={{ margin: '20px auto', maxWidth: '400px' }} + > + + +
+ )} +
+ ); +}