-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from teslasdev/fix/newsletter
pushed newsletter
- Loading branch information
Showing
5 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
u1tkxuctrqlzykeac095up2n3k48grh9gqgqxmk0ay3d2aqay3j420wne2v78eqg82cmmkwx24j7cw3rcfaa2jxvwdzmkg8cupjpvmfwjk9udcqxlns87qajzc3tju25v7dek3k99xvt9nv0nsxx5rr4vj9ltcjfhas999u0vpc9qfrejkcdycv6ugxt8adqujglx4xj6cpaz0zcp9zz0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"use client"; | ||
|
||
import React, { useState } from "react"; | ||
|
||
const Newsletter: React.FC = () => { | ||
const [unifiedAddress, setUnifiedAddress] = useState(""); | ||
const [selectedCategory, setSelectedCategory] = useState("Ecosystem News"); | ||
|
||
const handleSubmit = async () => { | ||
if (!unifiedAddress || !selectedCategory) { | ||
alert("Please enter a Unified Address and select a category."); | ||
return; | ||
} | ||
|
||
try { | ||
const response = await fetch("/api/subscribe", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
unifiedAddress, | ||
category: selectedCategory, | ||
}), | ||
}); | ||
|
||
if (response.ok) { | ||
alert("Unified address submitted successfully!"); | ||
setUnifiedAddress(""); | ||
setSelectedCategory("Ecosystem News"); | ||
} else { | ||
alert("Failed to submit the address."); | ||
} | ||
} catch (error) { | ||
console.error("Error submitting address:", error); | ||
alert("An error occurred. Please try again."); | ||
} | ||
}; | ||
return ( | ||
<div className="flex flex-col items-center justify-center h-[50vh] bg-gray-100 dark:bg-slate-900 p-4"> | ||
<h1 className="text-2xl font-bold mb-4">ZecHub Shielded Newsletter</h1> | ||
<p className="text-gray-700 mb-6"> | ||
Subscribe to updates by entering your Unified Address. | ||
</p> | ||
<div className="space-y-4 md:space-x-4 space-x-2"> | ||
<button | ||
onClick={() => setSelectedCategory("Ecosystem News")} | ||
className={`md:px-6 px-2 py-2 font-semibold rounded-lg ${ | ||
selectedCategory !== "Ecosystem News" ? "bg-gray-200" : "bg-[#1984c7] text-white" | ||
}`} | ||
> | ||
Ecosystem News | ||
</button> | ||
<button | ||
onClick={() => setSelectedCategory("Network Stats")} | ||
className={`md:px-6 px-4 py-2 font-semibold rounded-lg ${ | ||
selectedCategory !== "Network Stats" ? "bg-gray-200" : "bg-[#1984c7] text-white" | ||
}`} | ||
> | ||
Network Stats | ||
</button> | ||
</div> | ||
<input | ||
type="text" | ||
placeholder="Enter your Unified Address" | ||
value={unifiedAddress} | ||
onChange={(e) => setUnifiedAddress(e.target.value)} | ||
className="w-full max-w-md mt-6 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" | ||
/> | ||
<button | ||
onClick={handleSubmit} | ||
className="mt-4 px-6 py-2 bg-purple-600 text-white font-semibold rounded-lg hover:bg-purple-700" | ||
> | ||
Submit | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Newsletter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export default function handler(req, res) { | ||
if (req.method === "POST") { | ||
const { unifiedAddress, category } = req.body; | ||
|
||
// Basic validation for Unified Address | ||
const isValidUnifiedAddress = (address) => { | ||
return ( | ||
typeof address === "string" && | ||
address.startsWith("u") && | ||
address.length >= 78 | ||
); | ||
}; | ||
|
||
if ( | ||
!unifiedAddress || | ||
!category || | ||
!isValidUnifiedAddress(unifiedAddress) | ||
) { | ||
return res | ||
.status(400) | ||
.json({ message: "Invalid Unified Address or data" }); | ||
} | ||
|
||
// Determine the file path based on the category | ||
const fileName = | ||
category === "Ecosystem News" | ||
? "ecosystemnews.txt" | ||
: category === "Network Stats" | ||
? "networkstats.txt" | ||
: null; | ||
|
||
if (!fileName) { | ||
return res.status(400).json({ message: "Invalid category" }); | ||
} | ||
|
||
const filePath = path.join( | ||
process.cwd(), | ||
"public/shieldednewsletter", | ||
fileName | ||
); | ||
|
||
const entry = `${unifiedAddress}\n`; | ||
|
||
try { | ||
fs.appendFileSync(filePath, entry, "utf8"); | ||
return res.status(200).json({ message: "Address saved successfully" }); | ||
} catch (error) { | ||
console.error("Error writing to file:", error); | ||
return res.status(500).json({ message: "Error saving address" }); | ||
} | ||
} | ||
|
||
return res.status(405).json({ message: "Method not allowed" }); | ||
} |