"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Button from "@/components/shared/Button"; import Input from "@/components/shared/Input"; export default function NewApplicationPage() { const router = useRouter(); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [error, setError] = useState(""); const [newApplication, setNewApplication] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); async function handleSubmit(e) { e.preventDefault(); setIsSubmitting(true); setError(""); try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/CreateApplication", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), name, description, }), }, ); const data = await response.json(); // Check for Twirp error response if (data.code || data.msg) { throw new Error(data.msg || "Failed to create application"); } if (!data.key) { throw new Error("No API key received"); } setNewApplication({ key: data.key, name: data.application.name, description: data.application.description, created_at: data.application.created_at, }); } catch (error) { setError(error.message || "Failed to create application"); console.error("Error:", error); } finally { setIsSubmitting(false); } } return ( <> {newApplication ? (
Description:
{newApplication.description}
Please copy your API key now. You won't be able to see it again!
{newApplication.key}
Created on: {new Date(newApplication.created_at).toLocaleString()}