| 1 | import { useQueryClient } from "@tanstack/react-query"; |
| 2 | import { useState } from "react"; |
| 3 | import { toast } from "sonner"; |
| 4 | |
| 5 | import { createFontPair } from "@/app/_actions/presentation/font-pair-actions"; |
| 6 | |
| 7 | interface FontPairData { |
| 8 | heading: string; |
| 9 | body: string; |
| 10 | headingUrl?: string; |
| 11 | bodyUrl?: string; |
| 12 | } |
| 13 | |
| 14 | export function useSaveFontPair(onSuccess?: () => void) { |
| 15 | const [isSaving, setIsSaving] = useState(false); |
| 16 | const queryClient = useQueryClient(); |
| 17 | |
| 18 | const saveFontPair = async (data: FontPairData) => { |
| 19 | const { heading, body, headingUrl, bodyUrl } = data; |
| 20 | |
| 21 | if (!heading || !body) { |
| 22 | toast.error("Please select both heading and body fonts"); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | setIsSaving(true); |
| 27 | try { |
| 28 | const result = await createFontPair({ |
| 29 | heading, |
| 30 | headingUrl, |
| 31 | body, |
| 32 | bodyUrl, |
| 33 | }); |
| 34 | |
| 35 | if (result.success) { |
| 36 | toast.success("Font pair saved successfully"); |
| 37 | await queryClient.invalidateQueries({ queryKey: ["userFontPairs"] }); |
| 38 | onSuccess?.(); |
| 39 | return true; |
| 40 | } else { |
| 41 | toast.error(result.message || "Failed to save font pair"); |
| 42 | return false; |
| 43 | } |
| 44 | } catch (error) { |
| 45 | console.error("Error saving font pair:", error); |
| 46 | toast.error("Failed to save font pair"); |
| 47 | return false; |
| 48 | } finally { |
| 49 | setIsSaving(false); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | return { isSaving, saveFontPair }; |
| 54 | } |
| 55 |