api-client/components/http/BodyParameters.vue

315 lines
8 KiB
Vue
Raw Normal View History

<template>
2021-07-10 18:08:35 +00:00
<AppSection label="bodyParameters">
<div
class="
bg-primary
2021-07-17 17:40:28 +00:00
border-b border-dividerLight
flex flex-1
2021-08-27 04:07:29 +00:00
top-upperTertiaryStickyFold
2021-08-07 03:37:26 +00:00
pl-4
2021-07-17 17:40:28 +00:00
z-10
sticky
items-center
justify-between
"
2021-07-10 18:08:35 +00:00
>
<label class="font-semibold text-secondaryLight">
{{ $t("request.body") }}
2021-07-10 18:08:35 +00:00
</label>
2021-07-24 16:46:48 +00:00
<div class="flex">
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
2021-08-19 06:16:22 +00:00
to="https://docs.hoppscotch.io/features/body"
blank
:title="$t('app.wiki')"
2021-08-28 00:17:33 +00:00
svg="help-circle"
/>
2021-07-24 16:46:48 +00:00
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('action.clear_all')"
2021-08-28 00:17:33 +00:00
svg="trash-2"
2021-08-08 15:17:50 +00:00
@click.native="clearContent"
/>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('add.new')"
2021-08-28 00:17:33 +00:00
svg="plus"
2021-08-08 15:17:50 +00:00
@click.native="addBodyParam"
2021-07-24 16:46:48 +00:00
/>
</div>
2021-07-10 18:08:35 +00:00
</div>
<div
v-for="(param, index) in bodyParams"
2021-07-13 23:49:08 +00:00
:key="`param-${index}`"
class="divide-x divide-dividerLight border-b border-dividerLight flex"
>
2021-08-08 15:17:50 +00:00
<SmartEnvInput
v-if="EXPERIMENTAL_URL_BAR_ENABLED"
v-model="param.key"
:placeholder="$t('count.parameter', { count: index + 1 })"
styles="
2021-08-27 04:07:29 +00:00
bg-transparent
2021-08-08 15:17:50 +00:00
flex
flex-1
py-1
px-4
"
@change="
updateBodyParam(index, {
key: $event,
2021-08-08 15:17:50 +00:00
value: param.value,
active: param.active,
isFile: param.isFile,
2021-08-08 15:17:50 +00:00
})
"
/>
2021-07-10 18:08:35 +00:00
<input
2021-08-08 15:17:50 +00:00
v-else
2021-08-27 04:07:29 +00:00
class="bg-transparent flex flex-1 py-2 px-4"
2021-08-02 15:27:18 +00:00
:placeholder="$t('count.parameter', { count: index + 1 })"
2021-07-10 18:08:35 +00:00
:name="'param' + index"
:value="param.key"
autofocus
2021-08-08 15:17:50 +00:00
@change="
updateBodyParam(index, {
key: $event.target.value,
value: param.value,
active: param.active,
isFile: param.isFile,
2021-08-08 15:17:50 +00:00
})
"
/>
<div v-if="param.isFile" class="file-chips-container hide-scrollbar">
<div class="space-x-2 file-chips-wrapper">
2021-07-10 18:08:35 +00:00
<SmartDeletableChip
v-for="(file, fileIndex) in param.value"
2021-07-13 23:49:08 +00:00
:key="`param-${index}-file-${fileIndex}`"
@chip-delete="chipDelete(index, fileIndex)"
2021-07-10 18:08:35 +00:00
>
{{ file.name }}
</SmartDeletableChip>
</div>
2021-07-10 18:08:35 +00:00
</div>
<span v-else class="flex flex-1">
<SmartEnvInput
v-if="EXPERIMENTAL_URL_BAR_ENABLED"
v-model="param.value"
:placeholder="$t('count.value', { count: index + 1 })"
styles="
2021-08-27 04:07:29 +00:00
bg-transparent
flex
flex-1
py-1
px-4
"
@change="
updateBodyParam(index, {
key: param.key,
value: $event,
active: param.active,
isFile: param.isFile,
})
"
/>
<input
v-else
2021-08-27 04:07:29 +00:00
class="bg-transparent flex flex-1 py-2 px-4"
:placeholder="$t('count.value', { count: index + 1 })"
:name="'value' + index"
:value="param.value"
@change="
updateBodyParam(index, {
key: param.key,
value: $event.target.value,
active: param.active,
isFile: param.isFile,
})
"
/>
</span>
<span>
<label for="attachment" class="p-0">
<ButtonSecondary
class="w-full"
2021-08-28 00:17:33 +00:00
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>
2021-08-11 08:46:43 +00:00
<span>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="
param.hasOwnProperty('active')
? param.active
? $t('action.turn_off')
: $t('action.turn_on')
: $t('action.turn_off')
"
2021-08-28 00:17:33 +00:00
:svg="
2021-08-11 08:46:43 +00:00
param.hasOwnProperty('active')
? param.active
2021-08-28 00:17:33 +00:00
? 'check-circle'
: 'circle'
: 'check-circle'
2021-08-11 08:46:43 +00:00
"
color="green"
@click.native="
updateBodyParam(index, {
key: param.key,
value: param.value,
active: param.hasOwnProperty('active') ? !param.active : false,
isFile: param.isFile,
2021-08-11 08:46:43 +00:00
})
"
/>
</span>
<span>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('action.remove')"
2021-08-28 00:17:33 +00:00
svg="trash"
2021-08-11 08:46:43 +00:00
color="red"
@click.native="deleteBodyParam(index)"
/>
</span>
2021-08-08 15:17:50 +00:00
</div>
<div
v-if="bodyParams.length === 0"
class="flex flex-col text-secondaryLight p-4 items-center justify-center"
>
<span class="text-center pb-4">
{{ $t("empty.body") }}
2021-08-08 15:17:50 +00:00
</span>
<ButtonSecondary
:label="$t('add.new')"
filled
2021-08-28 00:17:33 +00:00
svg="plus"
2021-08-08 15:17:50 +00:00
@click.native="addBodyParam"
/>
2021-07-10 18:08:35 +00:00
</div>
</AppSection>
</template>
<script lang="ts">
import { defineComponent, onMounted, Ref, watch } from "@nuxtjs/composition-api"
import { FormDataKeyValue } from "~/helpers/types/HoppRESTRequest"
import { pluckRef } from "~/helpers/utils/composables"
import {
addFormDataEntry,
deleteAllFormDataEntries,
deleteFormDataEntry,
updateFormDataEntry,
useRESTRequestBody,
} from "~/newstore/RESTSession"
import { useSetting } from "~/newstore/settings"
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,
EXPERIMENTAL_URL_BAR_ENABLED: useSetting("EXPERIMENTAL_URL_BAR_ENABLED"),
2021-07-10 18:08:35 +00:00
}
},
})
</script>
2021-05-18 09:27:29 +00:00
<style scoped lang="scss">
.file-chips-container {
2021-06-26 10:41:19 +00:00
@apply flex flex-1;
2021-05-18 09:27:29 +00:00
@apply whitespace-nowrap;
@apply overflow-auto;
2021-08-27 04:07:29 +00:00
@apply bg-transparent;
2021-05-18 09:27:29 +00:00
.file-chips-wrapper {
@apply flex;
@apply px-4;
@apply py-1;
2021-05-18 09:27:29 +00:00
@apply w-0;
}
}
</style>