Skip to content

Commit

Permalink
Refactor SignInForm
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcretu committed Nov 27, 2023
1 parent 8ad2133 commit f454c0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
26 changes: 12 additions & 14 deletions ui/src/components/auth/SignInForm.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Alert, Box, Button, Link, TextField } from "@mui/material";
import NextLink from "next/link";
import React, { useState } from "react";
import { Alert, Box, Button, TextField } from "@mui/material";
import { FormEvent, useState } from "react";

interface SignInFormProps {
onSubmit: (email: string, password: string) => void;
import Link from "@/components/Link";
import { SignInWithPasswordMutationVariables } from "@/graphql/generated";

type SignInFormProps = {
onSubmit: (args: SignInWithPasswordMutationVariables) => void;
errors?: string[];
}
};

const SignInForm: React.FC<SignInFormProps> = ({ onSubmit, errors }) => {
export default function SignInForm({ onSubmit, errors }: SignInFormProps) {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
onSubmit(email, password);
onSubmit({ email, password });
};

return (
Expand Down Expand Up @@ -87,7 +89,6 @@ const SignInForm: React.FC<SignInFormProps> = ({ onSubmit, errors }) => {

<Box display="flex" justifyContent="space-between">
<Link
component={NextLink}
variant="body2"
sx={{
textDecoration: "none",
Expand All @@ -99,7 +100,6 @@ const SignInForm: React.FC<SignInFormProps> = ({ onSubmit, errors }) => {
Forgot your password?
</Link>
<Link
component={NextLink}
variant="body2"
sx={{
textDecoration: "none",
Expand Down Expand Up @@ -128,7 +128,7 @@ const SignInForm: React.FC<SignInFormProps> = ({ onSubmit, errors }) => {
</Button>
</form>
);
};
}

const errorCodeToMessage = (error: string) => {
if (error === "invalid_credentials") {
Expand All @@ -139,5 +139,3 @@ const errorCodeToMessage = (error: string) => {
return `An unknown error occurred. Please try again and let us know if this keeps happening.`;
}
};

export default SignInForm;
7 changes: 1 addition & 6 deletions ui/src/pages/sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ const SignInPage: NextPageWithLayout = () => {
</Head>

<main>
<SignInForm
onSubmit={(email, password) =>
submitSignIn.mutate({ email, password })
}
errors={errors}
/>
<SignInForm onSubmit={submitSignIn.mutate} errors={errors} />
</main>
</div>
);
Expand Down

0 comments on commit f454c0e

Please sign in to comment.