api-client/components/http/BodyParameters.vue

266 lines
6.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-07-17 17:40:28 +00:00
pl-4
2021-07-26 03:38:06 +00:00
top-24
2021-07-17 17:40:28 +00:00
z-10
sticky
items-center
justify-between
"
2021-07-10 18:08:35 +00:00
>
<label for="reqParamList" 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"
@click.native="clearContent('bodyParams', $event)"
/>
</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-07-10 18:08:35 +00:00
<input
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
2021-07-10 18:08:35 +00:00
focus:outline-none
"
:placeholder="$t('parameter_count', { count: index + 1 })"
:name="'param' + index"
:value="param.key"
autofocus
@change="updateBodyParams($event, index, `setKeyBodyParams`)"
@keyup.prevent="setRouteQueryState"
/>
<input
v-if="!requestBodyParamIsFile(index)"
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
2021-07-10 18:08:35 +00:00
focus:outline-none
"
:placeholder="$t('value_count', { count: index + 1 })"
:name="'value' + index"
:value="param.value"
@change="
// if input is form data, set value to be an array containing the value
// only
updateBodyParams($event, index, `setValueBodyParams`)
"
@keyup.prevent="setRouteQueryState"
/>
<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>
<div>
2021-07-10 18:08:35 +00:00
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
:title="
param.hasOwnProperty('active')
? param.active
? $t('turn_off')
: $t('turn_on')
: $t('turn_off')
"
:icon="
param.hasOwnProperty('active')
? param.active
? 'check_box'
: 'check_box_outline_blank'
: 'check_box'
"
color="green"
2021-07-10 18:08:35 +00:00
@click.native="toggleActive(index, param)"
/>
</div>
2021-07-22 18:37:39 +00:00
<div>
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)"
/>
</div>
2021-07-10 18:08:35 +00:00
<div>
2021-07-03 13:14:58 +00:00
<ButtonSecondary
2021-07-10 18:08:35 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('delete')"
icon="delete"
color="red"
2021-07-10 18:08:35 +00:00
@click.native="removeRequestBodyParam(index)"
2021-07-03 13:14:58 +00:00
/>
2021-07-10 18:08:35 +00:00
</div>
</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 (
newValue[newValue.length - 1]?.key !== "" ||
newValue[newValue.length - 1]?.value !== ""
)
this.addRequestBodyParam()
},
deep: true,
},
},
mounted() {
if (!this.bodyParams?.length) {
this.addRequestBodyParam()
}
},
methods: {
2021-07-24 21:45:48 +00:00
clearContent(bodyParams, $event) {
this.$emit("clear-content", bodyParams, $event)
},
setRouteQueryState() {
this.$emit("set-route-query-state")
},
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>