|
| 1 | +import {Component, OnInit, TemplateRef, ViewChild} from '@angular/core'; |
| 2 | +import {NgbModal, NgbModalRef} from '@ng-bootstrap/ng-bootstrap'; |
| 3 | +import * as moment from 'moment'; |
| 4 | +import {UtmToastService} from '../../shared/alert/utm-toast.service'; |
| 5 | +import { |
| 6 | + ModalConfirmationComponent |
| 7 | +} from '../../shared/components/utm/util/modal-confirmation/modal-confirmation.component'; |
| 8 | +import {ITEMS_PER_PAGE} from '../../shared/constants/pagination.constants'; |
| 9 | +import {SortEvent} from '../../shared/directives/sortable/type/sort-event'; |
| 10 | +import {ApiKeyModalComponent} from './shared/components/api-key-modal/api-key-modal.component'; |
| 11 | +import {ApiKeyResponse} from './shared/models/ApiKeyResponse'; |
| 12 | +import {ApiKeysService} from './shared/service/api-keys.service'; |
| 13 | + |
| 14 | +@Component({ |
| 15 | + selector: 'app-api-keys', |
| 16 | + templateUrl: './api-keys.component.html', |
| 17 | + styleUrls: ['./api-keys.component.scss'] |
| 18 | +}) |
| 19 | +export class ApiKeysComponent implements OnInit { |
| 20 | + |
| 21 | + generating: string[] = []; |
| 22 | + noData = false; |
| 23 | + apiKeys: ApiKeyResponse[] = []; |
| 24 | + loading = false; |
| 25 | + generatedApiKey = ''; |
| 26 | + @ViewChild('generatedModal') generatedModal!: TemplateRef<any>; |
| 27 | + generatedModalRef!: NgbModalRef; |
| 28 | + copied = false; |
| 29 | + readonly itemsPerPage = ITEMS_PER_PAGE; |
| 30 | + totalItems = 0; |
| 31 | + page = 0; |
| 32 | + size = this.itemsPerPage; |
| 33 | + |
| 34 | + request = { |
| 35 | + sort: 'createdAt,desc', |
| 36 | + page: this.page, |
| 37 | + size: this.size |
| 38 | + }; |
| 39 | + |
| 40 | + constructor( private toastService: UtmToastService, |
| 41 | + private apiKeyService: ApiKeysService, |
| 42 | + private modalService: NgbModal |
| 43 | + ) {} |
| 44 | + |
| 45 | + ngOnInit(): void { |
| 46 | + this.loadKeys(); |
| 47 | + } |
| 48 | + |
| 49 | + loadKeys(): void { |
| 50 | + this.loading = true; |
| 51 | + this.apiKeyService.list(this.request).subscribe({ |
| 52 | + next: (res) => { |
| 53 | + this.totalItems = Number(res.headers.get('X-Total-Count')); |
| 54 | + this.apiKeys = res.body || []; |
| 55 | + this.noData = this.apiKeys.length === 0; |
| 56 | + this.loading = false; |
| 57 | + }, |
| 58 | + error: () => { |
| 59 | + this.loading = false; |
| 60 | + this.apiKeys = []; |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + copyToClipboard(): void { |
| 66 | + if (!this.generatedApiKey) { return; } |
| 67 | + |
| 68 | + if (navigator && (navigator as any).clipboard && (navigator as any).clipboard.writeText) { |
| 69 | + (navigator as any).clipboard.writeText(this.generatedApiKey) |
| 70 | + .then(() => this.copied = true) |
| 71 | + .catch(err => { |
| 72 | + console.error('Error al copiar con clipboard API', err); |
| 73 | + this.fallbackCopy(this.generatedApiKey); |
| 74 | + }); |
| 75 | + } else { |
| 76 | + this.fallbackCopy(this.generatedApiKey); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private fallbackCopy(text: string): void { |
| 81 | + try { |
| 82 | + const textarea = document.createElement('textarea'); |
| 83 | + textarea.value = text; |
| 84 | + |
| 85 | + textarea.style.position = 'fixed'; |
| 86 | + textarea.style.top = '0'; |
| 87 | + textarea.style.left = '0'; |
| 88 | + textarea.style.opacity = '0'; |
| 89 | + |
| 90 | + document.body.appendChild(textarea); |
| 91 | + textarea.focus(); |
| 92 | + textarea.select(); |
| 93 | + |
| 94 | + const successful = document.execCommand('copy'); |
| 95 | + document.body.removeChild(textarea); |
| 96 | + |
| 97 | + if (successful) { |
| 98 | + this.showCopiedFeedback(); |
| 99 | + } else { |
| 100 | + console.warn('Fallback copy failed'); |
| 101 | + } |
| 102 | + } catch (err) { |
| 103 | + console.error('Error en fallback copy', err); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private showCopiedFeedback(): void { |
| 108 | + this.copied = true; |
| 109 | + setTimeout(() => this.copied = false, 2000); |
| 110 | + } |
| 111 | + |
| 112 | + openCreateModal(): void { |
| 113 | + const modalRef = this.modalService.open(ApiKeyModalComponent, { centered: true }); |
| 114 | + |
| 115 | + modalRef.result.then((key: ApiKeyResponse) => { |
| 116 | + if (key) { |
| 117 | + this.generateKey(key); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + editKey(key: ApiKeyResponse): void { |
| 123 | + const modalRef = this.modalService.open(ApiKeyModalComponent, {centered: true}); |
| 124 | + modalRef.componentInstance.apiKey = key; |
| 125 | + |
| 126 | + modalRef.result.then((key: ApiKeyResponse) => { |
| 127 | + if (key) { |
| 128 | + this.generateKey(key); |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + deleteKey(apiKey: ApiKeyResponse): void { |
| 134 | + const modalRef = this.modalService.open(ModalConfirmationComponent, {centered: true}); |
| 135 | + modalRef.componentInstance.header = `Delete API Key: ${apiKey.name}`; |
| 136 | + modalRef.componentInstance.message = 'Are you sure you want to delete this API key?'; |
| 137 | + modalRef.componentInstance.confirmBtnType = 'delete'; |
| 138 | + modalRef.componentInstance.type = 'danger'; |
| 139 | + modalRef.componentInstance.confirmBtnText = 'Delete'; |
| 140 | + modalRef.componentInstance.confirmBtnIcon = 'icon-cross-circle'; |
| 141 | + |
| 142 | + modalRef.result.then(reason => { |
| 143 | + if (reason === 'ok') { |
| 144 | + this.delete(apiKey); |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + delete(apiKey: ApiKeyResponse): void { |
| 150 | + this.apiKeyService.delete(apiKey.id).subscribe({ |
| 151 | + next: () => { |
| 152 | + this.toastService.showSuccess('API key deleted successfully.'); |
| 153 | + this.loadKeys(); |
| 154 | + }, |
| 155 | + error: (err) => { |
| 156 | + this.toastService.showError('Error', 'An error occurred while deleting the API key.'); |
| 157 | + throw err; |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + getDaysUntilExpire(expiresAt: string): number { |
| 163 | + if (!expiresAt) { |
| 164 | + return -1; |
| 165 | + } |
| 166 | + |
| 167 | + const today = moment().startOf('day'); |
| 168 | + const expireDate = moment(expiresAt).startOf('day'); |
| 169 | + return expireDate.diff(today, 'days'); |
| 170 | + } |
| 171 | + |
| 172 | + onSortBy($event: SortEvent) { |
| 173 | + this.request.sort = $event.column + ',' + $event.direction; |
| 174 | + this.loadKeys(); |
| 175 | + } |
| 176 | + |
| 177 | + maskSecrets(str: string): string { |
| 178 | + if (!str || str.length <= 10) { |
| 179 | + return str; |
| 180 | + } |
| 181 | + const prefix = str.substring(0, 10); |
| 182 | + const maskLength = str.length - 30; |
| 183 | + const maskedPart = '*'.repeat(maskLength); |
| 184 | + return prefix + maskedPart; |
| 185 | + } |
| 186 | + |
| 187 | + generateKey(apiKey: ApiKeyResponse): void { |
| 188 | + this.generating.push(apiKey.id); |
| 189 | + this.apiKeyService.generateApiKey(apiKey.id).subscribe(response => { |
| 190 | + this.generatedApiKey = response.body ? response.body : ""; |
| 191 | + this.generatedModalRef = this.modalService.open(this.generatedModal, {centered: true}); |
| 192 | + const index = this.generating.indexOf(apiKey.id); |
| 193 | + if (index > -1) { |
| 194 | + this.generating.splice(index, 1); |
| 195 | + } |
| 196 | + this.loadKeys(); |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + isApiKeyExpired(expiresAt?: string | null ): boolean { |
| 201 | + if (!expiresAt) { |
| 202 | + return false; |
| 203 | + } |
| 204 | + const expirationTime = new Date(expiresAt).getTime(); |
| 205 | + return expirationTime < Date.now(); |
| 206 | + } |
| 207 | + |
| 208 | + close() { |
| 209 | + this.generatedModalRef.close(); |
| 210 | + this.copied = false; |
| 211 | + this.generatedApiKey = ''; |
| 212 | + } |
| 213 | + |
| 214 | + loadPage($event: number) { |
| 215 | + this.page = $event - 1; |
| 216 | + this.request = { |
| 217 | + ...this.request, |
| 218 | + page: this.page |
| 219 | + }; |
| 220 | + this.loadKeys(); |
| 221 | + } |
| 222 | + |
| 223 | + onItemsPerPageChange($event: number) { |
| 224 | + this.request = { |
| 225 | + ...this.request, |
| 226 | + size: $event, |
| 227 | + page: 0 |
| 228 | + }; |
| 229 | + this.page = 0; |
| 230 | + this.loadKeys(); |
| 231 | + } |
| 232 | +} |
0 commit comments