api-client/components/http/CodegenModal.vue

149 lines
4 KiB
Vue
Raw Normal View History

<template>
<SmartModal v-if="show" @close="hideModal">
<template #header>
<h3 class="heading">{{ $t("generate_code") }}</h3>
2021-07-09 17:19:45 +00:00
<ButtonSecondary icon="close" @click.native="hideModal" />
</template>
<template #body>
2021-07-17 17:40:28 +00:00
<div class="flex flex-col px-2">
<label for="requestType" class="font-semibold text-xs px-4 pb-4">
2021-07-09 17:19:45 +00:00
{{ $t("choose_language") }}
</label>
<div class="flex flex-1">
<span class="select-wrapper">
<tippy
ref="options"
interactive
tabindex="-1"
trigger="click"
theme="popover"
arrow
>
<template #trigger>
<span
class="
2021-07-17 17:40:28 +00:00
bg-primaryLight
border-b border-dividerLight
rounded-lg
cursor-pointer
2021-07-09 17:19:45 +00:00
flex
2021-07-17 17:40:28 +00:00
font-semibold
2021-07-09 17:19:45 +00:00
text-xs
2021-07-17 17:40:28 +00:00
w-full
2021-07-09 17:19:45 +00:00
py-3
2021-07-17 17:40:28 +00:00
px-4
2021-07-09 17:19:45 +00:00
focus:outline-none
"
>
{{ codegens.find((x) => x.id === requestType).name }}
</span>
</template>
<SmartItem
2021-07-13 23:49:08 +00:00
v-for="(gen, index) in codegens"
:key="`gen-${index}`"
2021-07-09 17:19:45 +00:00
:label="gen.name"
@click.native="
requestType = gen.id
$refs.options.tippy().hide()
"
/>
</tippy>
</span>
</div>
2021-07-17 17:40:28 +00:00
<div class="flex flex-1 justify-between">
2021-07-09 17:19:45 +00:00
<label
for="generatedCode"
2021-07-17 17:40:28 +00:00
class="font-semibold text-xs px-4 pt-4 pb-4"
2021-07-09 17:19:45 +00:00
>
{{ $t("generated_code") }}
</label>
2021-07-03 13:14:58 +00:00
<ButtonSecondary
2020-12-11 10:29:03 +00:00
ref="copyRequestCode"
2021-07-02 16:30:08 +00:00
v-tippy="{ theme: 'tooltip' }"
:title="$t('copy_code')"
2021-07-05 12:56:00 +00:00
:icon="copyIcon"
2021-07-03 13:14:58 +00:00
@click.native="copyRequestCode"
/>
2020-12-11 10:29:03 +00:00
</div>
2021-07-09 17:19:45 +00:00
<SmartAceEditor
v-if="requestType"
ref="generatedCode"
:value="requestCode"
:lang="codegens.find((x) => x.id === requestType).language"
:options="{
maxLines: '10',
minLines: '10',
2021-07-11 22:50:00 +00:00
fontSize: '14px',
2021-07-09 17:19:45 +00:00
autoScrollEditorIntoView: true,
readOnly: true,
showPrintMargin: false,
useWorker: false,
}"
styles="rounded-lg"
/>
2020-12-11 10:29:03 +00:00
</div>
</template>
</SmartModal>
</template>
2021-07-18 04:33:52 +00:00
<script lang="ts">
import { defineComponent } from "@nuxtjs/composition-api"
import { codegens } from "~/helpers/codegen/codegen"
2021-07-18 04:33:52 +00:00
import { getRESTRequest } from "~/newstore/RESTSession"
import { getEffectiveRESTRequest } from "~/helpers/utils/EffectiveURL"
import { getCurrentEnvironment } from "~/newstore/environments"
2021-07-18 04:33:52 +00:00
export default defineComponent({
props: {
show: Boolean,
requestTypeProp: { type: String, default: "curl" },
},
data() {
return {
codegens,
2021-06-29 11:34:02 +00:00
copyIcon: "content_copy",
2021-07-18 04:33:52 +00:00
request: getRESTRequest(),
}
},
computed: {
requestType: {
2021-07-18 04:33:52 +00:00
get(): string {
return this.requestTypeProp
},
2021-07-18 04:33:52 +00:00
set(val: string) {
this.$emit("set-request-type", val)
},
},
},
2021-07-18 04:33:52 +00:00
watch: {
show(goingToShow) {
if (goingToShow) {
this.request = getRESTRequest()
}
},
},
methods: {
2021-07-18 04:33:52 +00:00
requestCode() {
return getEffectiveRESTRequest(this.request, getCurrentEnvironment())
},
hideModal() {
this.$emit("hide-modal")
},
handleImport() {
this.$emit("handle-import")
},
copyRequestCode() {
2021-07-18 04:33:52 +00:00
;(this.$refs.generatedCode as any).editor
.selectAll()(this.$refs.generatedCode as any)
.editor.focus()
document.execCommand("copy")
2021-06-29 11:34:02 +00:00
this.copyIcon = "done"
2021-07-18 04:33:52 +00:00
this.$toast.success(this.$t("copied_to_clipboard").toString(), {
2021-06-29 11:34:02 +00:00
icon: "done",
})
setTimeout(() => (this.copyIcon = "content_copy"), 1000)
},
},
2021-07-18 04:33:52 +00:00
})
</script>