Skip to content

Commit

Permalink
feat(docs): add loading state on generate
Browse files Browse the repository at this point in the history
  • Loading branch information
ubinatus committed Jan 9, 2024
1 parent aa1ac77 commit f9963bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
18 changes: 9 additions & 9 deletions apps/docs/components/landing/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { useRef, useState, type ReactNode } from "react";
import { CopyCode } from "@/components/copy-code";
import { QRForm } from "@/components/qr-form";
import { Button } from "@/components/ui/button";
Expand Down Expand Up @@ -59,14 +59,8 @@ function SiteCards() {
// Ref
const awesomeQRCodeRef = useRef<AwesomeQRCodeRef>(null);
// State
const [isLoading, setIsLoading] = useState(false);
const [options, setOptions] = useState<AwesomeQRCodeProps>(defaultValues);
// Effects
useEffect(() => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}, [options]);
// Downloading the QRCode
function download() {
if (awesomeQRCodeRef.current) {
Expand All @@ -81,6 +75,12 @@ function SiteCards() {
}
}
}
async function generate(opts: AwesomeQRCodeProps) {
setIsLoading(true);
await new Promise((resolve) => setTimeout(resolve, 300));
setOptions(opts);
setIsLoading(false);
}
return (
<div className="lg:!my-15 my-8 flex w-full flex-col items-center justify-center gap-6 px-6 sm:mx-0 md:!my-14 md:mb-0 lg:!translate-y-0 lg:!flex-row">
<Card>
Expand All @@ -92,7 +92,7 @@ function SiteCards() {
</div>
</Card>
<div className="relative max-w-lg">
<QRForm setOptions={(val) => setOptions(val)} />
<QRForm setOptions={generate} isLoading={isLoading} />
</div>
</div>
);
Expand Down
12 changes: 9 additions & 3 deletions apps/docs/components/qr-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ import {
type FormValues,
} from "@/lib/form";
import { zodResolver } from "@hookform/resolvers/zod";
import { LoaderIcon } from "lucide-react";
import { useForm } from "react-hook-form";

import { type AwesomeQRCodeProps } from "@awesome-qrcode/react";

type Props = {
setOptions: (opts: AwesomeQRCodeProps) => void;
isLoading: boolean;
};

export function QRForm({ setOptions }: Props) {
export function QRForm({ setOptions, isLoading }: Props) {
// Form
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
Expand Down Expand Up @@ -192,8 +194,12 @@ export function QRForm({ setOptions }: Props) {
/>
</div>
<div className="flex flex-col gap-3 md:flex-row">
<Button type="submit" className="!bg-primary hover:!bg-primary/90">
Generate
<Button
disabled={isLoading}
type="submit"
className="min-w-[120px] !bg-primary hover:!bg-primary/90"
>
{isLoading ? <LoaderIcon className="animate-spin" /> : "Generate"}
</Button>
<div className="text-sm text-muted-foreground dark:text-muted">
Simple usage of @awesome-qrcode/react.
Expand Down

1 comment on commit f9963bd

@vercel
Copy link

@vercel vercel bot commented on f9963bd Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

awesome-qrcode – ./

awesome-qrcode-ubinatus.vercel.app
awesome-qrcode-git-main-ubinatus.vercel.app
awesome-qrcode.vercel.app

Please sign in to comment.