123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <template>
- <AppSection label="bodyParameters">
- <div
- class="bg-primary border-b border-dividerLight flex flex-1 top-upperTertiaryStickyFold pl-4 z-10 sticky items-center justify-between"
- >
- <label class="font-semibold text-secondaryLight">
- {{ $t("request.body") }}
- </label>
- <div class="flex">
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- to="https://docs.hoppscotch.io/features/body"
- blank
- :title="$t('app.wiki')"
- svg="help-circle"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="$t('action.clear_all')"
- svg="trash-2"
- @click.native="clearContent"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="$t('add.new')"
- svg="plus"
- @click.native="addBodyParam"
- />
- </div>
- </div>
- <div
- v-for="(param, index) in bodyParams"
- :key="`param-${index}`"
- class="divide-dividerLight divide-x border-b border-dividerLight flex"
- >
- <SmartEnvInput
- v-model="param.key"
- :placeholder="`${$t('count.parameter', { count: index + 1 })}`"
- styles="
- bg-transparent
- flex
- flex-1
- py-1
- px-4
- "
- @change="
- updateBodyParam(index, {
- key: $event,
- value: param.value,
- active: param.active,
- isFile: param.isFile,
- })
- "
- />
- <div v-if="param.isFile" class="file-chips-container hide-scrollbar">
- <div class="space-x-2 file-chips-wrapper">
- <SmartDeletableChip
- v-for="(file, fileIndex) in param.value"
- :key="`param-${index}-file-${fileIndex}`"
- @chip-delete="chipDelete(index, fileIndex)"
- >
- {{ file.name }}
- </SmartDeletableChip>
- </div>
- </div>
- <span v-else class="flex flex-1">
- <SmartEnvInput
- v-model="param.value"
- :placeholder="`${$t('count.value', { count: index + 1 })}`"
- styles="
- bg-transparent
- flex
- flex-1
- py-1
- px-4
- "
- @change="
- updateBodyParam(index, {
- key: param.key,
- value: $event,
- active: param.active,
- isFile: param.isFile,
- })
- "
- />
- </span>
- <span>
- <label for="attachment" class="p-0">
- <ButtonSecondary
- class="w-full"
- svg="paperclip"
- @click.native="$refs.attachment[index].click()"
- />
- </label>
- <input
- ref="attachment"
- class="input"
- name="attachment"
- type="file"
- multiple
- @change="setRequestAttachment(index, param, $event)"
- />
- </span>
- <span>
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="
- param.hasOwnProperty('active')
- ? param.active
- ? $t('action.turn_off')
- : $t('action.turn_on')
- : $t('action.turn_off')
- "
- :svg="
- param.hasOwnProperty('active')
- ? param.active
- ? 'check-circle'
- : 'circle'
- : 'check-circle'
- "
- color="green"
- @click.native="
- updateBodyParam(index, {
- key: param.key,
- value: param.value,
- active: param.hasOwnProperty('active') ? !param.active : false,
- isFile: param.isFile,
- })
- "
- />
- </span>
- <span>
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="$t('action.remove')"
- svg="trash"
- color="red"
- @click.native="deleteBodyParam(index)"
- />
- </span>
- </div>
- <div
- v-if="bodyParams.length === 0"
- class="flex flex-col text-secondaryLight p-4 items-center justify-center"
- >
- <img
- :src="`/images/states/${$colorMode.value}/upload_single_file.svg`"
- loading="lazy"
- class="flex-col object-contain object-center h-16 my-4 w-16 inline-flex"
- :alt="$t('empty.body')"
- />
- <span class="text-center pb-4">
- {{ $t("empty.body") }}
- </span>
- <ButtonSecondary
- :label="`${$t('add.new')}`"
- filled
- svg="plus"
- class="mb-4"
- @click.native="addBodyParam"
- />
- </div>
- </AppSection>
- </template>
- <script lang="ts">
- import { defineComponent, onMounted, Ref, watch } from "@nuxtjs/composition-api"
- import { FormDataKeyValue } from "@hoppscotch/data"
- import { pluckRef } from "~/helpers/utils/composables"
- import {
- addFormDataEntry,
- deleteAllFormDataEntries,
- deleteFormDataEntry,
- updateFormDataEntry,
- useRESTRequestBody,
- } from "~/newstore/RESTSession"
- export default defineComponent({
- setup() {
- const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref<
- FormDataKeyValue[]
- >
- const addBodyParam = () => {
- addFormDataEntry({ key: "", value: "", active: true, isFile: false })
- }
- const updateBodyParam = (index: number, entry: FormDataKeyValue) => {
- updateFormDataEntry(index, entry)
- }
- const deleteBodyParam = (index: number) => {
- deleteFormDataEntry(index)
- }
- const clearContent = () => {
- deleteAllFormDataEntries()
- }
- const chipDelete = (paramIndex: number, fileIndex: number) => {
- const entry = bodyParams.value[paramIndex]
- if (entry.isFile) {
- entry.value.splice(fileIndex, 1)
- if (entry.value.length === 0) {
- updateFormDataEntry(paramIndex, {
- ...entry,
- isFile: false,
- value: "",
- })
- return
- }
- }
- updateFormDataEntry(paramIndex, entry)
- }
- const setRequestAttachment = (
- index: number,
- entry: FormDataKeyValue,
- event: InputEvent
- ) => {
- const fileEntry: FormDataKeyValue = {
- ...entry,
- isFile: true,
- value: Array.from((event.target as HTMLInputElement).files!),
- }
- updateFormDataEntry(index, fileEntry)
- }
- watch(
- bodyParams,
- () => {
- if (
- bodyParams.value.length > 0 &&
- (bodyParams.value[bodyParams.value.length - 1].key !== "" ||
- bodyParams.value[bodyParams.value.length - 1].value !== "")
- )
- addBodyParam()
- },
- { deep: true }
- )
- onMounted(() => {
- if (!bodyParams.value?.length) {
- addBodyParam()
- }
- })
- return {
- bodyParams,
- addBodyParam,
- updateBodyParam,
- deleteBodyParam,
- clearContent,
- setRequestAttachment,
- chipDelete,
- }
- },
- })
- </script>
- <style scoped lang="scss">
- .file-chips-container {
- @apply flex flex-1;
- @apply whitespace-nowrap;
- @apply overflow-auto;
- @apply bg-transparent;
- .file-chips-wrapper {
- @apply flex;
- @apply px-4;
- @apply py-1;
- @apply w-0;
- }
- }
- </style>
|