URLEncodedParams.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <div class="flex flex-col flex-1">
  3. <div
  4. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperMobileRawStickyFold sm:top-upperMobileRawTertiaryStickyFold"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("request.body") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-tippy="{ theme: 'tooltip' }"
  12. to="https://docs.hoppscotch.io/features/body"
  13. blank
  14. :title="t('app.wiki')"
  15. svg="help-circle"
  16. />
  17. <ButtonSecondary
  18. v-tippy="{ theme: 'tooltip' }"
  19. :title="t('action.clear_all')"
  20. svg="trash-2"
  21. @click.native="clearContent()"
  22. />
  23. <ButtonSecondary
  24. v-tippy="{ theme: 'tooltip' }"
  25. :title="t('state.bulk_mode')"
  26. svg="edit"
  27. :class="{ '!text-accent': bulkMode }"
  28. @click.native="bulkMode = !bulkMode"
  29. />
  30. <ButtonSecondary
  31. v-tippy="{ theme: 'tooltip' }"
  32. :title="t('add.new')"
  33. svg="plus"
  34. :disabled="bulkMode"
  35. @click.native="addUrlEncodedParam"
  36. />
  37. </div>
  38. </div>
  39. <div v-if="bulkMode" ref="bulkEditor" class="flex flex-col flex-1"></div>
  40. <div v-else>
  41. <draggable
  42. v-model="workingUrlEncodedParams"
  43. animation="250"
  44. handle=".draggable-handle"
  45. draggable=".draggable-content"
  46. ghost-class="cursor-move"
  47. chosen-class="bg-primaryLight"
  48. drag-class="cursor-grabbing"
  49. >
  50. <div
  51. v-for="(param, index) in workingUrlEncodedParams"
  52. :key="`param-${param.id}-${index}`"
  53. class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
  54. >
  55. <span>
  56. <ButtonSecondary
  57. svg="grip-vertical"
  58. class="cursor-auto text-primary hover:text-primary"
  59. :class="{
  60. 'draggable-handle group-hover:text-secondaryLight !cursor-grab':
  61. index !== workingUrlEncodedParams?.length - 1,
  62. }"
  63. tabindex="-1"
  64. />
  65. </span>
  66. <SmartEnvInput
  67. v-model="param.key"
  68. :placeholder="`${t('count.parameter', { count: index + 1 })}`"
  69. @change="
  70. updateUrlEncodedParam(index, {
  71. id: param.id,
  72. key: $event,
  73. value: param.value,
  74. active: param.active,
  75. })
  76. "
  77. />
  78. <SmartEnvInput
  79. v-model="param.value"
  80. :placeholder="`${t('count.value', { count: index + 1 })}`"
  81. @change="
  82. updateUrlEncodedParam(index, {
  83. id: param.id,
  84. key: param.key,
  85. value: $event,
  86. active: param.active,
  87. })
  88. "
  89. />
  90. <span>
  91. <ButtonSecondary
  92. v-tippy="{ theme: 'tooltip' }"
  93. :title="
  94. param.hasOwnProperty('active')
  95. ? param.active
  96. ? t('action.turn_off')
  97. : t('action.turn_on')
  98. : t('action.turn_off')
  99. "
  100. :svg="
  101. param.hasOwnProperty('active')
  102. ? param.active
  103. ? 'check-circle'
  104. : 'circle'
  105. : 'check-circle'
  106. "
  107. color="green"
  108. @click.native="
  109. updateUrlEncodedParam(index, {
  110. id: param.id,
  111. key: param.key,
  112. value: param.value,
  113. active: !param.active,
  114. })
  115. "
  116. />
  117. </span>
  118. <span>
  119. <ButtonSecondary
  120. v-tippy="{ theme: 'tooltip' }"
  121. :title="t('action.remove')"
  122. svg="trash"
  123. color="red"
  124. @click.native="deleteUrlEncodedParam(index)"
  125. />
  126. </span>
  127. </div>
  128. </draggable>
  129. <div
  130. v-if="workingUrlEncodedParams.length === 0"
  131. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  132. >
  133. <img
  134. :src="`/images/states/${$colorMode.value}/add_category.svg`"
  135. loading="lazy"
  136. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  137. :alt="`${t('empty.body')}`"
  138. />
  139. <span class="pb-4 text-center">
  140. {{ t("empty.body") }}
  141. </span>
  142. <ButtonSecondary
  143. filled
  144. :label="`${t('add.new')}`"
  145. svg="plus"
  146. class="mb-4"
  147. @click.native="addUrlEncodedParam"
  148. />
  149. </div>
  150. </div>
  151. </div>
  152. </template>
  153. <script setup lang="ts">
  154. import { computed, Ref, ref, watch } from "@nuxtjs/composition-api"
  155. import isEqual from "lodash/isEqual"
  156. import {
  157. HoppRESTReqBody,
  158. parseRawKeyValueEntries,
  159. parseRawKeyValueEntriesE,
  160. rawKeyValueEntriesToString,
  161. RawKeyValueEntry,
  162. } from "@hoppscotch/data"
  163. import { flow, pipe } from "fp-ts/function"
  164. import * as A from "fp-ts/Array"
  165. import * as O from "fp-ts/Option"
  166. import * as RA from "fp-ts/ReadonlyArray"
  167. import * as E from "fp-ts/Either"
  168. import { cloneDeep } from "lodash"
  169. import draggable from "vuedraggable"
  170. import { useCodemirror } from "~/helpers/editor/codemirror"
  171. import linter from "~/helpers/editor/linting/rawKeyValue"
  172. import { useRESTRequestBody } from "~/newstore/RESTSession"
  173. import { pluckRef, useI18n, useToast } from "~/helpers/utils/composables"
  174. import { objRemoveKey } from "~/helpers/functional/object"
  175. import { throwError } from "~/helpers/functional/error"
  176. const t = useI18n()
  177. const toast = useToast()
  178. const idTicker = ref(0)
  179. const bulkMode = ref(false)
  180. const bulkUrlEncodedParams = ref("")
  181. const bulkEditor = ref<any | null>(null)
  182. const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
  183. useCodemirror(bulkEditor, bulkUrlEncodedParams, {
  184. extendedEditorConfig: {
  185. mode: "text/x-yaml",
  186. placeholder: `${t("state.bulk_mode_placeholder")}`,
  187. },
  188. linter,
  189. completer: null,
  190. environmentHighlights: true,
  191. })
  192. // The functional urlEncodedParams list (the urlEncodedParams actually in the system)
  193. const urlEncodedParamsRaw = pluckRef(
  194. useRESTRequestBody() as Ref<
  195. HoppRESTReqBody & { contentType: "application/x-www-form-urlencoded" }
  196. >,
  197. "body"
  198. )
  199. const urlEncodedParams = computed<RawKeyValueEntry[]>({
  200. get() {
  201. return parseRawKeyValueEntries(urlEncodedParamsRaw.value)
  202. },
  203. set(newValue) {
  204. urlEncodedParamsRaw.value = rawKeyValueEntriesToString(newValue)
  205. },
  206. })
  207. // The UI representation of the urlEncodedParams list (has the empty end urlEncodedParam)
  208. const workingUrlEncodedParams = ref<Array<RawKeyValueEntry & { id: number }>>([
  209. {
  210. id: idTicker.value++,
  211. key: "",
  212. value: "",
  213. active: true,
  214. },
  215. ])
  216. // Rule: Working urlEncodedParams always have one empty urlEncodedParam or the last element is always an empty urlEncodedParams
  217. watch(workingUrlEncodedParams, (urlEncodedParamList) => {
  218. if (
  219. urlEncodedParamList.length > 0 &&
  220. urlEncodedParamList[urlEncodedParamList.length - 1].key !== ""
  221. ) {
  222. workingUrlEncodedParams.value.push({
  223. id: idTicker.value++,
  224. key: "",
  225. value: "",
  226. active: true,
  227. })
  228. }
  229. })
  230. // Sync logic between urlEncodedParams and working urlEncodedParams
  231. watch(
  232. urlEncodedParams,
  233. (newurlEncodedParamList) => {
  234. const filteredWorkingUrlEncodedParams = pipe(
  235. workingUrlEncodedParams.value,
  236. A.filterMap(
  237. flow(
  238. O.fromPredicate((x) => x.key !== ""),
  239. O.map(objRemoveKey("id"))
  240. )
  241. )
  242. )
  243. const filteredBulkUrlEncodedParams = pipe(
  244. parseRawKeyValueEntriesE(bulkUrlEncodedParams.value),
  245. E.map(
  246. flow(
  247. RA.filter((e) => e.key !== ""),
  248. RA.toArray
  249. )
  250. )
  251. )
  252. if (!isEqual(newurlEncodedParamList, filteredWorkingUrlEncodedParams)) {
  253. workingUrlEncodedParams.value = pipe(
  254. newurlEncodedParamList,
  255. A.map((x) => ({ id: idTicker.value++, ...x }))
  256. )
  257. }
  258. if (!isEqual(newurlEncodedParamList, filteredBulkUrlEncodedParams)) {
  259. bulkUrlEncodedParams.value = rawKeyValueEntriesToString(
  260. newurlEncodedParamList
  261. )
  262. }
  263. },
  264. { immediate: true }
  265. )
  266. watch(workingUrlEncodedParams, (newWorkingUrlEncodedParams) => {
  267. const fixedUrlEncodedParams = pipe(
  268. newWorkingUrlEncodedParams,
  269. A.filterMap(
  270. flow(
  271. O.fromPredicate((e) => e.key !== ""),
  272. O.map(objRemoveKey("id"))
  273. )
  274. )
  275. )
  276. if (!isEqual(urlEncodedParams.value, fixedUrlEncodedParams)) {
  277. urlEncodedParams.value = fixedUrlEncodedParams
  278. }
  279. })
  280. watch(bulkUrlEncodedParams, (newBulkUrlEncodedParams) => {
  281. const filteredBulkParams = pipe(
  282. parseRawKeyValueEntriesE(newBulkUrlEncodedParams),
  283. E.map(
  284. flow(
  285. RA.filter((e) => e.key !== ""),
  286. RA.toArray
  287. )
  288. ),
  289. E.getOrElse(() => [] as RawKeyValueEntry[])
  290. )
  291. if (!isEqual(urlEncodedParams.value, filteredBulkParams)) {
  292. urlEncodedParams.value = filteredBulkParams
  293. }
  294. })
  295. const addUrlEncodedParam = () => {
  296. workingUrlEncodedParams.value.push({
  297. id: idTicker.value++,
  298. key: "",
  299. value: "",
  300. active: true,
  301. })
  302. }
  303. const updateUrlEncodedParam = (
  304. index: number,
  305. param: RawKeyValueEntry & { id: number }
  306. ) => {
  307. workingUrlEncodedParams.value = workingUrlEncodedParams.value.map((p, i) =>
  308. i === index ? param : p
  309. )
  310. }
  311. const deleteUrlEncodedParam = (index: number) => {
  312. const urlEncodedParamsBeforeDeletion = cloneDeep(
  313. workingUrlEncodedParams.value
  314. )
  315. if (
  316. !(
  317. urlEncodedParamsBeforeDeletion.length > 0 &&
  318. index === urlEncodedParamsBeforeDeletion.length - 1
  319. )
  320. ) {
  321. if (deletionToast.value) {
  322. deletionToast.value.goAway(0)
  323. deletionToast.value = null
  324. }
  325. deletionToast.value = toast.success(`${t("state.deleted")}`, {
  326. action: [
  327. {
  328. text: `${t("action.undo")}`,
  329. onClick: (_, toastObject) => {
  330. workingUrlEncodedParams.value = urlEncodedParamsBeforeDeletion
  331. toastObject.goAway(0)
  332. deletionToast.value = null
  333. },
  334. },
  335. ],
  336. onComplete: () => {
  337. deletionToast.value = null
  338. },
  339. })
  340. }
  341. workingUrlEncodedParams.value = pipe(
  342. workingUrlEncodedParams.value,
  343. A.deleteAt(index),
  344. O.getOrElseW(() =>
  345. throwError("Working URL Encoded Params Deletion Out of Bounds")
  346. )
  347. )
  348. }
  349. const clearContent = () => {
  350. // set urlEncodedParams list to the initial state
  351. workingUrlEncodedParams.value = [
  352. {
  353. id: idTicker.value++,
  354. key: "",
  355. value: "",
  356. active: true,
  357. },
  358. ]
  359. bulkUrlEncodedParams.value = ""
  360. }
  361. </script>