blob: 50df9a42546af7fe4fb567bd9333bff5d3e92393 [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
42 <b-form-group
43 :label="$t('pageSslCertificates.modal.certificateFile')"
44 label-for="certificate-file"
45 >
46 <b-form-file
47 id="certificate-file"
48 v-model="form.file"
49 accept=".pem"
Mateusz Gapski47998e72020-07-23 11:04:48 +020050 :browse-text="$t('global.fileUpload.browseText')"
51 :drop-placeholder="$t('global.fileUpload.dropPlaceholder')"
52 :placeholder="$t('global.fileUpload.placeholder')"
Yoshie Muranaka37393812020-03-24 15:25:24 -070053 :state="getValidationState($v.form.file)"
54 />
55 <b-form-invalid-feedback role="alert">
56 <template v-if="!$v.form.file.required">
57 {{ $t('global.form.required') }}
58 </template>
59 </b-form-invalid-feedback>
60 </b-form-group>
61 </b-form>
Derick Montague602e98a2020-10-21 16:20:00 -050062 <template #modal-ok>
Yoshie Muranaka37393812020-03-24 15:25:24 -070063 <template v-if="certificate">
64 {{ $t('global.action.replace') }}
65 </template>
66 <template v-else>
67 {{ $t('global.action.add') }}
68 </template>
69 </template>
70 </b-modal>
71</template>
72
73<script>
74import { required, requiredIf } from 'vuelidate/lib/validators';
SurenNeware61859092020-10-01 09:37:32 +053075import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
Yoshie Muranaka37393812020-03-24 15:25:24 -070076
77export default {
78 mixins: [VuelidateMixin],
79 props: {
80 certificate: {
81 type: Object,
82 default: null,
Derick Montague602e98a2020-10-21 16:20:00 -050083 validator: (prop) => {
Yoshie Muranaka37393812020-03-24 15:25:24 -070084 if (prop === null) return true;
85 return (
Yoshie Muranakaefd7c882020-10-30 09:32:06 -070086 Object.prototype.hasOwnProperty.call(prop, 'type') &&
87 Object.prototype.hasOwnProperty.call(prop, 'certificate')
Yoshie Muranaka37393812020-03-24 15:25:24 -070088 );
Derick Montague602e98a2020-10-21 16:20:00 -050089 },
90 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070091 },
92 data() {
93 return {
94 form: {
95 certificateType: null,
Derick Montague602e98a2020-10-21 16:20:00 -050096 file: null,
97 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070098 };
99 },
100 computed: {
101 certificateTypes() {
102 return this.$store.getters['sslCertificates/availableUploadTypes'];
103 },
104 certificateOptions() {
105 return this.certificateTypes.map(({ type, label }) => {
106 return {
107 text: label,
Derick Montague602e98a2020-10-21 16:20:00 -0500108 value: type,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700109 };
110 });
Derick Montague602e98a2020-10-21 16:20:00 -0500111 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700112 },
113 watch: {
Derick Montague602e98a2020-10-21 16:20:00 -0500114 certificateOptions: function (options) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700115 if (options.length) {
116 this.form.certificateType = options[0].value;
117 }
Derick Montague602e98a2020-10-21 16:20:00 -0500118 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700119 },
120 validations() {
121 return {
122 form: {
123 certificateType: {
Derick Montague602e98a2020-10-21 16:20:00 -0500124 required: requiredIf(function () {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700125 return !this.certificate;
Derick Montague602e98a2020-10-21 16:20:00 -0500126 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700127 },
128 file: {
Derick Montague602e98a2020-10-21 16:20:00 -0500129 required,
130 },
131 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700132 };
133 },
134 methods: {
135 handleSubmit() {
136 this.$v.$touch();
137 if (this.$v.$invalid) return;
138 this.$emit('ok', {
139 addNew: !this.certificate,
140 file: this.form.file,
141 location: this.certificate ? this.certificate.location : null,
142 type: this.certificate
143 ? this.certificate.type
Derick Montague602e98a2020-10-21 16:20:00 -0500144 : this.form.certificateType,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700145 });
146 this.closeModal();
147 },
148 closeModal() {
149 this.$nextTick(() => {
150 this.$refs.modal.hide();
151 });
152 },
153 resetForm() {
154 this.form.certificateType = this.certificateOptions.length
155 ? this.certificateOptions[0].value
156 : null;
157 this.form.file = null;
158 this.$v.$reset();
159 },
160 onOk(bvModalEvt) {
161 // prevent modal close
162 bvModalEvt.preventDefault();
163 this.handleSubmit();
Derick Montague602e98a2020-10-21 16:20:00 -0500164 },
165 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700166};
167</script>