Skip to content

Commit

Permalink
Co-authored-by: Diego Romero <[email protected]
Browse files Browse the repository at this point in the history
…b.com>
  • Loading branch information
JD-n0v committed Nov 29, 2023
1 parent 6b6a4ea commit 826a94c
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 54 deletions.
4 changes: 3 additions & 1 deletion apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@acme/db": "*",
"@acme/tailwind-config": "*",
"@clerk/nextjs": "^4.11.0",
"@hookform/resolvers": "^3.3.2",
"@tanstack/react-query": "^4.16.1",
"@trpc/client": "^10.1.0",
"@trpc/next": "^10.1.0",
Expand All @@ -25,7 +26,8 @@
"next": "^13.1.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"zod": "^3.18.0"
"react-hook-form": "^7.48.2",
"zod": "^3.20.0"
},
"devDependencies": {
"@types/node": "^18.0.0",
Expand Down
14 changes: 12 additions & 2 deletions apps/nextjs/src/components/ItemHeader/ItemHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useRouter } from "next/router";
import styles from "./Header.module.css";
import { useAuth } from "@clerk/nextjs";

function ItemHeader() {
const router = useRouter();
const { isSignedIn, signOut } = useAuth();
return (
<>
<header className={styles.header}>
Expand All @@ -13,8 +15,16 @@ function ItemHeader() {
<ul>
<li onClick={() => router.push(`/`)}>Home</li>
<li onClick={() => router.push(`/shoppingCart`)}>Cart</li>
<li onClick={() => router.push(`/login`)}>Login</li>
<li>Contact</li>
<li
onClick={() =>
isSignedIn
? signOut(() => router.push(`/`))
: router.push(`/login`)
}
>
{isSignedIn ? "logout" : "login"}
</li>
<li onClick={() => router.push(`/orders`)}>Orders</li>
</ul>
</span>
</header>
Expand Down
10 changes: 10 additions & 0 deletions apps/nextjs/src/components/RegisterForm/RegisterForm.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@
.cancelbtn {
background-color: red;
}
.errorMsg {
margin-bottom: 10px;
--tw-border-opacity: 1;
border-color: rgb(248 113 113 / var(--tw-border-opacity));
--tw-bg-opacity: 1;
background-color: rgb(254 242 242 / var(--tw-bg-opacity));
padding: 0.5rem;
--tw-text-opacity: 1;
color: rgb(153 27 27 / var(--tw-text-opacity));
}
123 changes: 85 additions & 38 deletions apps/nextjs/src/components/RegisterForm/RegisterForm.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,94 @@
import styles from "./RegisterForm.module.css";
import { useRouter } from "next/router";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { useSignUp } from "@clerk/nextjs";
import { useState } from "react";

const signUpFormValidator = z.object({
email: z.string().email(),
password: z.string().min(8, "Password must be at least 8 characters"),
});

export type SignUpFormSchema = Zod.infer<typeof signUpFormValidator>;

function RegisterForm() {
const router = useRouter();

const { register, handleSubmit, setError, formState } =
useForm<SignUpFormSchema>({
resolver: zodResolver(signUpFormValidator),
mode: "onBlur",
});
const { signUp, setActive } = useSignUp();
const [isLoading, setIsLoading] = useState(false);

const onSubmit = handleSubmit(async (vals) => {
if (!signUp) return;
setIsLoading(true);

try {
const result = await signUp.create({
emailAddress: vals.email,
password: vals.password,
});

if (result?.status === "missing_requirements") {
alert("mising");
}

if (result?.status === "complete") {
setActive({ session: result.createdSessionId });
router.push("/");
}
} catch (err) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
setError("root", { message: err.errors[0].message });
}
setIsLoading(false);
});

return (
<>
<form action="" method="post">
<div className={styles.container}>
<h1>Sign Up</h1>
<hr />

<label htmlFor="email">Email</label>
<input type="text" placeholder="Enter Email" name="email" required />

<label htmlFor="psw">Password</label>
<input
type="password"
placeholder="Enter Password"
name="psw"
required
/>

<label htmlFor="psw-repeat"> Repeat Password </label>
<input
type="password"
placeholder="Repeat Password"
name="psw-repeat"
required
/>
<div className={styles.buttonDiv}>
<button
type="button"
className={styles.cancelbtn}
onClick={() => router.back()}
>
Cancel
</button>
<button type="submit" className={styles.signupbtn}>
Sign Up
</button>
</div>
</div>
<main className={styles.container}>
<h1>Sign Up</h1>
<hr />
{formState.errors.root && (
<p className={styles.errorMsg}>{formState.errors.root?.message}</p>
)}

<form onSubmit={onSubmit}>
<label htmlFor="email">Email</label>
<input
type="email"
placeholder="[email protected]"
required
{...register("email")}
/>
<label htmlFor="psw">Password</label>
<input
type="password"
placeholder="Enter Password"
required
{...register("password")}
/>
{/* <div className={styles.buttonDiv}> */}
<button
type={"submit"}
className={styles.signupbtn}
disabled={isLoading}
>
Sign Up
</button>
{/* <button onClick={() => router.push(`/login`)}> Cancel </button>
</div> */}
</form>
</>

<p>
have an account? <a onClick={() => router.push(`/login`)}>Log in</a>
</p>
</main>
);
}
export default RegisterForm;
4 changes: 4 additions & 0 deletions apps/nextjs/src/components/content/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ function ItemCard({
onClick={(e) => {
e.stopPropagation();
addToCart();
if (item.stock.lessThanOrEqualTo(0)) {
alert("Item has no stock");
}
}}
disabled={item.stock.lessThanOrEqualTo(0)}
>
Add to cart
</button>
Expand Down
18 changes: 14 additions & 4 deletions apps/nextjs/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useAuth } from "@clerk/nextjs";
import styles from "./Header.module.css";
import { useRouter } from "next/router";
import { Dispatch, SetStateAction } from "react";
Expand All @@ -9,6 +10,7 @@ interface HeaderProps {

function Header({ filterValue, setFilterValue }: HeaderProps) {
const router = useRouter();
const { isSignedIn, signOut } = useAuth();
return (
<>
<header className={styles.header}>
Expand All @@ -24,10 +26,18 @@ function Header({ filterValue, setFilterValue }: HeaderProps) {
</span>
<span className={styles.listSpan}>
<ul>
<li>Home</li>
<li onClick={() => router.push(`/shoppingCart`)}>Shop</li>
<li onClick={() => router.push(`/login`)}>About</li>
<li>Contact</li>
<li onClick={() => router.push(`/`)}>Home</li>
<li onClick={() => router.push(`/shoppingCart`)}>Cart</li>
<li
onClick={() =>
isSignedIn
? signOut(() => router.push(`/`))
: router.push(`/login`)
}
>
{isSignedIn ? "logout" : "login"}
</li>
<li onClick={() => router.push(`/orders`)}>Orders</li>
</ul>
</span>
</header>
Expand Down
11 changes: 11 additions & 0 deletions apps/nextjs/src/components/loginContent/LoginContent.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
height: 40px;
background-color: rgb(211, 211, 211);
border-radius: 3px;
margin-top: 10px;
}
.container button:active {
background-color: darkgrey;
Expand All @@ -43,3 +44,13 @@
.container p a {
color: rgb(64, 64, 241);
}
.errorMsg {
margin-bottom: 10px;
--tw-border-opacity: 1;
border-color: rgb(248 113 113 / var(--tw-border-opacity));
--tw-bg-opacity: 1;
background-color: rgb(254 242 242 / var(--tw-bg-opacity));
padding: 0.5rem;
--tw-text-opacity: 1;
color: rgb(153 27 27 / var(--tw-text-opacity));
}
56 changes: 51 additions & 5 deletions apps/nextjs/src/components/loginContent/LoginContent.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,74 @@
import styles from "./LoginContent.module.css";
import { useRouter } from "next/router";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { useSignIn } from "@clerk/nextjs";
import { useState } from "react";

const signInFormValidator = z.object({
email: z.string().email(),
password: z.string().min(1, "Password cannot be empty"),
});

export type SignInFormSchema = Zod.infer<typeof signInFormValidator>;
function LoginContent() {
const router = useRouter();
const { register, handleSubmit, setError, formState } =
useForm<SignInFormSchema>({
resolver: zodResolver(signInFormValidator),
mode: "onBlur",
});
const { signIn, setActive } = useSignIn();
const [isLoading, setIsLoading] = useState(false);

const onSubmit = handleSubmit(async (vals) => {
if (!signIn) return;
setIsLoading(true);

try {
const result = await signIn.create({
identifier: vals.email,
password: vals.password,
});

if (result?.status === "complete") {
setIsLoading(false);
setActive({ session: result.createdSessionId });
}
} catch (err) {
setIsLoading(false);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
setError("root", { message: err.errors[0].message });
}
});
return (
<>
<main className={styles.container}>
<h1> Sign In</h1>
<form action="" method="post">
<h1>Sign In</h1>
{formState.errors.root && (
<p className={styles.errorMsg}>{formState.errors.root?.message}</p>
)}

<form onSubmit={onSubmit}>
<label htmlFor="email">Email</label>
<input
type="email"
placeholder="[email protected]"
name="email"
required
{...register("email")}
/>
<label htmlFor="psw">Password</label>
<input
type="password"
placeholder="Enter Password"
name="psw"
required
{...register("password")}
/>
<button disabled={isLoading}>Sign In</button>
</form>
<button>Sign In</button>

<p>
Dont have an account?{" "}
<a onClick={() => router.push(`/register`)}>Register</a>
Expand Down
52 changes: 52 additions & 0 deletions apps/nextjs/src/components/ordersContent/OrdersContent.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.container {
margin: 15%;
margin-top: 5%;
}
.container h1 {
font-size: x-large;
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.orderItem {
border: 3px solid black;
border-radius: 10px;
height: 100px;
display: flex;
justify-content: space-between;
padding-left: 2rem;
padding-right: 2rem;
margin-bottom: 10px;
}
.orderId {
display: flex;
align-items: center;
font-weight: bold;
}
.itemName {
display: flex;
align-items: center;
}
.clientName {
display: flex;
align-items: center;
}
.quantity {
display: flex;
align-items: center;
}
.price {
display: flex;
align-items: center;
}
.deleteBtn {
margin-top: 25px;
margin-bottom: 25px;
background-color: rgb(51, 49, 49);
color: white;
border-radius: 5px;
width: 100px;
}
.deleteBtn:active {
background-color: gray;
}
Loading

0 comments on commit 826a94c

Please sign in to comment.