QueryParams.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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-upperMobileSecondaryStickyFold sm:top-upperSecondaryStickyFold"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("request.parameter_list") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-tippy="{ theme: 'tooltip' }"
  12. to="https://docs.hoppscotch.io/features/parameters"
  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="addParam"
  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="workingParams"
  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 workingParams"
  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 !== workingParams?.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. updateParam(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. updateParam(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. updateParam(index, {
  110. id: param.id,
  111. key: param.key,
  112. value: param.value,
  113. active: param.hasOwnProperty('active')
  114. ? !param.active
  115. : false,
  116. })
  117. "
  118. />
  119. </span>
  120. <span>
  121. <ButtonSecondary
  122. v-tippy="{ theme: 'tooltip' }"
  123. :title="t('action.remove')"
  124. svg="trash"
  125. color="red"
  126. @click.native="deleteParam(index)"
  127. />
  128. </span>
  129. </div>
  130. </draggable>
  131. <div
  132. v-if="workingParams.length === 0"
  133. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  134. >
  135. <img
  136. :src="`/images/states/${$colorMode.value}/add_files.svg`"
  137. loading="lazy"
  138. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  139. :alt="`${t('empty.parameters')}`"
  140. />
  141. <span class="pb-4 text-center">{{ t("empty.parameters") }}</span>
  142. <ButtonSecondary
  143. :label="`${t('add.new')}`"
  144. svg="plus"
  145. filled
  146. class="mb-4"
  147. @click.native="addParam"
  148. />
  149. </div>
  150. </div>
  151. </div>
  152. </template>
  153. <script setup lang="ts">
  154. import { Ref, ref, watch } from "@nuxtjs/composition-api"
  155. import { flow, pipe } from "fp-ts/function"
  156. import * as O from "fp-ts/Option"
  157. import * as A from "fp-ts/Array"
  158. import * as RA from "fp-ts/ReadonlyArray"
  159. import * as E from "fp-ts/Either"
  160. import {
  161. HoppRESTParam,
  162. parseRawKeyValueEntriesE,
  163. rawKeyValueEntriesToString,
  164. RawKeyValueEntry,
  165. } from "@hoppscotch/data"
  166. import isEqual from "lodash/isEqual"
  167. import cloneDeep from "lodash/cloneDeep"
  168. import draggable from "vuedraggable"
  169. import linter from "~/helpers/editor/linting/rawKeyValue"
  170. import { useCodemirror } from "~/helpers/editor/codemirror"
  171. import { useI18n, useToast, useStream } from "~/helpers/utils/composables"
  172. import { restParams$, setRESTParams } from "~/newstore/RESTSession"
  173. import { throwError } from "~/helpers/functional/error"
  174. import { objRemoveKey } from "~/helpers/functional/object"
  175. const t = useI18n()
  176. const toast = useToast()
  177. const idTicker = ref(0)
  178. const bulkMode = ref(false)
  179. const bulkParams = ref("")
  180. const bulkEditor = ref<any | null>(null)
  181. const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
  182. useCodemirror(bulkEditor, bulkParams, {
  183. extendedEditorConfig: {
  184. mode: "text/x-yaml",
  185. placeholder: `${t("state.bulk_mode_placeholder")}`,
  186. },
  187. linter,
  188. completer: null,
  189. environmentHighlights: true,
  190. })
  191. // The functional parameters list (the parameters actually applied to the session)
  192. const params = useStream(restParams$, [], setRESTParams) as Ref<HoppRESTParam[]>
  193. // The UI representation of the parameters list (has the empty end param)
  194. const workingParams = ref<Array<HoppRESTParam & { id: number }>>([
  195. {
  196. id: idTicker.value++,
  197. key: "",
  198. value: "",
  199. active: true,
  200. },
  201. ])
  202. // Rule: Working Params always have last element is always an empty param
  203. watch(workingParams, (paramsList) => {
  204. if (paramsList.length > 0 && paramsList[paramsList.length - 1].key !== "") {
  205. workingParams.value.push({
  206. id: idTicker.value++,
  207. key: "",
  208. value: "",
  209. active: true,
  210. })
  211. }
  212. })
  213. // Sync logic between params and working/bulk params
  214. watch(
  215. params,
  216. (newParamsList) => {
  217. // Sync should overwrite working params
  218. const filteredWorkingParams: HoppRESTParam[] = pipe(
  219. workingParams.value,
  220. A.filterMap(
  221. flow(
  222. O.fromPredicate((e) => e.key !== ""),
  223. O.map(objRemoveKey("id"))
  224. )
  225. )
  226. )
  227. const filteredBulkParams = pipe(
  228. parseRawKeyValueEntriesE(bulkParams.value),
  229. E.map(
  230. flow(
  231. RA.filter((e) => e.key !== ""),
  232. RA.toArray
  233. )
  234. ),
  235. E.getOrElse(() => [] as RawKeyValueEntry[])
  236. )
  237. if (!isEqual(newParamsList, filteredWorkingParams)) {
  238. workingParams.value = pipe(
  239. newParamsList,
  240. A.map((x) => ({ id: idTicker.value++, ...x }))
  241. )
  242. }
  243. if (!isEqual(newParamsList, filteredBulkParams)) {
  244. bulkParams.value = rawKeyValueEntriesToString(newParamsList)
  245. }
  246. },
  247. { immediate: true }
  248. )
  249. watch(workingParams, (newWorkingParams) => {
  250. const fixedParams = pipe(
  251. newWorkingParams,
  252. A.filterMap(
  253. flow(
  254. O.fromPredicate((e) => e.key !== ""),
  255. O.map(objRemoveKey("id"))
  256. )
  257. )
  258. )
  259. if (!isEqual(params.value, fixedParams)) {
  260. params.value = cloneDeep(fixedParams)
  261. }
  262. })
  263. watch(bulkParams, (newBulkParams) => {
  264. const filteredBulkParams = pipe(
  265. parseRawKeyValueEntriesE(newBulkParams),
  266. E.map(
  267. flow(
  268. RA.filter((e) => e.key !== ""),
  269. RA.toArray
  270. )
  271. ),
  272. E.getOrElse(() => [] as RawKeyValueEntry[])
  273. )
  274. if (!isEqual(params.value, filteredBulkParams)) {
  275. params.value = filteredBulkParams
  276. }
  277. })
  278. const addParam = () => {
  279. workingParams.value.push({
  280. id: idTicker.value++,
  281. key: "",
  282. value: "",
  283. active: true,
  284. })
  285. }
  286. const updateParam = (index: number, param: HoppRESTParam & { id: number }) => {
  287. workingParams.value = workingParams.value.map((h, i) =>
  288. i === index ? param : h
  289. )
  290. }
  291. const deleteParam = (index: number) => {
  292. const paramsBeforeDeletion = cloneDeep(workingParams.value)
  293. if (
  294. !(
  295. paramsBeforeDeletion.length > 0 &&
  296. index === paramsBeforeDeletion.length - 1
  297. )
  298. ) {
  299. if (deletionToast.value) {
  300. deletionToast.value.goAway(0)
  301. deletionToast.value = null
  302. }
  303. deletionToast.value = toast.success(`${t("state.deleted")}`, {
  304. action: [
  305. {
  306. text: `${t("action.undo")}`,
  307. onClick: (_, toastObject) => {
  308. workingParams.value = paramsBeforeDeletion
  309. toastObject.goAway(0)
  310. deletionToast.value = null
  311. },
  312. },
  313. ],
  314. onComplete: () => {
  315. deletionToast.value = null
  316. },
  317. })
  318. }
  319. workingParams.value = pipe(
  320. workingParams.value,
  321. A.deleteAt(index),
  322. O.getOrElseW(() => throwError("Working Params Deletion Out of Bounds"))
  323. )
  324. }
  325. const clearContent = () => {
  326. // set params list to the initial state
  327. workingParams.value = [
  328. {
  329. id: idTicker.value++,
  330. key: "",
  331. value: "",
  332. active: true,
  333. },
  334. ]
  335. bulkParams.value = ""
  336. }
  337. </script>