useTicketArticleRetryMediaDownload.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref, type Ref } from 'vue'
  3. import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
  4. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  5. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  6. import { useTicketArticleRetryMediaDownloadMutation } from '../graphql/mutations/ticketArticleRetryMediaDownload.api.ts'
  7. export const useTicketArticleRetryMediaDownload = (articleId: Ref<string>) => {
  8. const retryMutation = new MutationHandler(
  9. useTicketArticleRetryMediaDownloadMutation(() => ({
  10. variables: {
  11. articleId: articleId.value,
  12. },
  13. })),
  14. )
  15. const { notify } = useNotifications()
  16. const loading = ref(false)
  17. const tryAgain = async () => {
  18. loading.value = true
  19. try {
  20. const result = await retryMutation.send()
  21. if (!result?.ticketArticleRetryMediaDownload?.success) throw new Error()
  22. notify({
  23. id: 'media-download-success',
  24. type: NotificationTypes.Success,
  25. message: __('Media download was successful.'),
  26. })
  27. return Promise.resolve()
  28. } catch (error) {
  29. notify({
  30. id: 'media-download-failed',
  31. type: NotificationTypes.Error,
  32. message: __('Media download failed. Please try again later.'),
  33. })
  34. return Promise.reject(error)
  35. } finally {
  36. loading.value = false
  37. }
  38. }
  39. return {
  40. loading,
  41. tryAgain,
  42. }
  43. }