|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <a-spin :spinning="loading"> |
| 20 | + <a-form |
| 21 | + class="form" |
| 22 | + layout="vertical" |
| 23 | + :ref="formRef" |
| 24 | + :model="form" |
| 25 | + :rules="rules" |
| 26 | + > |
| 27 | + <a-form-item ref="name" name="name"> |
| 28 | + <template #label> |
| 29 | + <tooltip-label :title="$t('label.name')"/> |
| 30 | + </template> |
| 31 | + <a-input |
| 32 | + v-focus="true" |
| 33 | + v-model:value="form.name" |
| 34 | + :placeholder="$t('label.project.name')" /> |
| 35 | + </a-form-item> |
| 36 | + <a-form-item> |
| 37 | + <template #label> |
| 38 | + <tooltip-label :title="$t('label.description')" :tooltip="$t('label.project.description.tooltip')"/> |
| 39 | + </template> |
| 40 | + <a-input |
| 41 | + v-model:value="form.displaytext" |
| 42 | + :placeholder="$t('label.description')"/> |
| 43 | + </a-form-item> |
| 44 | + <ownership-selection v-if="isAdminOrDomainAdmin()" @fetch-owner="fetchOwner" :show-owner-type-field="false" /> |
| 45 | + <a-form-item v-if="Object.keys(this.selectedAccount).length !== 0" ref="userid" name="userid"> |
| 46 | + <template #label> |
| 47 | + <tooltip-label :title="$t('label.user')" :tooltip="$t('label.project.user.tooltip')"/> |
| 48 | + </template> |
| 49 | + <a-select |
| 50 | + v-model:value="form.userid" |
| 51 | + :loading="loading" |
| 52 | + showSearch |
| 53 | + :placeholder="this.$t('label.user')" |
| 54 | + > |
| 55 | + <a-select-option v-for="user in selectedAccount.user" :value="user.id" :key="user.id"> |
| 56 | + {{ user.username }} |
| 57 | + </a-select-option> |
| 58 | + </a-select> |
| 59 | + </a-form-item> |
| 60 | + <div :span="24" class="action-button"> |
| 61 | + <a-button @click="closeAction">{{ $t('label.cancel') }}</a-button> |
| 62 | + <a-button :loading="loading" ref="submit" type="primary" @click="handleSubmit">{{ $t('label.ok') }}</a-button> |
| 63 | + </div> |
| 64 | + </a-form> |
| 65 | + </a-spin> |
| 66 | +</template> |
| 67 | + |
| 68 | +<script> |
| 69 | +
|
| 70 | +import { reactive, ref, toRaw } from 'vue' |
| 71 | +import { api } from '@/api' |
| 72 | +import TooltipLabel from '@/components/widgets/TooltipLabel.vue' |
| 73 | +import ResourceIcon from '@/components/view/ResourceIcon.vue' |
| 74 | +import OwnershipSelection from '@/views/compute/wizard/OwnershipSelection.vue' |
| 75 | +import { isAdminOrDomainAdmin } from '@/role' |
| 76 | +
|
| 77 | +export default { |
| 78 | + name: 'CreateProject', |
| 79 | + components: { OwnershipSelection, ResourceIcon, TooltipLabel }, |
| 80 | + data () { |
| 81 | + return { |
| 82 | + loading: false, |
| 83 | + selectedAccount: {} |
| 84 | + } |
| 85 | + }, |
| 86 | + created () { |
| 87 | + this.initForm() |
| 88 | + }, |
| 89 | + methods: { |
| 90 | + initForm () { |
| 91 | + this.formRef = ref() |
| 92 | + this.form = reactive({}) |
| 93 | + this.rules = reactive({ |
| 94 | + name: [{ required: true, message: this.$t('message.error.required.project.name') }] |
| 95 | + }) |
| 96 | + }, |
| 97 | + fetchOwner (ownerData) { |
| 98 | + if (ownerData.selectedAccountType === 'Account') { |
| 99 | + if (!ownerData.selectedAccount) { |
| 100 | + this.selectedAccount = {} |
| 101 | + return |
| 102 | + } |
| 103 | +
|
| 104 | + this.selectedAccount = ownerData.accounts.find(acc => acc.name === ownerData.selectedAccount) |
| 105 | + this.form.account = ownerData.selectedAccount |
| 106 | + this.form.domainId = ownerData.selectedDomain |
| 107 | + } |
| 108 | + }, |
| 109 | + isAdminOrDomainAdmin () { |
| 110 | + return isAdminOrDomainAdmin() |
| 111 | + }, |
| 112 | + handleSubmit (e) { |
| 113 | + e.preventDefault() |
| 114 | + if (this.loading) return |
| 115 | +
|
| 116 | + this.formRef.value.validate().then(() => { |
| 117 | + this.loading = true |
| 118 | + const params = toRaw(this.form) |
| 119 | +
|
| 120 | + if (isAdminOrDomainAdmin()) { |
| 121 | + params.accountid = this.selectedAccount.id |
| 122 | + } |
| 123 | +
|
| 124 | + api('createProject', {}, 'POST', params).then(() => { |
| 125 | + this.$message.success(this.$t('message.success.project.creation')) |
| 126 | + this.closeAction() |
| 127 | + this.$emit('refresh-data') |
| 128 | + }).catch(error => { |
| 129 | + this.$notifyError(error) |
| 130 | + }).finally(() => { |
| 131 | + this.loading = false |
| 132 | + }) |
| 133 | + }) |
| 134 | + }, |
| 135 | + closeAction () { |
| 136 | + this.$emit('close-action') |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +
|
| 141 | +</script> |
| 142 | + |
| 143 | +<style lang="less" scoped> |
| 144 | + .form { |
| 145 | + width: 20vw; |
| 146 | +
|
| 147 | + } |
| 148 | +</style> |
0 commit comments