{"version":3,"sources":["../../../src/hooks/useCreateSubscription.ts"],"sourcesContent":["import { CreateSubscriptionArgs, NovuError, TopicSubscription } from '@novu/js';\nimport { useCallback, useRef, useState } from 'react';\nimport { useNovu } from './NovuProvider';\n\nexport type UseCreateSubscriptionProps = {\n  onSuccess?: (data: TopicSubscription) => void;\n  onError?: (error: NovuError) => void;\n};\n\nexport type UseCreateSubscriptionResult = {\n  isCreating: boolean;\n  error?: NovuError;\n  create: (args: CreateSubscriptionArgs) => Promise<{\n    data?: TopicSubscription | undefined;\n    error?: NovuError | undefined;\n  }>;\n};\n\nexport const useCreateSubscription = (props: UseCreateSubscriptionProps = {}): UseCreateSubscriptionResult => {\n  const propsRef = useRef<UseCreateSubscriptionProps>(props);\n  propsRef.current = props;\n  const novu = useNovu();\n  const [isCreating, setIsCreating] = useState(false);\n  const [error, setError] = useState<NovuError>();\n\n  const create = useCallback(\n    async (args: CreateSubscriptionArgs) => {\n      const { onSuccess, onError } = propsRef.current;\n      setError(undefined);\n      setIsCreating(true);\n\n      const response = await novu.subscriptions.create(args);\n\n      setIsCreating(false);\n\n      if (response.error) {\n        setError(response.error);\n        onError?.(response.error);\n      } else if (response.data) {\n        onSuccess?.(response.data);\n      }\n\n      return response;\n    },\n    [novu]\n  );\n\n  return {\n    create,\n    isCreating,\n    error,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAA8C;AAC9C,0BAAwB;AAgBjB,IAAM,wBAAwB,CAAC,QAAoC,CAAC,MAAmC;AAC5G,QAAM,eAAW,qBAAmC,KAAK;AACzD,WAAS,UAAU;AACnB,QAAM,WAAO,6BAAQ;AACrB,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,KAAK;AAClD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAoB;AAE9C,QAAM,aAAS;AAAA,IACb,OAAO,SAAiC;AACtC,YAAM,EAAE,WAAW,QAAQ,IAAI,SAAS;AACxC,eAAS,MAAS;AAClB,oBAAc,IAAI;AAElB,YAAM,WAAW,MAAM,KAAK,cAAc,OAAO,IAAI;AAErD,oBAAc,KAAK;AAEnB,UAAI,SAAS,OAAO;AAClB,iBAAS,SAAS,KAAK;AACvB,2CAAU,SAAS;AAAA,MACrB,WAAW,SAAS,MAAM;AACxB,+CAAY,SAAS;AAAA,MACvB;AAEA,aAAO;AAAA,IACT;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}