blob: 653a232f2e37a73f61edf5571b6ef4b2cb966aa4 [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"
50 plain
51 :state="getValidationState($v.form.file)"
52 />
53 <b-form-invalid-feedback role="alert">
54 <template v-if="!$v.form.file.required">
55 {{ $t('global.form.required') }}
56 </template>
57 </b-form-invalid-feedback>
58 </b-form-group>
59 </b-form>
60 <template v-slot:modal-ok>
61 <template v-if="certificate">
62 {{ $t('global.action.replace') }}
63 </template>
64 <template v-else>
65 {{ $t('global.action.add') }}
66 </template>
67 </template>
68 </b-modal>
69</template>
70
71<script>
72import { required, requiredIf } from 'vuelidate/lib/validators';
73import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js';
74
75export default {
76 mixins: [VuelidateMixin],
77 props: {
78 certificate: {
79 type: Object,
80 default: null,
81 validator: prop => {
82 if (prop === null) return true;
83 return (
84 prop.hasOwnProperty('type') && prop.hasOwnProperty('certificate')
85 );
86 }
87 }
88 },
89 data() {
90 return {
91 form: {
92 certificateType: null,
93 file: null
94 }
95 };
96 },
97 computed: {
98 certificateTypes() {
99 return this.$store.getters['sslCertificates/availableUploadTypes'];
100 },
101 certificateOptions() {
102 return this.certificateTypes.map(({ type, label }) => {
103 return {
104 text: label,
105 value: type
106 };
107 });
108 }
109 },
110 watch: {
111 certificateOptions: function(options) {
112 if (options.length) {
113 this.form.certificateType = options[0].value;
114 }
115 }
116 },
117 validations() {
118 return {
119 form: {
120 certificateType: {
121 required: requiredIf(function() {
122 return !this.certificate;
123 })
124 },
125 file: {
126 required
127 }
128 }
129 };
130 },
131 methods: {
132 handleSubmit() {
133 this.$v.$touch();
134 if (this.$v.$invalid) return;
135 this.$emit('ok', {
136 addNew: !this.certificate,
137 file: this.form.file,
138 location: this.certificate ? this.certificate.location : null,
139 type: this.certificate
140 ? this.certificate.type
141 : this.form.certificateType
142 });
143 this.closeModal();
144 },
145 closeModal() {
146 this.$nextTick(() => {
147 this.$refs.modal.hide();
148 });
149 },
150 resetForm() {
151 this.form.certificateType = this.certificateOptions.length
152 ? this.certificateOptions[0].value
153 : null;
154 this.form.file = null;
155 this.$v.$reset();
156 },
157 onOk(bvModalEvt) {
158 // prevent modal close
159 bvModalEvt.preventDefault();
160 this.handleSubmit();
161 }
162 }
163};
164</script>