blob: 8a71977558fab2979fca04915f7164c5d4dbcd83 [file] [log] [blame]
Yoshie Muranaka37393812020-03-24 15:25:24 -07001<template>
2 <b-modal id="upload-certificate" ref="modal" @ok="onOk" @hidden="resetForm">
Derick Montague602e98a2020-10-21 16:20:00 -05003 <template #modal-title>
Yoshie Muranaka37393812020-03-24 15:25:24 -07004 <template v-if="certificate">
5 {{ $t('pageSslCertificates.replaceCertificate') }}
6 </template>
7 <template v-else>
8 {{ $t('pageSslCertificates.addNewCertificate') }}
9 </template>
10 </template>
11 <b-form>
12 <!-- Replace Certificate type -->
13 <template v-if="certificate !== null">
14 <dl class="mb-4">
15 <dt>{{ $t('pageSslCertificates.modal.certificateType') }}</dt>
16 <dd>{{ certificate.certificate }}</dd>
17 </dl>
18 </template>
19
20 <!-- Add new Certificate type -->
21 <template v-else>
22 <b-form-group
23 :label="$t('pageSslCertificates.modal.certificateType')"
24 label-for="certificate-type"
25 >
26 <b-form-select
27 id="certificate-type"
28 v-model="form.certificateType"
29 :options="certificateOptions"
30 :state="getValidationState($v.form.certificateType)"
31 @input="$v.form.certificateType.$touch()"
32 >
33 </b-form-select>
34 <b-form-invalid-feedback role="alert">
35 <template v-if="!$v.form.certificateType.required">
36 {{ $t('global.form.fieldRequired') }}
37 </template>
38 </b-form-invalid-feedback>
39 </b-form-group>
40 </template>
41
SurenNeware978807d2020-09-03 18:35:21 +053042 <b-form-group :label="$t('pageSslCertificates.modal.certificateFile')">
43 <form-file
Yoshie Muranaka37393812020-03-24 15:25:24 -070044 id="certificate-file"
45 v-model="form.file"
46 accept=".pem"
Yoshie Muranaka37393812020-03-24 15:25:24 -070047 :state="getValidationState($v.form.file)"
SurenNeware978807d2020-09-03 18:35:21 +053048 >
49 <template #invalid>
50 <b-form-invalid-feedback role="alert">
51 {{ $t('global.form.required') }}
52 </b-form-invalid-feedback>
Yoshie Muranaka37393812020-03-24 15:25:24 -070053 </template>
SurenNeware978807d2020-09-03 18:35:21 +053054 </form-file>
Yoshie Muranaka37393812020-03-24 15:25:24 -070055 </b-form-group>
56 </b-form>
Derick Montague602e98a2020-10-21 16:20:00 -050057 <template #modal-ok>
Yoshie Muranaka37393812020-03-24 15:25:24 -070058 <template v-if="certificate">
59 {{ $t('global.action.replace') }}
60 </template>
61 <template v-else>
62 {{ $t('global.action.add') }}
63 </template>
64 </template>
65 </b-modal>
66</template>
67
68<script>
69import { required, requiredIf } from 'vuelidate/lib/validators';
SurenNeware61859092020-10-01 09:37:32 +053070import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
Yoshie Muranaka37393812020-03-24 15:25:24 -070071
SurenNeware978807d2020-09-03 18:35:21 +053072import FormFile from '@/components/Global/FormFile';
73
Yoshie Muranaka37393812020-03-24 15:25:24 -070074export default {
SurenNeware978807d2020-09-03 18:35:21 +053075 components: { FormFile },
Yoshie Muranaka37393812020-03-24 15:25:24 -070076 mixins: [VuelidateMixin],
77 props: {
78 certificate: {
79 type: Object,
80 default: null,
Derick Montague602e98a2020-10-21 16:20:00 -050081 validator: (prop) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -070082 if (prop === null) return true;
83 return (
Yoshie Muranakaefd7c882020-10-30 09:32:06 -070084 Object.prototype.hasOwnProperty.call(prop, 'type') &&
85 Object.prototype.hasOwnProperty.call(prop, 'certificate')
Yoshie Muranaka37393812020-03-24 15:25:24 -070086 );
Derick Montague602e98a2020-10-21 16:20:00 -050087 },
88 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070089 },
90 data() {
91 return {
92 form: {
93 certificateType: null,
Derick Montague602e98a2020-10-21 16:20:00 -050094 file: null,
95 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070096 };
97 },
98 computed: {
99 certificateTypes() {
100 return this.$store.getters['sslCertificates/availableUploadTypes'];
101 },
102 certificateOptions() {
103 return this.certificateTypes.map(({ type, label }) => {
104 return {
105 text: label,
Derick Montague602e98a2020-10-21 16:20:00 -0500106 value: type,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700107 };
108 });
Derick Montague602e98a2020-10-21 16:20:00 -0500109 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700110 },
111 watch: {
Derick Montague602e98a2020-10-21 16:20:00 -0500112 certificateOptions: function (options) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700113 if (options.length) {
114 this.form.certificateType = options[0].value;
115 }
Derick Montague602e98a2020-10-21 16:20:00 -0500116 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700117 },
118 validations() {
119 return {
120 form: {
121 certificateType: {
Derick Montague602e98a2020-10-21 16:20:00 -0500122 required: requiredIf(function () {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700123 return !this.certificate;
Derick Montague602e98a2020-10-21 16:20:00 -0500124 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700125 },
126 file: {
Derick Montague602e98a2020-10-21 16:20:00 -0500127 required,
128 },
129 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700130 };
131 },
132 methods: {
133 handleSubmit() {
134 this.$v.$touch();
135 if (this.$v.$invalid) return;
136 this.$emit('ok', {
137 addNew: !this.certificate,
138 file: this.form.file,
139 location: this.certificate ? this.certificate.location : null,
140 type: this.certificate
141 ? this.certificate.type
Derick Montague602e98a2020-10-21 16:20:00 -0500142 : this.form.certificateType,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700143 });
144 this.closeModal();
145 },
146 closeModal() {
147 this.$nextTick(() => {
148 this.$refs.modal.hide();
149 });
150 },
151 resetForm() {
152 this.form.certificateType = this.certificateOptions.length
153 ? this.certificateOptions[0].value
154 : null;
155 this.form.file = null;
156 this.$v.$reset();
157 },
158 onOk(bvModalEvt) {
159 // prevent modal close
160 bvModalEvt.preventDefault();
161 this.handleSubmit();
Derick Montague602e98a2020-10-21 16:20:00 -0500162 },
163 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700164};
165</script>