39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
'use client'
|
|
|
|
import { useRouter } from 'next/navigation'
|
|
import { useTransition } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { setActiveProductAction } from '@/actions/active-product'
|
|
|
|
interface Props {
|
|
productId: string
|
|
isDemo: boolean
|
|
/** Navigate here after activation. Omit to refresh the current page in place. */
|
|
redirectTo?: string
|
|
label?: string
|
|
}
|
|
|
|
export function ActivateProductButton({ productId, isDemo, redirectTo, label = 'Activeer' }: Props) {
|
|
const router = useRouter()
|
|
const [isPending, startTransition] = useTransition()
|
|
|
|
function handleActivate() {
|
|
if (isDemo) { toast.error('Niet beschikbaar in demo-modus'); return }
|
|
startTransition(async () => {
|
|
const result = await setActiveProductAction(productId)
|
|
if (result?.error) toast.error(typeof result.error === 'string' ? result.error : 'Activeren mislukt')
|
|
else if (redirectTo) router.push(redirectTo)
|
|
else router.refresh()
|
|
})
|
|
}
|
|
|
|
return (
|
|
<button
|
|
onClick={handleActivate}
|
|
disabled={isPending}
|
|
className="text-xs text-primary hover:underline font-medium disabled:opacity-50"
|
|
>
|
|
{label}
|
|
</button>
|
|
)
|
|
}
|