_id.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="flex flex-col items-center justify-between">
  3. <div
  4. v-if="invalidLink"
  5. class="flex flex-col items-center justify-center flex-1"
  6. >
  7. <i class="pb-2 opacity-75 material-icons">error_outline</i>
  8. <h1 class="text-center heading">
  9. {{ $t("error.invalid_link") }}
  10. </h1>
  11. <p class="mt-2 text-center">
  12. {{ $t("error.invalid_link_description") }}
  13. </p>
  14. </div>
  15. <div v-else class="flex flex-col items-center justify-center flex-1 p-4">
  16. <div
  17. v-if="shortcodeDetails.loading"
  18. class="flex flex-col items-center justify-center flex-1 p-4"
  19. >
  20. <SmartSpinner />
  21. </div>
  22. <div v-else>
  23. <div
  24. v-if="!shortcodeDetails.loading && E.isLeft(shortcodeDetails.data)"
  25. class="flex flex-col items-center p-4"
  26. >
  27. <i class="pb-2 opacity-75 material-icons">error_outline</i>
  28. <h1 class="text-center heading">
  29. {{ $t("error.invalid_link") }}
  30. </h1>
  31. <p class="mt-2 text-center">
  32. {{ $t("error.invalid_link_description") }}
  33. </p>
  34. <p class="mt-4">
  35. <ButtonSecondary to="/" svg="home" filled :label="$t('app.home')" />
  36. <ButtonSecondary
  37. svg="refresh-cw"
  38. :label="$t('app.reload')"
  39. filled
  40. @click.native="reloadApplication"
  41. />
  42. </p>
  43. </div>
  44. <div
  45. v-if="!shortcodeDetails.loading && E.isRight(shortcodeDetails.data)"
  46. class="flex flex-col items-center justify-center flex-1 p-4"
  47. >
  48. <h1 class="heading">
  49. {{ $t("state.loading") }}
  50. </h1>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. </template>
  56. <script lang="ts">
  57. import {
  58. defineComponent,
  59. useContext,
  60. useRoute,
  61. useRouter,
  62. watch,
  63. } from "@nuxtjs/composition-api"
  64. import * as E from "fp-ts/Either"
  65. import { safelyExtractRESTRequest } from "@hoppscotch/data"
  66. import { useGQLQuery } from "~/helpers/backend/GQLClient"
  67. import {
  68. ResolveShortcodeDocument,
  69. ResolveShortcodeQuery,
  70. ResolveShortcodeQueryVariables,
  71. } from "~/helpers/backend/graphql"
  72. import { getDefaultRESTRequest, setRESTRequest } from "~/newstore/RESTSession"
  73. export default defineComponent({
  74. setup() {
  75. const route = useRoute()
  76. const router = useRouter()
  77. const { localePath } = useContext() as any
  78. const shortcodeDetails = useGQLQuery<
  79. ResolveShortcodeQuery,
  80. ResolveShortcodeQueryVariables,
  81. ""
  82. >({
  83. query: ResolveShortcodeDocument,
  84. variables: {
  85. code: route.value.params.id as string,
  86. },
  87. })
  88. watch(
  89. () => shortcodeDetails.data,
  90. () => {
  91. if (shortcodeDetails.loading) return
  92. const data = shortcodeDetails.data
  93. if (E.isRight(data)) {
  94. const request: unknown = JSON.parse(
  95. data.right.shortcode?.request as string
  96. )
  97. setRESTRequest(
  98. safelyExtractRESTRequest(request, getDefaultRESTRequest())
  99. )
  100. router.push({ path: localePath("/") })
  101. }
  102. }
  103. )
  104. const reloadApplication = () => {
  105. window.location.reload()
  106. }
  107. return {
  108. E,
  109. shortcodeDetails,
  110. reloadApplication,
  111. }
  112. },
  113. data() {
  114. return {
  115. invalidLink: false,
  116. shortcodeID: "",
  117. }
  118. },
  119. mounted() {
  120. if (typeof this.$route.params.id === "string") {
  121. this.shortcodeID = this.$route.params.id
  122. }
  123. this.invalidLink = !this.shortcodeID
  124. },
  125. })
  126. </script>