2025-05-28 23:08:42 +00:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
|
import { Clock } from "lucide-react"
|
|
|
|
|
import { useState } from "react"
|
|
|
|
|
import { useToast } from "@/components/hooks/use-toast"
|
|
|
|
|
import { createAccountRequest } from "@/actions"
|
|
|
|
|
import { isServiceError } from "@/lib/utils"
|
2025-07-15 03:14:41 +00:00
|
|
|
import { useRouter } from "next/navigation"
|
2025-05-28 23:08:42 +00:00
|
|
|
|
2025-07-15 03:14:41 +00:00
|
|
|
interface SubmitButtonProps {
|
2025-05-28 23:08:42 +00:00
|
|
|
domain: string
|
|
|
|
|
userId: string
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 03:14:41 +00:00
|
|
|
export function SubmitAccountRequestButton({ domain, userId }: SubmitButtonProps) {
|
2025-05-28 23:08:42 +00:00
|
|
|
const { toast } = useToast()
|
2025-07-15 03:14:41 +00:00
|
|
|
const router = useRouter()
|
2025-05-28 23:08:42 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
const result = await createAccountRequest(userId, domain)
|
|
|
|
|
if (!isServiceError(result)) {
|
|
|
|
|
if (result.existingRequest) {
|
|
|
|
|
toast({
|
|
|
|
|
title: "Request Already Submitted",
|
|
|
|
|
description: "Your request to join the organization has already been submitted. Please wait for it to be approved.",
|
|
|
|
|
variant: "default",
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
2025-07-15 03:14:41 +00:00
|
|
|
title: "Request Submitted",
|
|
|
|
|
description: "Your request to join the organization has been submitted.",
|
2025-05-28 23:08:42 +00:00
|
|
|
variant: "default",
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-07-15 03:14:41 +00:00
|
|
|
// Refresh the page to trigger layout re-render and show PendingApprovalCard
|
|
|
|
|
router.refresh()
|
2025-05-28 23:08:42 +00:00
|
|
|
} else {
|
|
|
|
|
toast({
|
2025-07-15 03:14:41 +00:00
|
|
|
title: "Failed to Submit",
|
|
|
|
|
description: `There was an error submitting your request. Reason: ${result.message}`,
|
2025-05-28 23:08:42 +00:00
|
|
|
variant: "destructive",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form onSubmit={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleSubmit();
|
|
|
|
|
}}>
|
|
|
|
|
<input type="hidden" name="domain" value={domain} />
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="flex items-center gap-2"
|
|
|
|
|
variant="outline"
|
|
|
|
|
disabled={isSubmitting}
|
|
|
|
|
>
|
|
|
|
|
<Clock className="h-4 w-4" />
|
2025-07-15 03:14:41 +00:00
|
|
|
{isSubmitting ? "Submitting..." : "Submit Request"}
|
2025-05-28 23:08:42 +00:00
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
)
|
|
|
|
|
}
|