Basic api handling

This commit is contained in:
pmoharana-cmd 2024-04-23 09:40:03 -04:00
parent c71d5d4026
commit 3fd0a545e7
3 changed files with 91 additions and 72 deletions

3
compass/.gitignore vendored
View File

@ -33,3 +33,6 @@ yarn-error.log*
# typescript # typescript
*.tsbuildinfo *.tsbuildinfo
next-env.d.ts next-env.d.ts
# environment variables
.env

View File

@ -0,0 +1,9 @@
import { NextResponse } from "next/server";
interface ResponseData {
message: string;
}
export async function GET() {
return NextResponse.json({ message: "Hello World!" }, { status: 200 });
}

View File

@ -1,13 +1,13 @@
// pages/index.tsx // pages/index.tsx
"use client"; "use client";
import Button from '@/components/Button'; import Button from "@/components/Button";
import Input from '@/components/Input' import Input from "@/components/Input";
import InlineLink from '@/components/InlineLink'; import InlineLink from "@/components/InlineLink";
import Paper from '@/components/auth/Paper'; import Paper from "@/components/auth/Paper";
// import { Metadata } from 'next' // import { Metadata } from 'next'
import Image from 'next/image'; import Image from "next/image";
import {ChangeEvent, useState} from "react"; import { ChangeEvent, useState } from "react";
// export const metadata: Metadata = { // export const metadata: Metadata = {
// title: 'Login', // title: 'Login',
@ -18,34 +18,37 @@ export default function Page() {
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [error, setError] = useState(""); const [error, setError] = useState("");
const testFetch = () => {
const result = fetch("/api/health");
console.log(result);
};
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setEmail(event.currentTarget.value); setEmail(event.currentTarget.value);
console.log("email " + email); console.log("email " + email);
} };
const handlePasswordChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handlePasswordChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setPassword(event.currentTarget.value); setPassword(event.currentTarget.value);
console.log("password " + password) console.log("password " + password);
} };
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault(); event.preventDefault();
// Priority: Incorrect combo > Missing email > Missing password // Priority: Incorrect combo > Missing email > Missing password
if (password.trim().length === 0) { if (password.trim().length === 0) {
setError("Please enter your password.") setError("Please enter your password.");
} }
// This shouldn't happen, <input type="email"> already provides validation, but just in case. // This shouldn't happen, <input type="email"> already provides validation, but just in case.
if (email.trim().length === 0) { if (email.trim().length === 0) {
setError("Please enter your email.") setError("Please enter your email.");
} }
// Placeholder for incorrect email + password combo. // Placeholder for incorrect email + password combo.
if (email === "incorrect@gmail.com" && password) { if (email === "incorrect@gmail.com" && password) {
setError("Incorrect password.") setError("Incorrect password.");
} }
} };
return ( return (
<> <>
@ -53,28 +56,32 @@ export default function Page() {
<form className="mb-0 m-auto mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8 bg-white max-w-xl"> <form className="mb-0 m-auto mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8 bg-white max-w-xl">
<Image <Image
src="/logo.png" src="/logo.png"
alt='Compass Center logo.' alt="Compass Center logo."
width={100} width={100}
height={91} height={91}
/> />
<h1 className='font-bold text-xl text-purple-800'>Login</h1> <h1 className="font-bold text-xl text-purple-800">Login</h1>
<div className="mb-4"> <div className="mb-4">
<Input type='email' title="Email" placeholder="janedoe@gmail.com" onChange={handleEmailChange} /> <Input
type="email"
title="Email"
placeholder="janedoe@gmail.com"
onChange={handleEmailChange}
/>
</div> </div>
<div className="mb-6"> <div className="mb-6">
<Input type='password' title="Password" onChange={handlePasswordChange} /> <Input
type="password"
title="Password"
onChange={handlePasswordChange}
/>
</div> </div>
<div className="flex flex-col items-left space-y-4"> <div className="flex flex-col items-left space-y-4">
<InlineLink href="/forgot_password"> <InlineLink href="/forgot_password">Forgot password?</InlineLink>
Forgot password? <Button onClick={handleClick}>Login</Button>
</InlineLink>
<Button onClick={handleClick}>
Login
</Button>
<div className="text-center text-red-600" hidden={!error}> <div className="text-center text-red-600" hidden={!error}>
<p>{error}</p> <p>{error}</p>
</div> </div>
</div> </div>
</form> </form>
<p className="text-center mt-6 text-gray-500 text-xs"> <p className="text-center mt-6 text-gray-500 text-xs">
@ -83,4 +90,4 @@ export default function Page() {
</Paper> </Paper>
</> </>
); );
}; }