Skip to content

Commit

Permalink
Merge pull request #122 from teslasdev/fix/newsletter
Browse files Browse the repository at this point in the history
pushed newsletter
  • Loading branch information
zksquirrel authored Jan 7, 2025
2 parents edcb252 + e685a0a commit ac04515
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/shieldednewsletter/ecosystemnews.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@

u1tkxuctrqlzykeac095up2n3k48grh9gqgqxmk0ay3d2aqay3j420wne2v78eqg82cmmkwx24j7cw3rcfaa2jxvwdzmkg8cupjpvmfwjk9udcqxlns87qajzc3tju25v7dek3k99xvt9nv0nsxx5rr4vj9ltcjfhas999u0vpc9qfrejkcdycv6ugxt8adqujglx4xj6cpaz0zcp9zz0
1 change: 0 additions & 1 deletion public/shieldednewsletter/networkstats.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

80 changes: 80 additions & 0 deletions src/app/newsletter/page.tsx
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;
2 changes: 1 addition & 1 deletion src/components/ContentSections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const ContentSections = () => {
<div className='flex flex-row space-x-4 mt-4'>
<FadeInAnimation>
<Link
href={'https://zechub.wiki/newsletter'}
href={'/newsletter'}
className='inline-flex justify-center items-center w-full px-3 py-2 text-sm font-medium text-center text-white bg-blue-700 rounded-lg hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800'
>
Subscribe
Expand Down
57 changes: 57 additions & 0 deletions src/pages/api/subscribe.js
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" });
}

0 comments on commit ac04515

Please sign in to comment.