BodyParameters.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <AppSection label="bodyParameters">
  3. <div
  4. class="
  5. bg-primary
  6. border-b border-dividerLight
  7. flex flex-1
  8. top-upperTertiaryStickyFold
  9. pl-4
  10. z-10
  11. sticky
  12. items-center
  13. justify-between
  14. "
  15. >
  16. <label class="font-semibold text-secondaryLight">
  17. {{ $t("request.body") }}
  18. </label>
  19. <div class="flex">
  20. <ButtonSecondary
  21. v-tippy="{ theme: 'tooltip' }"
  22. to="https://docs.hoppscotch.io/features/body"
  23. blank
  24. :title="$t('app.wiki')"
  25. svg="help-circle"
  26. />
  27. <ButtonSecondary
  28. v-tippy="{ theme: 'tooltip' }"
  29. :title="$t('action.clear_all')"
  30. svg="trash-2"
  31. @click.native="clearContent"
  32. />
  33. <ButtonSecondary
  34. v-tippy="{ theme: 'tooltip' }"
  35. :title="$t('add.new')"
  36. svg="plus"
  37. @click.native="addBodyParam"
  38. />
  39. </div>
  40. </div>
  41. <div
  42. v-for="(param, index) in bodyParams"
  43. :key="`param-${index}`"
  44. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  45. >
  46. <SmartEnvInput
  47. v-model="param.key"
  48. :placeholder="`${$t('count.parameter', { count: index + 1 })}`"
  49. styles="
  50. bg-transparent
  51. flex
  52. flex-1
  53. py-1
  54. px-4
  55. "
  56. @change="
  57. updateBodyParam(index, {
  58. key: $event,
  59. value: param.value,
  60. active: param.active,
  61. isFile: param.isFile,
  62. })
  63. "
  64. />
  65. <div v-if="param.isFile" class="file-chips-container hide-scrollbar">
  66. <div class="space-x-2 file-chips-wrapper">
  67. <SmartDeletableChip
  68. v-for="(file, fileIndex) in param.value"
  69. :key="`param-${index}-file-${fileIndex}`"
  70. @chip-delete="chipDelete(index, fileIndex)"
  71. >
  72. {{ file.name }}
  73. </SmartDeletableChip>
  74. </div>
  75. </div>
  76. <span v-else class="flex flex-1">
  77. <SmartEnvInput
  78. v-model="param.value"
  79. :placeholder="`${$t('count.value', { count: index + 1 })}`"
  80. styles="
  81. bg-transparent
  82. flex
  83. flex-1
  84. py-1
  85. px-4
  86. "
  87. @change="
  88. updateBodyParam(index, {
  89. key: param.key,
  90. value: $event,
  91. active: param.active,
  92. isFile: param.isFile,
  93. })
  94. "
  95. />
  96. </span>
  97. <span>
  98. <label for="attachment" class="p-0">
  99. <ButtonSecondary
  100. class="w-full"
  101. svg="paperclip"
  102. @click.native="$refs.attachment[index].click()"
  103. />
  104. </label>
  105. <input
  106. ref="attachment"
  107. class="input"
  108. name="attachment"
  109. type="file"
  110. multiple
  111. @change="setRequestAttachment(index, param, $event)"
  112. />
  113. </span>
  114. <span>
  115. <ButtonSecondary
  116. v-tippy="{ theme: 'tooltip' }"
  117. :title="
  118. param.hasOwnProperty('active')
  119. ? param.active
  120. ? $t('action.turn_off')
  121. : $t('action.turn_on')
  122. : $t('action.turn_off')
  123. "
  124. :svg="
  125. param.hasOwnProperty('active')
  126. ? param.active
  127. ? 'check-circle'
  128. : 'circle'
  129. : 'check-circle'
  130. "
  131. color="green"
  132. @click.native="
  133. updateBodyParam(index, {
  134. key: param.key,
  135. value: param.value,
  136. active: param.hasOwnProperty('active') ? !param.active : false,
  137. isFile: param.isFile,
  138. })
  139. "
  140. />
  141. </span>
  142. <span>
  143. <ButtonSecondary
  144. v-tippy="{ theme: 'tooltip' }"
  145. :title="$t('action.remove')"
  146. svg="trash"
  147. color="red"
  148. @click.native="deleteBodyParam(index)"
  149. />
  150. </span>
  151. </div>
  152. <div
  153. v-if="bodyParams.length === 0"
  154. class="flex flex-col text-secondaryLight p-4 items-center justify-center"
  155. >
  156. <img
  157. :src="`/images/states/${$colorMode.value}/upload_single_file.svg`"
  158. loading="lazy"
  159. class="flex-col my-4 object-contain object-center h-16 w-16 inline-flex"
  160. :alt="$t('empty.body')"
  161. />
  162. <span class="text-center pb-4">
  163. {{ $t("empty.body") }}
  164. </span>
  165. <ButtonSecondary
  166. :label="`${$t('add.new')}`"
  167. filled
  168. svg="plus"
  169. class="mb-4"
  170. @click.native="addBodyParam"
  171. />
  172. </div>
  173. </AppSection>
  174. </template>
  175. <script lang="ts">
  176. import { defineComponent, onMounted, Ref, watch } from "@nuxtjs/composition-api"
  177. import { FormDataKeyValue } from "~/helpers/types/HoppRESTRequest"
  178. import { pluckRef } from "~/helpers/utils/composables"
  179. import {
  180. addFormDataEntry,
  181. deleteAllFormDataEntries,
  182. deleteFormDataEntry,
  183. updateFormDataEntry,
  184. useRESTRequestBody,
  185. } from "~/newstore/RESTSession"
  186. export default defineComponent({
  187. setup() {
  188. const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref<
  189. FormDataKeyValue[]
  190. >
  191. const addBodyParam = () => {
  192. addFormDataEntry({ key: "", value: "", active: true, isFile: false })
  193. }
  194. const updateBodyParam = (index: number, entry: FormDataKeyValue) => {
  195. updateFormDataEntry(index, entry)
  196. }
  197. const deleteBodyParam = (index: number) => {
  198. deleteFormDataEntry(index)
  199. }
  200. const clearContent = () => {
  201. deleteAllFormDataEntries()
  202. }
  203. const chipDelete = (paramIndex: number, fileIndex: number) => {
  204. const entry = bodyParams.value[paramIndex]
  205. if (entry.isFile) {
  206. entry.value.splice(fileIndex, 1)
  207. if (entry.value.length === 0) {
  208. updateFormDataEntry(paramIndex, {
  209. ...entry,
  210. isFile: false,
  211. value: "",
  212. })
  213. return
  214. }
  215. }
  216. updateFormDataEntry(paramIndex, entry)
  217. }
  218. const setRequestAttachment = (
  219. index: number,
  220. entry: FormDataKeyValue,
  221. event: InputEvent
  222. ) => {
  223. const fileEntry: FormDataKeyValue = {
  224. ...entry,
  225. isFile: true,
  226. value: Array.from((event.target as HTMLInputElement).files!),
  227. }
  228. updateFormDataEntry(index, fileEntry)
  229. }
  230. watch(
  231. bodyParams,
  232. () => {
  233. if (
  234. bodyParams.value.length > 0 &&
  235. (bodyParams.value[bodyParams.value.length - 1].key !== "" ||
  236. bodyParams.value[bodyParams.value.length - 1].value !== "")
  237. )
  238. addBodyParam()
  239. },
  240. { deep: true }
  241. )
  242. onMounted(() => {
  243. if (!bodyParams.value?.length) {
  244. addBodyParam()
  245. }
  246. })
  247. return {
  248. bodyParams,
  249. addBodyParam,
  250. updateBodyParam,
  251. deleteBodyParam,
  252. clearContent,
  253. setRequestAttachment,
  254. chipDelete,
  255. }
  256. },
  257. })
  258. </script>
  259. <style scoped lang="scss">
  260. .file-chips-container {
  261. @apply flex flex-1;
  262. @apply whitespace-nowrap;
  263. @apply overflow-auto;
  264. @apply bg-transparent;
  265. .file-chips-wrapper {
  266. @apply flex;
  267. @apply px-4;
  268. @apply py-1;
  269. @apply w-0;
  270. }
  271. }
  272. </style>