Yoshie Muranaka | 3739381 | 2020-03-24 15:25:24 -0700 | [diff] [blame^] | 1 | <template> |
| 2 | <b-container fluid> |
| 3 | <page-title /> |
| 4 | <b-row> |
| 5 | <b-col xl="9" class="text-right"> |
| 6 | <b-button |
| 7 | variant="primary" |
| 8 | :disabled="certificatesForUpload.length === 0" |
| 9 | @click="initModalUploadCertificate(null)" |
| 10 | > |
| 11 | <icon-add /> |
| 12 | {{ $t('pageSslCertificates.addNewCertificate') }} |
| 13 | </b-button> |
| 14 | </b-col> |
| 15 | </b-row> |
| 16 | <b-row> |
| 17 | <b-col xl="9"> |
| 18 | <b-table :fields="fields" :items="tableItems"> |
| 19 | <template v-slot:cell(validFrom)="{ value }"> |
| 20 | {{ value | formatDate }} |
| 21 | </template> |
| 22 | |
| 23 | <template v-slot:cell(validUntil)="{ value }"> |
| 24 | {{ value | formatDate }} |
| 25 | </template> |
| 26 | |
| 27 | <template v-slot:cell(actions)="{ value, item }"> |
| 28 | <table-row-action |
| 29 | v-for="(action, index) in value" |
| 30 | :key="index" |
| 31 | :value="action.value" |
| 32 | :title="action.title" |
| 33 | :enabled="action.enabled" |
| 34 | @click:tableAction="onTableRowAction($event, item)" |
| 35 | > |
| 36 | <template v-slot:icon> |
| 37 | <icon-replace v-if="action.value === 'replace'" /> |
| 38 | <icon-trashcan v-if="action.value === 'delete'" /> |
| 39 | </template> |
| 40 | </table-row-action> |
| 41 | </template> |
| 42 | </b-table> |
| 43 | </b-col> |
| 44 | </b-row> |
| 45 | |
| 46 | <!-- Modals --> |
| 47 | <modal-upload-certificate :certificate="modalCertificate" @ok="onModalOk" /> |
| 48 | </b-container> |
| 49 | </template> |
| 50 | |
| 51 | <script> |
| 52 | import IconAdd from '@carbon/icons-vue/es/add--alt/20'; |
| 53 | import IconReplace from '@carbon/icons-vue/es/renew/20'; |
| 54 | import IconTrashcan from '@carbon/icons-vue/es/trash-can/20'; |
| 55 | |
| 56 | import ModalUploadCertificate from './ModalUploadCertificate'; |
| 57 | import PageTitle from '../../../components/Global/PageTitle'; |
| 58 | import TableRowAction from '../../../components/Global/TableRowAction'; |
| 59 | |
| 60 | import BVToastMixin from '../../../components/Mixins/BVToastMixin'; |
| 61 | |
| 62 | export default { |
| 63 | name: 'SslCertificates', |
| 64 | components: { |
| 65 | IconAdd, |
| 66 | IconReplace, |
| 67 | IconTrashcan, |
| 68 | ModalUploadCertificate, |
| 69 | PageTitle, |
| 70 | TableRowAction |
| 71 | }, |
| 72 | mixins: [BVToastMixin], |
| 73 | data() { |
| 74 | return { |
| 75 | modalCertificate: null, |
| 76 | fields: [ |
| 77 | { |
| 78 | key: 'certificate', |
| 79 | label: this.$t('pageSslCertificates.table.certificate') |
| 80 | }, |
| 81 | { |
| 82 | key: 'issuedBy', |
| 83 | label: this.$t('pageSslCertificates.table.issuedBy') |
| 84 | }, |
| 85 | { |
| 86 | key: 'issuedTo', |
| 87 | label: this.$t('pageSslCertificates.table.issuedTo') |
| 88 | }, |
| 89 | { |
| 90 | key: 'validFrom', |
| 91 | label: this.$t('pageSslCertificates.table.validFrom') |
| 92 | }, |
| 93 | { |
| 94 | key: 'validUntil', |
| 95 | label: this.$t('pageSslCertificates.table.validUntil') |
| 96 | }, |
| 97 | { |
| 98 | key: 'actions', |
| 99 | label: '', |
| 100 | tdClass: 'text-right' |
| 101 | } |
| 102 | ] |
| 103 | }; |
| 104 | }, |
| 105 | computed: { |
| 106 | certificates() { |
| 107 | return this.$store.getters['sslCertificates/allCertificates']; |
| 108 | }, |
| 109 | tableItems() { |
| 110 | return this.certificates.map(certificate => { |
| 111 | return { |
| 112 | ...certificate, |
| 113 | actions: [ |
| 114 | { |
| 115 | value: 'replace', |
| 116 | title: this.$t('pageSslCertificates.replaceCertificate') |
| 117 | }, |
| 118 | { |
| 119 | value: 'delete', |
| 120 | title: this.$t('pageSslCertificates.deleteCertificate'), |
| 121 | enabled: |
| 122 | certificate.type === 'TrustStore Certificate' ? true : false |
| 123 | } |
| 124 | ] |
| 125 | }; |
| 126 | }); |
| 127 | }, |
| 128 | certificatesForUpload() { |
| 129 | return this.$store.getters['sslCertificates/availableUploadTypes']; |
| 130 | } |
| 131 | }, |
| 132 | created() { |
| 133 | this.$store.dispatch('sslCertificates/getCertificates'); |
| 134 | }, |
| 135 | methods: { |
| 136 | onTableRowAction(event, rowItem) { |
| 137 | switch (event) { |
| 138 | case 'replace': |
| 139 | this.initModalUploadCertificate(rowItem); |
| 140 | break; |
| 141 | case 'delete': |
| 142 | this.initModalDeleteCertificate(rowItem); |
| 143 | break; |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | }, |
| 148 | initModalUploadCertificate(certificate = null) { |
| 149 | this.modalCertificate = certificate; |
| 150 | this.$bvModal.show('upload-certificate'); |
| 151 | }, |
| 152 | initModalDeleteCertificate(certificate) { |
| 153 | this.$bvModal |
| 154 | .msgBoxConfirm( |
| 155 | this.$t('pageSslCertificates.modal.deleteConfirmMessage', { |
| 156 | issuedBy: certificate.issuedBy, |
| 157 | certificate: certificate.certificate |
| 158 | }), |
| 159 | { |
| 160 | title: this.$t('pageSslCertificates.deleteCertificate'), |
| 161 | okTitle: this.$t('global.action.delete') |
| 162 | } |
| 163 | ) |
| 164 | .then(deleteConfirmed => { |
| 165 | if (deleteConfirmed) this.deleteCertificate(certificate); |
| 166 | }); |
| 167 | }, |
| 168 | onModalOk({ addNew, file, type, location }) { |
| 169 | if (addNew) { |
| 170 | // Upload a new certificate |
| 171 | this.addNewCertificate(file, type); |
| 172 | } else { |
| 173 | // Replace an existing certificate |
| 174 | this.replaceCertificate(file, type, location); |
| 175 | } |
| 176 | }, |
| 177 | addNewCertificate(file, type) { |
| 178 | this.$store |
| 179 | .dispatch('sslCertificates/addNewCertificate', { file, type }) |
| 180 | .then(success => this.successToast(success)) |
| 181 | .catch(({ message }) => this.errorToast(message)); |
| 182 | }, |
| 183 | replaceCertificate(file, type, location) { |
| 184 | const reader = new FileReader(); |
| 185 | reader.readAsBinaryString(file); |
| 186 | reader.onloadend = event => { |
| 187 | const certificateString = event.target.result; |
| 188 | this.$store |
| 189 | .dispatch('sslCertificates/replaceCertificate', { |
| 190 | certificateString, |
| 191 | type, |
| 192 | location |
| 193 | }) |
| 194 | .then(success => this.successToast(success)) |
| 195 | .catch(({ message }) => this.errorToast(message)); |
| 196 | }; |
| 197 | }, |
| 198 | deleteCertificate({ type, location }) { |
| 199 | this.$store |
| 200 | .dispatch('sslCertificates/deleteCertificate', { |
| 201 | type, |
| 202 | location |
| 203 | }) |
| 204 | .then(success => this.successToast(success)) |
| 205 | .catch(({ message }) => this.errorToast(message)); |
| 206 | } |
| 207 | } |
| 208 | }; |
| 209 | </script> |