Skip to content

Commit

Permalink
Merge pull request #109 from Kinfe123/comp-fix
Browse files Browse the repository at this point in the history
fix: loading state happens on both of button when either of the btn p…
  • Loading branch information
sadmann7 authored Jan 14, 2024
2 parents 5315cba + 37e4f43 commit 1169fe5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/app/(dashboard)/dashboard/stores/[storeId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ export default async function UpdateStorePage({
/>
</div>
<div className="flex flex-col gap-2 xs:flex-row">
<LoadingButton>
<LoadingButton action='update'>
Update store
<span className="sr-only">Update store</span>
</LoadingButton>
<LoadingButton
formAction={deleteStore.bind(null, storeId)}
variant="destructive"
action='delete'
>
Delete store
<span className="sr-only">Delete store</span>
Expand Down
6 changes: 3 additions & 3 deletions src/components/file-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function FileCard({ i, file, files, setFiles }: FileCardProps) {
}, [onCrop])

return (
<div className="relative flex items-center justify-between gap-2.5">
<div className="relative flex items-center justify-between gap-10">
<div className="flex items-center gap-2">
<Image
src={cropData ? cropData : file.preview}
Expand All @@ -264,7 +264,7 @@ function FileCard({ i, file, files, setFiles }: FileCardProps) {
/>
<div className="flex flex-col">
<p className="line-clamp-1 text-sm font-medium text-muted-foreground">
{file.name}
{file.name.slice(0 , 45)}
</p>
<p className="text-xs text-slate-500">
{(file.size / 1024 / 1024).toFixed(2)}MB
Expand Down Expand Up @@ -352,7 +352,7 @@ function FileCard({ i, file, files, setFiles }: FileCardProps) {
setFiles(files.filter((_, j) => j !== i))
}}
>
<Cross2Icon className="h-4 w-4" aria-hidden="true" />
<Cross2Icon className="h-4 w-4 " aria-hidden="true" />
<span className="sr-only">Remove file</span>
</Button>
</div>
Expand Down
26 changes: 23 additions & 3 deletions src/components/loading-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client"

import * as React from "react"
import { useState } from "react"
import { useFormStatus } from "react-dom"

import { cn } from "@/lib/utils"
Expand All @@ -13,9 +14,15 @@ import {
import { Skeleton } from "@/components/ui/skeleton"
import { Icons } from "@/components/icons"

const LoadingButton = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ children, className, variant, size, ...props }, ref) => {
type ButtonActionProps = ButtonProps & {
action: string
}

const LoadingButton = React.forwardRef<HTMLButtonElement, ButtonActionProps>(
({ children, className, variant, size, action, ...props }, ref) => {
const { pending } = useFormStatus()
const [del, setDel] = useState(false)
const [update, setUpdate] = useState(false)
const mounted = useMounted()

if (!mounted)
Expand All @@ -36,8 +43,21 @@ const LoadingButton = React.forwardRef<HTMLButtonElement, ButtonProps>(
ref={ref}
disabled={pending}
{...props}
onClick={() => {
if (action === "update") {
setUpdate(true)
} else {
setDel(true)
}
}}
>
{pending && (
{del && pending && (
<Icons.spinner
className="mr-2 h-4 w-4 animate-spin"
aria-hidden="true"
/>
)}
{update && pending && (
<Icons.spinner
className="mr-2 h-4 w-4 animate-spin"
aria-hidden="true"
Expand Down

1 comment on commit 1169fe5

@vercel
Copy link

@vercel vercel bot commented on 1169fe5 Jan 14, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.