api-client/components/http/BodyParameters.vue

341 lines
8.6 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
top-upperSecondaryStickyFold
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
>
2021-08-08 15:17:50 +00:00
<label class="font-semibold">
2021-07-10 18:08:35 +00:00
{{ $t("request_body") }}
</label>
2021-07-24 16:46:48 +00:00
<div class="flex">
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('clear')"
icon="clear_all"
2021-08-08 15:17:50 +00:00
@click.native="clearContent"
/>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('add.new')"
icon="add"
@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"
:class="{ 'border-t': index == 0 }"
>
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="
bg-primaryLight
flex
font-semibold font-mono
flex-1
py-1
px-4
focus:outline-none
"
@change="
updateBodyParam(index, {
key: $event.target.value,
value: param.value,
active: param.active,
})
"
/>
2021-07-10 18:08:35 +00:00
<input
2021-08-08 15:17:50 +00:00
v-else
2021-07-10 18:08:35 +00:00
class="
bg-primaryLight
2021-07-17 17:40:28 +00:00
flex
2021-07-23 19:12:31 +00:00
font-semibold font-mono
2021-07-17 17:40:28 +00:00
flex-1
2021-07-24 05:53:10 +00:00
py-2
2021-07-17 17:40:28 +00:00
px-4
truncate
2021-07-10 18:08:35 +00:00
focus:outline-none
"
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,
})
"
/>
<SmartEnvInput
v-if="EXPERIMENTAL_URL_BAR_ENABLED && !requestBodyParamIsFile(index)"
v-model="param.value"
:placeholder="$t('count.value', { count: index + 1 })"
styles="
bg-primaryLight
flex
font-semibold font-mono
flex-1
py-1
px-4
focus:outline-none
"
@change="
updateBodyParam(index, {
key: param.key,
value: $event.target.value,
active: param.active,
})
"
2021-07-10 18:08:35 +00:00
/>
<input
2021-08-08 15:17:50 +00:00
v-if="!EXPERIMENTAL_URL_BAR_ENABLED && !requestBodyParamIsFile(index)"
2021-07-10 18:08:35 +00:00
class="
bg-primaryLight
2021-07-17 17:40:28 +00:00
flex
2021-07-23 19:12:31 +00:00
font-semibold font-mono
2021-07-17 17:40:28 +00:00
flex-1
2021-07-23 19:12:31 +00:00
py-2
2021-07-17 17:40:28 +00:00
px-4
truncate
2021-07-10 18:08:35 +00:00
focus:outline-none
"
2021-08-02 15:27:18 +00:00
:placeholder="$t('count.value', { count: index + 1 })"
2021-07-10 18:08:35 +00:00
:name="'value' + index"
:value="param.value"
@change="
2021-08-08 15:17:50 +00:00
updateBodyParam(index, {
key: param.key,
value: $event.target.value,
active: param.active,
})
2021-07-10 18:08:35 +00:00
"
/>
<div v-else class="file-chips-container">
<div class="file-chips-wrapper">
<SmartDeletableChip
2021-07-13 23:49:08 +00:00
v-for="(file, fileIndex) in Array.from(bodyParams[index].value)"
: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>
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')
"
:icon="
param.hasOwnProperty('active')
? param.active
? 'check_box'
: 'check_box_outline_blank'
: 'check_box'
"
color="green"
@click.native="
updateBodyParam(index, {
key: param.key,
value: param.value,
active: param.hasOwnProperty('active') ? !param.active : false,
})
"
/>
</span>
<span>
2021-07-10 18:08:35 +00:00
<label for="attachment" class="p-0">
2021-07-03 13:14:58 +00:00
<ButtonSecondary
2021-07-10 18:08:35 +00:00
class="w-full"
icon="attach_file"
@click.native="$refs.attachment[index].click()"
2021-07-03 13:14:58 +00:00
/>
2021-07-10 18:08:35 +00:00
</label>
<input
ref="attachment"
class="input"
name="attachment"
type="file"
multiple
@change="setRequestAttachment($event, index)"
/>
2021-08-11 08:46:43 +00:00
</span>
<span>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="$t('delete')"
icon="delete"
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"
>
<i class="opacity-75 pb-2 material-icons">post_add</i>
<span class="text-center pb-4">
{{ $t("empty.parameters") }}
</span>
<ButtonSecondary
:label="$t('add.new')"
outline
@click.native="addBodyParam"
/>
2021-07-10 18:08:35 +00:00
</div>
</AppSection>
</template>
2021-07-24 21:45:48 +00:00
<script>
export default {
props: {
bodyParams: { type: Array, default: () => [] },
},
2021-05-18 09:27:29 +00:00
computed: {
contentType() {
return this.$store.state.request.contentType
},
},
2021-07-10 18:08:35 +00:00
watch: {
bodyParams: {
handler(newValue) {
if (
2021-08-08 15:17:50 +00:00
(newValue[newValue.length - 1]?.key !== "" ||
newValue[newValue.length - 1]?.value !== "") &&
newValue.length
2021-07-10 18:08:35 +00:00
)
2021-08-08 15:17:50 +00:00
this.addBodyParam()
2021-07-10 18:08:35 +00:00
},
deep: true,
},
},
mounted() {
if (!this.bodyParams?.length) {
this.addRequestBodyParam()
}
},
methods: {
2021-08-08 15:17:50 +00:00
clearContent() {
this.$emit("clear-content")
},
2021-07-24 21:45:48 +00:00
removeRequestBodyParam(index) {
2021-05-15 12:43:31 +00:00
const paramArr = this.$store.state.request.bodyParams.filter(
2021-07-24 21:45:48 +00:00
(item, itemIndex) =>
2021-05-18 09:27:29 +00:00
itemIndex !== index &&
(Object.prototype.hasOwnProperty.call(item, "active")
? item.active === true
: true)
2021-05-15 12:43:31 +00:00
)
this.setRawParams(paramArr)
this.$emit("remove-request-body-param", index)
},
addRequestBodyParam() {
this.$emit("add-request-body-param")
},
2021-07-24 21:45:48 +00:00
setRequestAttachment(event, index) {
const { files } = event.target
this.$store.commit("setFilesBodyParams", {
index,
value: Array.from(files),
})
},
2021-07-24 21:45:48 +00:00
requestBodyParamIsFile(index) {
const bodyParamValue = this.bodyParams?.[index]?.value
const isFile = bodyParamValue?.[0] instanceof File
return isFile
},
2021-07-24 21:45:48 +00:00
chipDelete(paramIndex, fileIndex) {
this.$store.commit("removeFile", {
index: paramIndex,
fileIndex,
})
},
2021-07-24 21:45:48 +00:00
updateBodyParams(event, index, type) {
this.$store.commit(type, {
index,
value: event.target.value,
})
2021-07-24 21:45:48 +00:00
const paramArr = this.$store.state.request.bodyParams.filter((item) =>
Object.prototype.hasOwnProperty.call(item, "active")
? item.active === true
: true
2021-05-15 12:43:31 +00:00
)
this.setRawParams(paramArr)
},
2021-07-24 21:45:48 +00:00
toggleActive(index, param) {
2021-05-18 09:27:29 +00:00
const paramArr = this.$store.state.request.bodyParams.filter(
2021-07-24 21:45:48 +00:00
(item, itemIndex) => {
2021-05-18 09:27:29 +00:00
if (index === itemIndex) {
return !param.active
} else {
return Object.prototype.hasOwnProperty.call(item, "active")
? item.active === true
: true
}
2021-05-15 12:43:31 +00:00
}
2021-05-18 09:27:29 +00:00
)
2021-05-15 12:43:31 +00:00
this.setRawParams(paramArr)
2021-05-15 12:43:31 +00:00
this.$store.commit("setActiveBodyParams", {
index,
2021-05-18 09:27:29 +00:00
value: Object.prototype.hasOwnProperty.call(param, "active")
? !param.active
: false,
})
},
2021-07-24 21:45:48 +00:00
setRawParams(filteredParamArr) {
let rawParams = {}
2021-07-24 21:45:48 +00:00
filteredParamArr.forEach((_param) => {
2021-05-15 12:43:31 +00:00
rawParams = {
...rawParams,
2021-05-15 12:43:31 +00:00
[_param.key]: _param.value,
}
})
2021-05-15 12:43:31 +00:00
const rawParamsStr = JSON.stringify(rawParams, null, 2)
2021-05-18 09:27:29 +00:00
this.$store.commit("setState", {
value: rawParamsStr,
attribute: "rawParams",
})
},
},
2021-07-24 21:45:48 +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-06-12 16:46:17 +00:00
@apply bg-primaryDark;
2021-05-18 09:27:29 +00:00
.file-chips-wrapper {
@apply flex;
@apply w-0;
}
}
</style>