Skip to content

Commit

Permalink
💄 feat (library): Now displays loading state when signing up
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Mar 4, 2024
1 parent 48fe3f7 commit 3faf54d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/library/src/app/(auth)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function RootLayout({
export default function AuthLayout({
children,
}: {
children: React.ReactNode;
Expand Down
18 changes: 14 additions & 4 deletions apps/library/src/app/(auth)/signup/signup-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { useFormState } from "react-dom";
import { Loader2 } from "lucide-react";
import { useFormState, useFormStatus } from "react-dom";

import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
Expand All @@ -24,9 +25,18 @@ export default function SignupForm() {
<Input type="password" name="password" id="password" />
</div>
{formState && <p className="text-sm text-red-500">{formState.error}</p>}
<Button type="submit" className="mt-4 w-full">
Sign up
</Button>
<SubmitButton />
</form>
);
}

function SubmitButton() {
const { pending } = useFormStatus();

return (
<Button disabled={pending} type="submit" className="mt-4 w-full">
{pending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Sign up
</Button>
);
}
17 changes: 12 additions & 5 deletions apps/library/src/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ export async function signup(

// TODO: check if username is already used

await db.insert(user).values({
id: userId,
username: username,
hashedPassword: hashedPassword,
});
try {
await db.insert(user).values({
id: userId,
username: username,
hashedPassword: hashedPassword,
});
} catch (e) {
console.log("Error adding to DB, user already exists...", e);
return {
error: "User already exists",
};
}

console.log("Added to DB...");

Expand Down

0 comments on commit 3faf54d

Please sign in to comment.