Skip to content

Commit 6961238

Browse files
committed
loader adder on signup & signin btn
1 parent 0265438 commit 6961238

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

client/src/pages/Blogs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const Blogs =() => {
2929
setblogs(res.data.bulk);
3030
setloader(false);
3131

32-
}, 1500);
32+
}, 100);
3333

3434
} catch (error) {
3535
console.log(error)

client/src/pages/Signin.tsx

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const serverurl = backendUrl
1616
console.log(serverurl)
1717

1818
export default function Signin() {
19+
const [loading,setLoading] = useState(false)
1920
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
2021
e.preventDefault();
2122
console.log("Form submitted");
@@ -74,32 +75,44 @@ const [Signininput, useSignininput] = useState<Signininput>({
7475
className="bg-gradient-to-br relative group/btn from-black dark:from-zinc-900 dark:to-zinc-900 to-neutral-600 block dark:bg-zinc-800 w-full text-white rounded-md h-10 font-medium shadow-[0px_1px_0px_0px_#ffffff40_inset,0px_-1px_0px_0px_#ffffff40_inset] dark:shadow-[0px_1px_0px_0px_var(--zinc-800)_inset,0px_-1px_0px_0px_var(--zinc-800)_inset]"
7576
type="submit"
7677
onClick={async () => {
77-
try {
78-
const res = await axios.post(`${serverurl}/user/signin`, {
79-
email: Signininput.email,
80-
password: Signininput.password,
81-
});
82-
const token = res.data.token;
83-
if (token) {
84-
localStorage.setItem("token", token);
85-
toast.success(res.data.msg,{position:"top-right",theme:"dark",autoClose:2000})
86-
setTimeout(() => {
87-
navigate("/");
88-
89-
}, 1000);
90-
} else {
91-
toast.error(res.data.msg,{position:"top-right",theme:"dark",autoClose:2000})
92-
console.log(res.data.msg);
93-
}
94-
95-
} catch (error: any) {
96-
console.log(error)
97-
toast.error(error.response.data.msg,{position:"top-right",theme:"dark",autoClose:2000})
98-
99-
}
78+
setLoading(true);
79+
try {
80+
const res = await axios.post(`${serverurl}/user/signin`, {
81+
email: Signininput.email,
82+
password: Signininput.password,
83+
});
84+
const token = res.data.token;
85+
if (token) {
86+
localStorage.setItem("token", token);
87+
toast.success(res.data.msg, {
88+
position: "top-right",
89+
theme: "dark",
90+
autoClose: 2000,
91+
});
92+
setTimeout(() => {
93+
navigate("/");
94+
}, 300);
95+
} else {
96+
toast.error(res.data.msg, {
97+
position: "top-right",
98+
theme: "dark",
99+
autoClose: 2000,
100+
});
101+
console.log(res.data.msg);
102+
}
103+
} catch (error: any) {
104+
console.log(error);
105+
toast.error(error.response.data.msg[0], {
106+
position: "top-right",
107+
theme: "dark",
108+
autoClose: 2000,
109+
});
110+
} finally {
111+
setLoading(false);
112+
}
100113
}}
101114
>
102-
Sign in &rarr;
115+
{loading ? "wait..." : "Sign in"} &rarr;
103116
<BottomGradient />
104117
</button>
105118

client/src/pages/Signup.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ShineBorder from "../components/effects/shineborder";
2-
import React from "react";
2+
import React, { useState } from "react";
33
import { Label } from "../components/ui/label";
44
import { Input } from "../components/ui/input";
55
import { cn } from "../components/utils/cn";
@@ -41,6 +41,7 @@ export default function Signup() {
4141

4242
const navigate = useNavigate();
4343
const [Signupinput, useSignupinput] = useRecoilState<Signupinput>(UserAtom);
44+
const [loading,Setloading] = useState(false)
4445

4546
console.log(Signupinput)
4647

@@ -120,6 +121,7 @@ const handlesubmit = async(e: React.FormEvent<HTMLFormElement>) => {
120121
className="bg-gradient-to-br relative group/btn from-black dark:from-zinc-900 dark:to-zinc-900 to-neutral-600 block dark:bg-zinc-800 w-full text-white rounded-md h-10 font-medium shadow-[0px_1px_0px_0px_#ffffff40_inset,0px_-1px_0px_0px_#ffffff40_inset] dark:shadow-[0px_1px_0px_0px_var(--zinc-800)_inset,0px_-1px_0px_0px_var(--zinc-800)_inset]"
121122
type="submit"
122123
onClick={async()=>{
124+
Setloading(true)
123125
try {
124126
const res = await axios.post(`${serverurl}/user/signup`,{
125127
email: Signupinput.email,
@@ -134,7 +136,7 @@ const handlesubmit = async(e: React.FormEvent<HTMLFormElement>) => {
134136
setTimeout(() => {
135137
navigate("/");
136138

137-
}, 1000);
139+
}, 300);
138140
}else if(res.data.status === 404){
139141
toast.warning<toastMsgprops>(res.data.msg,{position: "top-right",theme:"dark",autoClose:2000})
140142
console.log(res.data.msg)
@@ -146,14 +148,16 @@ const handlesubmit = async(e: React.FormEvent<HTMLFormElement>) => {
146148
toast.error<toastMsgprops>(error.response.data.msg[0],{position: "top-right",theme:"dark",autoClose:2000})
147149
}
148150

149-
toast.error<toastMsgprops>(error.response.data.msg,{position: "top-right",theme:"dark",autoClose:2000})
151+
toast.error<toastMsgprops>(error.response.data.msg[0],{position: "top-right",theme:"dark",autoClose:2000})
150152

153+
}finally{
154+
Setloading(false)
151155
}
152156
}}
153157

154158

155159
>
156-
Sign up &rarr;
160+
{loading ? "loading..." : "Sign-up"}&rarr;
157161
<BottomGradient />
158162
</button>
159163

server/src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import z from "zod";
44
export const signupinput = z.object({
55
email: z
66
.string()
7-
.min(5, { message: "username of min 5 character" })
7+
.min(5, { message: "Provide valid email" })
88
.email({ message: "Invalid email address" }),
99
password: z.string().min(6, { message: "Password must be 6 or more characters long" }),
1010
firstname: z
@@ -17,13 +17,13 @@ export const signupinput = z.object({
1717
export const signininput = z.object({
1818
email: z
1919
.string()
20-
.min(5, { message: "username of min 5 character" })
20+
.min(5, { message: "Provaide Valid email" })
2121
.email({ message: "Invalid email address" }),
2222
password: z.string().min(6, { message: "password must be 6 or more characters long" }),
2323
});
2424

2525
export const blogsinput = z.object({
26-
title: z.string().min(1, { message: "username of min 1 character" }),
26+
title: z.string().min(1, { message: "title should be of min 1 character" }),
2727
description: z.string().optional(),
2828
upvotes: z.number().optional(),
2929
});
@@ -33,7 +33,7 @@ export const blogsinput = z.object({
3333
export const updateblogsinput = z.object({
3434
title: z
3535
.string()
36-
.min(1, { message: "username of min 1 character" })
36+
.min(1, { message: "title should be of min 1 character" })
3737
.optional(),
3838
description: z.string().optional(),
3939
userid: z.number().optional(),

0 commit comments

Comments
 (0)