blob: 070dd0dc31070f4b4681a404c417f2800d03a609 [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 (
86 prop.hasOwnProperty('type') && prop.hasOwnProperty('certificate')
87 );
Derick Montague602e98a2020-10-21 16:20:00 -050088 },
89 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070090 },
91 data() {
92 return {
93 form: {
94 certificateType: null,
Derick Montague602e98a2020-10-21 16:20:00 -050095 file: null,
96 },
Yoshie Muranaka37393812020-03-24 15:25:24 -070097 };
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,
Derick Montague602e98a2020-10-21 16:20:00 -0500107 value: type,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700108 };
109 });
Derick Montague602e98a2020-10-21 16:20:00 -0500110 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700111 },
112 watch: {
Derick Montague602e98a2020-10-21 16:20:00 -0500113 certificateOptions: function (options) {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700114 if (options.length) {
115 this.form.certificateType = options[0].value;
116 }
Derick Montague602e98a2020-10-21 16:20:00 -0500117 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700118 },
119 validations() {
120 return {
121 form: {
122 certificateType: {
Derick Montague602e98a2020-10-21 16:20:00 -0500123 required: requiredIf(function () {
Yoshie Muranaka37393812020-03-24 15:25:24 -0700124 return !this.certificate;
Derick Montague602e98a2020-10-21 16:20:00 -0500125 }),
Yoshie Muranaka37393812020-03-24 15:25:24 -0700126 },
127 file: {
Derick Montague602e98a2020-10-21 16:20:00 -0500128 required,
129 },
130 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700131 };
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
Derick Montague602e98a2020-10-21 16:20:00 -0500143 : this.form.certificateType,
Yoshie Muranaka37393812020-03-24 15:25:24 -0700144 });
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();
Derick Montague602e98a2020-10-21 16:20:00 -0500163 },
164 },
Yoshie Muranaka37393812020-03-24 15:25:24 -0700165};
166</script>