-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated checkout flow, to work with guest user
- Loading branch information
Showing
29 changed files
with
1,464 additions
and
282 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/app/(client)/checkout/_components/AuthenticationStep.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
"use client"; | ||
|
||
import React from 'react'; | ||
import { | ||
Card, | ||
CardContent, | ||
CardDescription, | ||
CardHeader, | ||
CardTitle, | ||
} from "@/components/ui/card"; | ||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/components/ui/form"; | ||
import { useForm } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import * as z from "zod"; | ||
import { useAuth } from '@/context/AuthContext'; | ||
import { useCheckout } from '@/context/CheckoutContext'; | ||
import { useToast } from '@/hooks/use-toast'; | ||
|
||
const loginSchema = z.object({ | ||
email: z.string().email(), | ||
password: z.string().min(6, "Password must be at least 6 characters"), | ||
}); | ||
|
||
export function AuthenticationStep() { | ||
const { isAuthenticated, login } = useAuth(); | ||
const { setStep, setIsGuest } = useCheckout(); | ||
const { toast } = useToast(); | ||
const [isLoading, setIsLoading] = React.useState(false); | ||
|
||
const form = useForm<z.infer<typeof loginSchema>>({ | ||
resolver: zodResolver(loginSchema), | ||
}); | ||
|
||
console.log(isAuthenticated); | ||
|
||
if (isAuthenticated) { | ||
setStep('information'); | ||
return null; | ||
} | ||
|
||
const handleLogin = async (data: z.infer<typeof loginSchema>) => { | ||
try { | ||
setIsLoading(true); | ||
await login(data); | ||
setIsGuest(false); | ||
setStep('information'); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
toast({ | ||
title: "Login failed", | ||
description: "Invalid email or password", | ||
variant: "destructive", | ||
}); | ||
} | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
const handleGuestCheckout = () => { | ||
setIsGuest(true); | ||
setStep('information'); | ||
}; | ||
|
||
return ( | ||
<Card> | ||
<CardHeader> | ||
<CardTitle>Sign In</CardTitle> | ||
<CardDescription> | ||
Sign in to your account or continue as a guest | ||
</CardDescription> | ||
</CardHeader> | ||
<CardContent className="space-y-6"> | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(handleLogin)} className="space-y-4"> | ||
<FormField | ||
control={form.control} | ||
name="email" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Email</FormLabel> | ||
<FormControl> | ||
<Input type="email" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<FormField | ||
control={form.control} | ||
name="password" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Password</FormLabel> | ||
<FormControl> | ||
<Input type="password" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<Button type="submit" className="w-full" disabled={isLoading}> | ||
{isLoading ? "Signing in..." : "Sign In"} | ||
</Button> | ||
</form> | ||
</Form> | ||
|
||
<div className="relative"> | ||
<div className="absolute inset-0 flex items-center"> | ||
<div className="w-full border-t border-gray-200" /> | ||
</div> | ||
<div className="relative flex justify-center text-sm"> | ||
<span className="px-2 bg-background text-muted-foreground"> | ||
Or continue with | ||
</span> | ||
</div> | ||
</div> | ||
|
||
<Button | ||
variant="outline" | ||
className="w-full" | ||
onClick={handleGuestCheckout} | ||
disabled={isLoading} | ||
> | ||
Continue as Guest | ||
</Button> | ||
</CardContent> | ||
</Card> | ||
); | ||
} |
Oops, something went wrong.