"use client" import { useRouter } from "next/navigation" import { useIntl } from "react-intl" import { toast } from "@scandic-hotels/design-system/Toast" import { trpc } from "@scandic-hotels/trpc/client" export function useUpdateProfilingConsent() { const intl = useIntl() const utils = trpc.useUtils() const router = useRouter() const updateConsent = trpc.user.profilingConsent.update.useMutation({ onSuccess: async () => { await utils.user.get.invalidate() router.refresh() setTimeout(() => { toast.success( intl.formatMessage({ id: "profilingConsent.alert.updateConsentSuccessful", defaultMessage: "Preference saved!", }) ) }) }, onError: () => { setTimeout(() => { toast.error( intl.formatMessage({ id: "profilingConsent.alert.updateConsentFailed", defaultMessage: "An error occurred when updating preferences, please try again later.", }) ) }) }, }) const initiateUpdateConsent = (consent: boolean) => { updateConsent.mutate({ profilingConsent: consent }) } return { initiateUpdateConsent, isLoading: updateConsent.isPending, isSuccess: updateConsent.isSuccess, isError: updateConsent.isError, } }