import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
const relayURL = "https://cephalization-smsjournalertextrelay.web.val.run";
function App() {
const [phoneNumber, setPhoneNumber] = useState("");
const [registrationKey, setRegistrationKey] = useState("");
const [message, setMessage] = useState("");
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try {
const sanitizedPhoneNumber = phoneNumber.replace(/\D/g, "");
const response = await fetch("/submit", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ phoneNumber: sanitizedPhoneNumber, registrationKey }),
});
if (response.ok) {
setMessage("Phone number submitted successfully!");
setPhoneNumber("");
setRegistrationKey("");
} else {
setMessage("Failed to submit phone number.");
}
} catch (error) {
setMessage("An error occurred. Please try again.");
}
};
return (
<div>
<h1>Phone Number Submission</h1>
<form onSubmit={handleSubmit}>
<input
type="tel"
value={phoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
placeholder="Enter phone number"
required
/>
<input
value={registrationKey}
onChange={(e) => setRegistrationKey(e.target.value)}
name="registrationKey"
type="password"
placeholder="Enter registration key"
required
/>
<button type="submit">Submit</button>
</form>
{message && <p>{message}</p>}
<p>
<a href={import.meta.url.replace("esm.town", "val.town")}>View source</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const sharedSecret = Deno.env.get("smsjournalersecret");
const url = new URL(request.url);
if (url.pathname === "/submit" && request.method === "POST") {
const { phoneNumber, registrationKey } = await request.json();
const response = await fetch(relayURL + "/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${sharedSecret}`,
},
body: JSON.stringify({ phone: phoneNumber, registrationKey }),
});
if (!response.ok) {
return new Response(JSON.stringify({ success: false }), {
headers: { "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify({ success: true }), {
headers: { "Content-Type": "application/json" },
});
}
return new Response(
`
<html>
<head>
<title>Phone Number Submission</title>