ViewApiRequest.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <modal title="View API Request">
  3. <template #body>
  4. <div v-if="!loaded" class="vertical-padding">
  5. <p>Request hasn't loaded yet</p>
  6. </div>
  7. <div v-else class="vertical-padding">
  8. <p><b>ID:</b> {{ request._id }}</p>
  9. <p><b>URL:</b> {{ request.url }}</p>
  10. <div>
  11. <b>Params:</b>
  12. <ul v-if="request.params">
  13. <li
  14. v-for="[paramKey, paramValue] in Object.entries(
  15. request.params
  16. )"
  17. :key="paramKey"
  18. >
  19. <b>{{ paramKey }}</b
  20. >: {{ paramValue }}
  21. </li>
  22. </ul>
  23. <span v-else>None/Not found</span>
  24. </div>
  25. <div>
  26. <b>Results:</b>
  27. <vue-json-pretty
  28. :data="request.results"
  29. :show-length="true"
  30. ></vue-json-pretty>
  31. </div>
  32. <p><b>Date:</b> {{ request.date }}</p>
  33. <p><b>Quota cost:</b> {{ request.quotaCost }}</p>
  34. </div>
  35. </template>
  36. <template #footer>
  37. <quick-confirm v-if="removeAction" @confirm="remove()">
  38. <a class="button is-danger"> Remove API request</a>
  39. </quick-confirm>
  40. </template>
  41. </modal>
  42. </template>
  43. <script>
  44. import { mapActions, mapGetters } from "vuex";
  45. import VueJsonPretty from "vue-json-pretty";
  46. import "vue-json-pretty/lib/styles.css";
  47. import Toast from "toasters";
  48. import ws from "@/ws";
  49. import { mapModalState, mapModalActions } from "@/vuex_helpers";
  50. export default {
  51. components: {
  52. VueJsonPretty
  53. },
  54. props: {
  55. modalUuid: { type: String, default: "" }
  56. },
  57. data() {
  58. return {
  59. loaded: false
  60. };
  61. },
  62. computed: {
  63. ...mapModalState("modals/viewApiRequest/MODAL_UUID", {
  64. requestId: state => state.requestId,
  65. request: state => state.request,
  66. removeAction: state => state.removeAction
  67. }),
  68. ...mapGetters({
  69. socket: "websockets/getSocket"
  70. })
  71. },
  72. mounted() {
  73. ws.onConnect(this.init);
  74. },
  75. beforeUnmount() {
  76. this.socket.dispatch(
  77. "apis.leaveRoom",
  78. `view-api-request.${this.requestId}`,
  79. () => {}
  80. );
  81. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  82. this.$store.unregisterModule([
  83. "modals",
  84. "viewApiRequest",
  85. this.modalUuid
  86. ]);
  87. },
  88. methods: {
  89. init() {
  90. this.loaded = false;
  91. this.socket.dispatch(
  92. "youtube.getApiRequest",
  93. this.requestId,
  94. res => {
  95. if (res.status === "success") {
  96. const { apiRequest } = res.data;
  97. this.viewApiRequest(apiRequest);
  98. this.loaded = true;
  99. this.socket.dispatch(
  100. "apis.joinRoom",
  101. `view-api-request.${this.requestId}`
  102. );
  103. this.socket.on(
  104. "event:youtubeApiRequest.removed",
  105. () => {
  106. new Toast("This API request was removed.");
  107. this.closeCurrentModal();
  108. },
  109. { modalUuid: this.modalUuid }
  110. );
  111. } else {
  112. new Toast("API request with that ID not found");
  113. this.closeCurrentModal();
  114. }
  115. }
  116. );
  117. },
  118. remove() {
  119. if (this.removeAction)
  120. this.socket.dispatch(this.removeAction, this.requestId, res => {
  121. if (res.status === "success") {
  122. new Toast("API request successfully removed.");
  123. this.closeCurrentModal();
  124. } else {
  125. new Toast("API request with that ID not found.");
  126. }
  127. });
  128. },
  129. ...mapModalActions("modals/viewApiRequest/MODAL_UUID", [
  130. "viewApiRequest"
  131. ]),
  132. ...mapActions("modalVisibility", ["closeCurrentModal"])
  133. }
  134. };
  135. </script>
  136. <style lang="less" scoped>
  137. ul {
  138. list-style-type: disc;
  139. padding-left: 20px;
  140. }
  141. </style>