Files
Nexior/src/components/api/Form.vue
T
2023-01-29 14:39:06 +08:00

244 lines
6.5 KiB
Vue

<template>
<div v-if="schema" class="wrapper">
<div v-for="(item, itemKey) in schema?.properties" :key="itemKey" class="item">
<div class="left">
<div class="info">
<span class="key">{{ itemKey }}</span>
<span class="type">{{ item?.type }}</span>
<span v-if="!item?.optional" class="required">required</span>
</div>
<div class="description">
{{ item?.title }}
</div>
</div>
<div class="right">
<div v-if="itemKey === 'api_key'">
<div v-if="!isApplied">
<el-popover placement="bottom" :width="200" trigger="click">
<p class="text-center mb-2">
<small>{{ $t('application.message.notApplied') }}</small>
</p>
<p class="text-center">
<router-link
:to="{
name: 'service-detail',
params: {
id: service?.id
}
}"
class="inline-block"
target="_blank"
>
<el-button size="small" type="primary">{{ $t('common.button.apply') }}</el-button>
</router-link>
</p>
<template #reference>
<el-input
v-model="value[itemKey?.toString()]"
:placeholder="$t('common.title.placeholderOfInput')"
class="inline-block w-4/5"
@focus="$emit('refresh-applications')"
/>
</template>
</el-popover>
<el-tooltip effect="dark" :content="$t('common.button.refresh')" placement="right">
<span class="inline-block">
<font-awesome-icon
icon="fa-solid fa-rotate-right"
class="text-sm cursor-pointer ml-1 color-primary"
@click="$emit('refresh-applications')"
/>
</span>
</el-tooltip>
</div>
<el-select
v-else
v-model="value[itemKey?.toString()]"
:clearable="true"
:filterable="true"
:allow-create="true"
:placeholder="$t('common.title.placeholderOfSelect')"
>
<el-option
v-if="appliedApplication?.api_key"
:label="appliedApplication?.api_key"
:value="appliedApplication.api_key"
class="select-option"
>
<span class="select-option-main">{{ appliedApplication?.api_key }}</span>
<span class="select-option-description">{{ $t('application.message.yourApplication') }}</span>
</el-option>
</el-select>
</div>
<div v-else>
<el-select
v-if="item?.enum"
v-model="value[itemKey?.toString()]"
clearable
:placeholder="$t('common.title.placeholderOfSelect')"
>
<el-option v-for="e in item?.enum" :key="e" :label="e" :value="e" />
</el-select>
<el-input v-else v-model="value[itemKey?.toString()]" />
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { ISchema } from '@/operators/api/models';
import { IService } from '@/operators/service/models';
import { IApplication } from '@/operators';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { ElPopover, ElInput, ElSelect, ElButton, ElTooltip, ElOption } from 'element-plus';
interface IData {
value: {
[key: string]: string | number;
};
}
export default defineComponent({
name: 'ApiForm',
components: {
FontAwesomeIcon,
ElPopover,
ElInput,
ElSelect,
ElButton,
ElTooltip,
ElOption
},
props: {
schema: {
type: Object as () => ISchema | undefined,
required: true
},
service: {
type: Object as () => IService | undefined,
required: true
},
applications: {
type: Array as () => IApplication[],
required: true
}
},
emits: ['update:form', 'refresh-applications'],
data(): IData {
return {
value: {}
};
},
computed: {
isApplied() {
return this.applications.filter((application) => application.service?.id === this.service?.id).length > 0;
},
appliedApplication(): IApplication | undefined {
const applications = this.applications.filter((application) => application.service?.id === this.service?.id);
if (applications.length > 0) {
return applications[0];
}
return undefined;
}
},
watch: {
value: {
handler() {
this.$emit('update:form', { ...this.value });
},
deep: true
}
},
methods: {
onChange(key: string, val: string) {
this.value[key] = val;
},
onRefreshApplications() {
this.$emit('refresh-applications');
}
}
});
</script>
<style lang="scss" scoped>
.wrapper {
overflow: hidden;
.item {
display: block;
overflow: hidden;
background-color: rgb(248, 248, 248);
padding: 10px;
border-right: 1px solid rgba(0, 0, 0, 0.1);
border-left: 1px solid rgba(0, 0, 0, 0.1);
&:first-child {
border-top: 1px solid rgba(0, 0, 0, 0.1);
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
&:not(:first-child):not(:last-child) {
border-top: 1px solid rgba(0, 0, 0, 0.1);
}
&:last-child {
border-top: 1px solid rgba(0, 0, 0, 0.1);
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
.left {
width: 70%;
float: left;
.info {
.key {
font-size: 14px;
font-weight: bold;
display: inline-block;
padding: 3px;
padding-left: 0;
}
.type {
font-size: 14px;
display: inline-block;
padding: 3px;
}
.required {
font-size: 12px;
color: #dd1e2e;
display: inline-block;
padding: 3px;
}
}
.description {
font-size: 12px;
color: #666;
}
}
.right {
width: 30%;
float: left;
padding-top: 6px;
}
}
}
.el-select-dropdown {
.select-option {
.select-option-main {
display: inline-block;
font-size: 14px;
}
.select-option-description {
display: inline-block;
font-size: 12px;
color: #999;
margin-left: 20px;
}
}
}
</style>