blob: cafbd9352728a83ae90854b5fe1a430e29b883ab [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>
Sukanya Pandey38357132020-12-22 13:40:59 +053065 <template #modal-cancel>
66 {{ $t('global.action.cancel') }}
67 </template>
Yoshie Muranaka37393812020-03-24 15:25:24 -070068 </b-modal>
69</template>
70
71<script>
72import { required, requiredIf } from 'vuelidate/lib/validators';
SurenNeware61859092020-10-01 09:37:32 +053073import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
Yoshie Muranaka37393812020-03-24 15:25:24 -070074
SurenNeware978807d2020-09-03 18:35:21 +053075import FormFile from '@/components/Global/FormFile';
76
Yoshie Muranaka37393812020-03-24 15:25:24 -070077export default {
SurenNeware978807d2020-09-03 18:35:21 +053078 components: { FormFile },
Yoshie Muranaka37393812020-03-24 15:25:24 -070079 mixins: [VuelidateMixin],
80 props: {
81 certificate: {
82 type: Object,
83 default: null,
Derick Montague602e98a2020-10-21 16:20:00 -050084 validator: (prop) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -070085 if (prop === null) return true;
86 return (
Yoshie Muranakaefd7c882020-10-30 09:32:06 -070087 Object.prototype.hasOwnProperty.call(prop, 'type') &&
88 Object.prototype.hasOwnProperty.call(prop, 'certificate')
Yoshie Muranaka37393812020-03-24 15:25:24 -070089 );
Derick Montague602e98a2020-10-21 16:20:00 -050090 },
91 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070092 },
93 data() {
94 return {
95 form: {
96 certificateType: null,
Derick Montague602e98a2020-10-21 16:20:00 -050097 file: null,
98 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070099 };
100 },
101 computed: {
102 certificateTypes() {
103 return this.$store.getters['sslCertificates/availableUploadTypes'];
104 },
105 certificateOptions() {
106 return this.certificateTypes.map(({ type, label }) => {
107 return {
108 text: label,
Derick Montague602e98a2020-10-21 16:20:00 -0500109 value: type,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700110 };
111 });
Derick Montague602e98a2020-10-21 16:20:00 -0500112 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700113 },
114 watch: {
Derick Montague602e98a2020-10-21 16:20:00 -0500115 certificateOptions: function (options) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700116 if (options.length) {
117 this.form.certificateType = options[0].value;
118 }
Derick Montague602e98a2020-10-21 16:20:00 -0500119 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700120 },
121 validations() {
122 return {
123 form: {
124 certificateType: {
Derick Montague602e98a2020-10-21 16:20:00 -0500125 required: requiredIf(function () {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700126 return !this.certificate;
Derick Montague602e98a2020-10-21 16:20:00 -0500127 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700128 },
129 file: {
Derick Montague602e98a2020-10-21 16:20:00 -0500130 required,
131 },
132 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700133 };
134 },
135 methods: {
136 handleSubmit() {
137 this.$v.$touch();
138 if (this.$v.$invalid) return;
139 this.$emit('ok', {
140 addNew: !this.certificate,
141 file: this.form.file,
142 location: this.certificate ? this.certificate.location : null,
143 type: this.certificate
144 ? this.certificate.type
Derick Montague602e98a2020-10-21 16:20:00 -0500145 : this.form.certificateType,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700146 });
147 this.closeModal();
148 },
149 closeModal() {
150 this.$nextTick(() => {
151 this.$refs.modal.hide();
152 });
153 },
154 resetForm() {
155 this.form.certificateType = this.certificateOptions.length
156 ? this.certificateOptions[0].value
157 : null;
158 this.form.file = null;
159 this.$v.$reset();
160 },
161 onOk(bvModalEvt) {
162 // prevent modal close
163 bvModalEvt.preventDefault();
164 this.handleSubmit();
Derick Montague602e98a2020-10-21 16:20:00 -0500165 },
166 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700167};
168</script>