Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 1 | <template> |
| 2 | <b-modal id="upload-certificate" ref="modal" @ok="onOk" @hidden="resetForm"> |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 3 | <template #modal-title> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 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 Gapski | 47998e7 | 2020-07-23 11:04:48 +0200 | [diff] [blame] | 50 | :browse-text="$t('global.fileUpload.browseText')" |
| 51 | :drop-placeholder="$t('global.fileUpload.dropPlaceholder')" |
| 52 | :placeholder="$t('global.fileUpload.placeholder')" |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 53 | :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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 62 | <template #modal-ok> |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 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> |
| 74 | import { required, requiredIf } from 'vuelidate/lib/validators'; |
SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 75 | import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js'; |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 76 | |
| 77 | export default { |
| 78 | mixins: [VuelidateMixin], |
| 79 | props: { |
| 80 | certificate: { |
| 81 | type: Object, |
| 82 | default: null, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 83 | validator: (prop) => { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 84 | if (prop === null) return true; |
| 85 | return ( |
Yoshie Muranaka | efd7c88 | 2020-10-30 09:32:06 -0700 | [diff] [blame] | 86 | Object.prototype.hasOwnProperty.call(prop, 'type') && |
| 87 | Object.prototype.hasOwnProperty.call(prop, 'certificate') |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 88 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 89 | }, |
| 90 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 91 | }, |
| 92 | data() { |
| 93 | return { |
| 94 | form: { |
| 95 | certificateType: null, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 96 | file: null, |
| 97 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 98 | }; |
| 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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 108 | value: type, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 109 | }; |
| 110 | }); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 111 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 112 | }, |
| 113 | watch: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 114 | certificateOptions: function (options) { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 115 | if (options.length) { |
| 116 | this.form.certificateType = options[0].value; |
| 117 | } |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 118 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 119 | }, |
| 120 | validations() { |
| 121 | return { |
| 122 | form: { |
| 123 | certificateType: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 124 | required: requiredIf(function () { |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 125 | return !this.certificate; |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 126 | }), |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 127 | }, |
| 128 | file: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 129 | required, |
| 130 | }, |
| 131 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 132 | }; |
| 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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 144 | : this.form.certificateType, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 145 | }); |
| 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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 164 | }, |
| 165 | }, |
Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame] | 166 | }; |
| 167 | </script> |