@@ -7,10 +7,48 @@ import { Input } from "@/components/ui/input"
77import { Label } from "@/components/ui/label"
88import { ShoppingCart , Eye , EyeOff , MapPin } from "lucide-react"
99import { useState } from "react"
10+ import axios , { AxiosError } from "axios"
11+ import { useRouter } from "next/navigation"
1012
1113export default function SignupPage ( ) {
1214 const [ showPassword , setShowPassword ] = useState ( false )
1315
16+ const router = useRouter ( )
17+ const [ form , setForm ] = useState ( {
18+ firstName : "" ,
19+ lastName : "" ,
20+ email : "" ,
21+ password : "" ,
22+ postcode : "" ,
23+ agreedToTerms : false
24+ } )
25+
26+ const handleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
27+ const { id, value, type, checked } = e . target
28+ setForm ( ( prev ) => ( {
29+ ...prev ,
30+ [ id ] : type === "checkbox" ? checked : value
31+ } ) )
32+ }
33+
34+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
35+ e . preventDefault ( )
36+ if ( ! form . agreedToTerms ) {
37+ alert ( "You must agree to the Terms and Privacy Policy." )
38+ return
39+ }
40+
41+ try {
42+ await axios . post ( "http://localhost:8080/api/signup" , form )
43+ router . push ( "/dashboard" )
44+ } catch ( err ) {
45+ console . error ( "Signup failed" , err )
46+ const axiosError = err as AxiosError < { message : string } > ;
47+ const errorMessage = axiosError . response ?. data ?. message || "Unknown error"
48+ alert ( "Signup failed: " + errorMessage )
49+ }
50+ }
51+
1452 return (
1553 < div className = "min-h-screen flex items-center justify-center bg-gradient-to-b from-green-50 to-white p-4" >
1654 < div className = "w-full max-w-md" >
@@ -31,21 +69,21 @@ export default function SignupPage() {
3169 < div className = "grid grid-cols-2 gap-4" >
3270 < div className = "space-y-2" >
3371 < Label htmlFor = "firstName" > First Name</ Label >
34- < Input id = "firstName" placeholder = "John" />
72+ < Input id = "firstName" value = { form . firstName } onChange = { handleChange } placeholder = "John" />
3573 </ div >
3674 < div className = "space-y-2" >
3775 < Label htmlFor = "lastName" > Last Name</ Label >
38- < Input id = "lastName" placeholder = "Smith" />
76+ < Input id = "lastName" value = { form . lastName } onChange = { handleChange } placeholder = "Smith" />
3977 </ div >
4078 </ div >
4179 < div className = "space-y-2" >
4280 < Label htmlFor = "email" > Email</ Label >
43- < Input id = "email" type = "email" placeholder = "john@example.com" />
81+ < Input id = "email" type = "email" value = { form . email } onChange = { handleChange } placeholder = "john@example.com" />
4482 </ div >
4583 < div className = "space-y-2" >
4684 < Label htmlFor = "password" > Password</ Label >
4785 < div className = "relative" >
48- < Input id = "password" type = { showPassword ? "text" : "password" } placeholder = "Create a strong password" />
86+ < Input id = "password" type = { showPassword ? "text" : "password" } value = { form . password } onChange = { handleChange } placeholder = "Create a strong password" />
4987 < Button
5088 type = "button"
5189 variant = "ghost"
@@ -61,12 +99,18 @@ export default function SignupPage() {
6199 < Label htmlFor = "postcode" > Postcode</ Label >
62100 < div className = "relative" >
63101 < MapPin className = "absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
64- < Input id = "postcode" placeholder = "2000" className = "pl-10" />
102+ < Input id = "postcode" placeholder = "2000" value = { form . postcode } onChange = { handleChange } className = "pl-10" />
65103 </ div >
66104 < p className = "text-xs text-muted-foreground" > We'll show you prices for stores in your area</ p >
67105 </ div >
68106 < div className = "flex items-center space-x-2" >
69- < input type = "checkbox" id = "terms" className = "rounded" />
107+ < input
108+ type = "checkbox"
109+ id = "agreedToTerms"
110+ className = "rounded"
111+ checked = { form . agreedToTerms }
112+ onChange = { handleChange }
113+ />
70114 < Label htmlFor = "terms" className = "text-sm" >
71115 I agree to the{ " " }
72116 < Link href = "/terms" className = "text-green-600 hover:underline" >
@@ -79,7 +123,11 @@ export default function SignupPage() {
79123 </ Label >
80124 </ div >
81125 < Link href = "/dashboard" >
82- < Button className = "w-full bg-green-600 hover:bg-green-700" > Create Account</ Button >
126+ < form onSubmit = { handleSubmit } className = "w-full" >
127+ < Button type = "submit" className = "w-full bg-green-600 hover:bg-green-700" >
128+ Create Account
129+ </ Button >
130+ </ form >
83131 </ Link >
84132 < div className = "text-center text-sm" >
85133 Already have an account?{ " " }
0 commit comments