blob: 63c3b4e8878ed1621fc07786ddc55dd940692f88 [file] [log] [blame]
Yoshie Muranaka37393812020-03-24 15:25:24 -07001<template>
2 <b-modal id="upload-certificate" ref="modal" @ok="onOk" @hidden="resetForm">
3 <template v-slot:modal-title>
4 <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>
62 <template v-slot:modal-ok>
63 <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,
83 validator: prop => {
84 if (prop === null) return true;
85 return (
86 prop.hasOwnProperty('type') && prop.hasOwnProperty('certificate')
87 );
88 }
89 }
90 },
91 data() {
92 return {
93 form: {
94 certificateType: null,
95 file: null
96 }
97 };
98 },
99 computed: {
100 certificateTypes() {
101 return this.$store.getters['sslCertificates/availableUploadTypes'];
102 },
103 certificateOptions() {
104 return this.certificateTypes.map(({ type, label }) => {
105 return {
106 text: label,
107 value: type
108 };
109 });
110 }
111 },
112 watch: {
113 certificateOptions: function(options) {
114 if (options.length) {
115 this.form.certificateType = options[0].value;
116 }
117 }
118 },
119 validations() {
120 return {
121 form: {
122 certificateType: {
123 required: requiredIf(function() {
124 return !this.certificate;
125 })
126 },
127 file: {
128 required
129 }
130 }
131 };
132 },
133 methods: {
134 handleSubmit() {
135 this.$v.$touch();
136 if (this.$v.$invalid) return;
137 this.$emit('ok', {
138 addNew: !this.certificate,
139 file: this.form.file,
140 location: this.certificate ? this.certificate.location : null,
141 type: this.certificate
142 ? this.certificate.type
143 : this.form.certificateType
144 });
145 this.closeModal();
146 },
147 closeModal() {
148 this.$nextTick(() => {
149 this.$refs.modal.hide();
150 });
151 },
152 resetForm() {
153 this.form.certificateType = this.certificateOptions.length
154 ? this.certificateOptions[0].value
155 : null;
156 this.form.file = null;
157 this.$v.$reset();
158 },
159 onOk(bvModalEvt) {
160 // prevent modal close
161 bvModalEvt.preventDefault();
162 this.handleSubmit();
163 }
164 }
165};
166</script>