refactor[loginPage]: Refactor login Page to use Form UI Radix Modal

This commit is contained in:
igorrCarvalho 2023-08-10 03:22:18 -03:00
commit 33944e5baf
4 changed files with 138 additions and 39 deletions

View file

@ -514,3 +514,8 @@ export const CONTROL_INPUT_STATE = {
cnfPassword: "", cnfPassword: "",
username: "", username: "",
}; };
export const CONTROL_LOGIN_STATE = {
username: "",
password: "",
};

View file

@ -1,39 +1,130 @@
import { FaApple, FaGithub } from "react-icons/fa"; import * as Form from "@radix-ui/react-form";
import { useState } from "react";
import InputComponent from "../../components/inputComponent";
import { Button } from "../../components/ui/button"; import { Button } from "../../components/ui/button";
import { Input } from "../../components/ui/input"; import { Input } from "../../components/ui/input";
import { GoogleIcon } from "../../icons/Google"; import { CONTROL_LOGIN_STATE } from "../../constants/constants";
import { inputHandlerEventType, loginInputStateType, } from "../../types/components";
import { Link } from "react-router-dom";
import IconComponent from "../../components/genericIconComponent";
export default function LoginPage() { export default function LoginPage(): JSX.Element {
const [inputState, setInputState] =
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
const { password, username } = inputState;
function handleInput({
target: { name, value },
}: inputHandlerEventType): void {
setInputState((prev) => ({ ...prev, [name]: value }));
}
return ( return (
<div className="flex h-full w-full flex-col items-center justify-center bg-muted"> <Form.Root
<div className="flex w-72 flex-col items-center justify-center gap-2"> onSubmit={(event) => {
<span className="mb-4 text-5xl">⛓️</span> if (password === "") {
<span className="mb-6 text-2xl font-semibold text-primary"> event.preventDefault();
Log in to LangFlow return;
</span> }
<div className="flex w-full items-center justify-center gap-2">
<Button variant="primary" className="w-full py-6"> const data = Object.fromEntries(new FormData(event.currentTarget));
<FaApple className="h-6 w-6" /> event.preventDefault();
</Button> }}
<Button variant="primary" className="w-full py-6"> className="w-full h-full"
<FaGithub className="h-6 w-6" /> >
</Button> <div className="flex h-full w-full flex-col items-center justify-center bg-muted">
<Button variant="primary" className="w-full py-6"> <div className="flex w-72 flex-col items-center justify-center gap-2">
<div className="h-6 w-6"> <span className="mb-4 text-5xl">⛓️</span>
<GoogleIcon /> <span className="mb-6 text-2xl font-semibold text-primary">
</div> Sign in to LangFlow
</Button> </span>
<div className="flex w-full items-center justify-center gap-2">
<Button variant="primary" className="w-full py-6">
<IconComponent
name="FaApple"
className="h-6 w-6"
/>
</Button>
<Button variant="primary" className="w-full py-6">
<IconComponent
name="FaGithub"
className="h-6 w-6"
/>
</Button>
<Button variant="primary" className="w-full py-6">
<IconComponent
name="GoogleSearchRun"
className="h-6 w-6"
/>
</Button>
</div>
<span className="text-sm text-muted-foreground">or</span>
<div className="w-full mb-3">
<Form.Field name="username">
<Form.Label className="data-[invalid]:label-invalid">
Username <span className="font-medium text-destructive">*</span>
</Form.Label>
<Form.Control asChild>
<Input
onChange={({ target: { value } }) => {
handleInput({ target: { name: "username", value } });
}}
value={username}
className="w-full"
required
placeholder="Username"
/>
</Form.Control>
<Form.Message match="valueMissing" className="field-invalid">
Please enter your username
</Form.Message>
</Form.Field>
</div>
<div className="w-full mb-3">
<Form.Field name="password" serverInvalid={password === ""}>
<Form.Label className="data-[invalid]:label-invalid">
Password <span className="font-medium text-destructive">*</span>
</Form.Label>
<Form.Control asChild>
<InputComponent
onChange={(value) => {
handleInput({ target: { name: "password", value } });
}}
value={password}
password={true}
placeholder="Password"
className="w-full"
/>
</Form.Control>
<Form.Message className="field-invalid" match="valueMissing">
Please enter your password
</Form.Message>
{password === "" && (
<Form.Message className="field-invalid">
Please enter a valid password
</Form.Message>
)}
</Form.Field>
</div>
<div className="w-full">
<Form.Submit asChild>
<Button className="mr-3 mt-6 w-full">Sign in</Button>
</Form.Submit>
</div>
<div className="w-full">
<Link to="/signup">
<Button className="w-full" variant="outline">
Don't have an account?&nbsp;<b>Sign Up</b>
</Button>
</Link>
</div>
</div> </div>
<span className="text-sm text-muted-foreground">or</span>
<Input className="bg-background" placeholder="Email address" />
<Input className="bg-background" placeholder="Password" />
<Button variant="default" className="w-full">
Login
</Button>
<Button variant="outline" className="mt-6 w-full">
Don't have an account?&nbsp;<b>Sign Up</b>
</Button>
</div> </div>
</div> </Form.Root>
); );
} }

View file

@ -1,18 +1,16 @@
import * as Form from "@radix-ui/react-form"; import * as Form from "@radix-ui/react-form";
import { useState } from "react"; import { FormEvent, useState } from "react";
import { FaApple, FaGithub } from "react-icons/fa";
import InputComponent from "../../components/inputComponent"; import InputComponent from "../../components/inputComponent";
import { Button } from "../../components/ui/button"; import { Button } from "../../components/ui/button";
import { Input } from "../../components/ui/input"; import { Input } from "../../components/ui/input";
import { CONTROL_INPUT_STATE } from "../../constants/constants"; import { CONTROL_INPUT_STATE } from "../../constants/constants";
import { GoogleIcon } from "../../icons/Google"; import { inputHandlerEventType, signUpInputStateType } from "../../types/components";
import { inputHandlerEventType, inputStateType } from "../../types/components";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import IconComponent from "../../components/genericIconComponent"; import IconComponent from "../../components/genericIconComponent";
export default function SignUp(): JSX.Element { export default function SignUp(): JSX.Element {
const [inputState, setInputState] = const [inputState, setInputState] =
useState<inputStateType>(CONTROL_INPUT_STATE); useState<signUpInputStateType>(CONTROL_INPUT_STATE);
const { password, cnfPassword, username } = inputState; const { password, cnfPassword, username } = inputState;
@ -23,8 +21,8 @@ export default function SignUp(): JSX.Element {
} }
return ( return (
<Form.Root <Form.Root
onSubmit={(event) => { onSubmit={(event: FormEvent<HTMLFormElement>) => {
if (password !== cnfPassword) { if (password === "") {
event.preventDefault(); event.preventDefault();
return; return;
} }

View file

@ -174,7 +174,7 @@ export interface languageMap {
[key: string]: string | undefined; [key: string]: string | undefined;
} }
export type inputStateType = { export type signUpInputStateType = {
password: string; password: string;
cnfPassword: string; cnfPassword: string;
username: string; username: string;
@ -219,3 +219,8 @@ export type UserManagementType = {
index?: number; index?: number;
onConfirm: (index, data) => void; onConfirm: (index, data) => void;
}; };
export type loginInputStateType = {
username: string;
password: string;
}