BodyParameters.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <div>
  3. <ul>
  4. <li>
  5. <div class="row-wrapper">
  6. <label for="reqParamList">{{ $t("request_body") }}</label>
  7. <div>
  8. <button
  9. v-tooltip.bottom="$t('clear')"
  10. class="icon"
  11. @click="clearContent('bodyParams', $event)"
  12. >
  13. <i class="material-icons">clear_all</i>
  14. </button>
  15. </div>
  16. </div>
  17. </li>
  18. </ul>
  19. <ul
  20. v-for="(param, index) in bodyParams"
  21. :key="index"
  22. class="
  23. border-b border-dashed
  24. divide-y
  25. md:divide-x
  26. border-divider
  27. divide-dashed divide-divider
  28. md:divide-y-0
  29. "
  30. :class="{ 'border-t': index == 0 }"
  31. >
  32. <li>
  33. <input
  34. :placeholder="`key ${index + 1}`"
  35. :name="`bparam ${index}`"
  36. :value="param.key"
  37. autofocus
  38. @change="updateBodyParams($event, index, `setKeyBodyParams`)"
  39. @keyup.prevent="setRouteQueryState"
  40. />
  41. </li>
  42. <li>
  43. <input
  44. v-if="!requestBodyParamIsFile(index)"
  45. :placeholder="`value ${index + 1}`"
  46. :value="param.value"
  47. @change="
  48. // if input is form data, set value to be an array containing the value
  49. // only
  50. updateBodyParams($event, index, `setValueBodyParams`)
  51. "
  52. @keyup.prevent="setRouteQueryState"
  53. />
  54. <div v-else class="file-chips-container">
  55. <div class="file-chips-wrapper">
  56. <SmartDeletableChip
  57. v-for="(file, i) in Array.from(bodyParams[index].value)"
  58. :key="`body-param-${index}-file-${i}`"
  59. @chip-delete="chipDelete(index, i)"
  60. >
  61. {{ file.name }}
  62. </SmartDeletableChip>
  63. </div>
  64. </div>
  65. </li>
  66. <div>
  67. <li>
  68. <button
  69. v-tooltip.bottom="{
  70. content: param.hasOwnProperty('active')
  71. ? param.active
  72. ? $t('turn_off')
  73. : $t('turn_on')
  74. : $t('turn_off'),
  75. }"
  76. class="icon"
  77. @click="toggleActive(index, param)"
  78. >
  79. <i class="material-icons">
  80. {{
  81. param.hasOwnProperty("active")
  82. ? param.active
  83. ? "check_box"
  84. : "check_box_outline_blank"
  85. : "check_box"
  86. }}
  87. </i>
  88. </button>
  89. </li>
  90. </div>
  91. <div v-if="contentType === 'multipart/form-data'">
  92. <li>
  93. <label for="attachment" class="p-0">
  94. <button
  95. class="w-full icon"
  96. @click="$refs.attachment[index].click()"
  97. >
  98. <i class="material-icons">attach_file</i>
  99. </button>
  100. </label>
  101. <input
  102. ref="attachment"
  103. name="attachment"
  104. type="file"
  105. multiple
  106. @change="setRequestAttachment($event, index)"
  107. />
  108. </li>
  109. </div>
  110. <div>
  111. <li>
  112. <button
  113. v-tooltip.bottom="$t('delete')"
  114. class="icon"
  115. @click="removeRequestBodyParam(index)"
  116. >
  117. <i class="material-icons">delete</i>
  118. </button>
  119. </li>
  120. </div>
  121. </ul>
  122. <ul>
  123. <li>
  124. <button class="icon" name="addrequest" @click="addRequestBodyParam">
  125. <i class="material-icons">add</i>
  126. <span>{{ $t("add_new") }}</span>
  127. </button>
  128. </li>
  129. </ul>
  130. </div>
  131. </template>
  132. <script>
  133. export default {
  134. props: {
  135. bodyParams: { type: Array, default: () => [] },
  136. },
  137. computed: {
  138. contentType() {
  139. return this.$store.state.request.contentType
  140. },
  141. },
  142. methods: {
  143. clearContent(bodyParams, $event) {
  144. this.$emit("clear-content", bodyParams, $event)
  145. },
  146. setRouteQueryState() {
  147. this.$emit("set-route-query-state")
  148. },
  149. removeRequestBodyParam(index) {
  150. const paramArr = this.$store.state.request.bodyParams.filter(
  151. (item, itemIndex) =>
  152. itemIndex !== index &&
  153. (Object.prototype.hasOwnProperty.call(item, "active")
  154. ? item.active === true
  155. : true)
  156. )
  157. this.setRawParams(paramArr)
  158. this.$emit("remove-request-body-param", index)
  159. },
  160. addRequestBodyParam() {
  161. this.$emit("add-request-body-param")
  162. },
  163. setRequestAttachment(event, index) {
  164. const { files } = event.target
  165. this.$store.commit("setFilesBodyParams", {
  166. index,
  167. value: Array.from(files),
  168. })
  169. },
  170. requestBodyParamIsFile(index) {
  171. const bodyParamValue = this.bodyParams?.[index]?.value
  172. const isFile = bodyParamValue?.[0] instanceof File
  173. return isFile
  174. },
  175. chipDelete(paramIndex, fileIndex) {
  176. this.$store.commit("removeFile", {
  177. index: paramIndex,
  178. fileIndex,
  179. })
  180. },
  181. updateBodyParams(event, index, type) {
  182. this.$store.commit(type, {
  183. index,
  184. value: event.target.value,
  185. })
  186. const paramArr = this.$store.state.request.bodyParams.filter((item) =>
  187. Object.prototype.hasOwnProperty.call(item, "active")
  188. ? item.active === true
  189. : true
  190. )
  191. this.setRawParams(paramArr)
  192. },
  193. toggleActive(index, param) {
  194. const paramArr = this.$store.state.request.bodyParams.filter(
  195. (item, itemIndex) => {
  196. if (index === itemIndex) {
  197. return !param.active
  198. } else {
  199. return Object.prototype.hasOwnProperty.call(item, "active")
  200. ? item.active === true
  201. : true
  202. }
  203. }
  204. )
  205. this.setRawParams(paramArr)
  206. this.$store.commit("setActiveBodyParams", {
  207. index,
  208. value: Object.prototype.hasOwnProperty.call(param, "active")
  209. ? !param.active
  210. : false,
  211. })
  212. },
  213. setRawParams(filteredParamArr) {
  214. let rawParams = {}
  215. filteredParamArr.forEach((_param) => {
  216. rawParams = {
  217. ...rawParams,
  218. [_param.key]: _param.value,
  219. }
  220. })
  221. const rawParamsStr = JSON.stringify(rawParams, null, 2)
  222. this.$store.commit("setState", {
  223. value: rawParamsStr,
  224. attribute: "rawParams",
  225. })
  226. },
  227. },
  228. }
  229. </script>
  230. <style scoped lang="scss">
  231. .file-chips-container {
  232. @apply flex;
  233. @apply flex-1;
  234. @apply whitespace-nowrap;
  235. @apply overflow-auto;
  236. @apply bg-primaryDark;
  237. .file-chips-wrapper {
  238. @apply flex;
  239. @apply w-0;
  240. }
  241. }
  242. </style>