|
| 1 | +<script> |
| 2 | + import { modalOpen } from '$src/stores/modalOpen'; |
| 3 | + import { |
| 4 | + faChevronLeft, |
| 5 | + faChevronRight as faChevronRightPagination |
| 6 | + } from '@fortawesome/free-solid-svg-icons'; |
| 7 | + import { FontAwesomeIcon } from '@fortawesome/svelte-fontawesome'; |
| 8 | + import { Modal } from 'flowbite-svelte'; |
| 9 | + import ServiceAlertItem from './ServiceAlertItem.svelte'; |
| 10 | + import { t } from 'svelte-i18n'; |
| 11 | +
|
| 12 | + let { serviceAlerts = $bindable([]) } = $props(); |
| 13 | +
|
| 14 | + let modalAlert = $state(null); |
| 15 | + let isAlertsHidden = $state(false); |
| 16 | + let currentPage = $state(1); |
| 17 | + const alertsPerPage = 3; |
| 18 | +
|
| 19 | + let totalPages = $derived(Math.ceil(serviceAlerts.length / alertsPerPage)); |
| 20 | +
|
| 21 | + let paginatedAlerts = $derived( |
| 22 | + serviceAlerts.slice((currentPage - 1) * alertsPerPage, currentPage * alertsPerPage) |
| 23 | + ); |
| 24 | +
|
| 25 | + function openModal(alert) { |
| 26 | + modalAlert = alert; |
| 27 | + modalOpen.set(true); |
| 28 | + console.debug('modal opened'); |
| 29 | + } |
| 30 | +
|
| 31 | + function closeModal() { |
| 32 | + modalOpen.set(false); |
| 33 | + console.debug('modal closed'); |
| 34 | + } |
| 35 | +
|
| 36 | + function toggleAlerts() { |
| 37 | + if (isAlertsHidden) { |
| 38 | + isAlertsHidden = false; |
| 39 | + currentPage = 1; |
| 40 | + } else { |
| 41 | + isAlertsHidden = true; |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | + function goToPage(page) { |
| 46 | + currentPage = Math.max(1, Math.min(page, totalPages)); |
| 47 | + isAlertsHidden = false; |
| 48 | + } |
| 49 | +
|
| 50 | + function handleKeydown(event) { |
| 51 | + if (event.key === 'Escape') { |
| 52 | + event.stopPropagation(); |
| 53 | + event.preventDefault(); |
| 54 | + closeModal(); |
| 55 | + } |
| 56 | + } |
| 57 | +</script> |
| 58 | + |
| 59 | +{#if serviceAlerts.length > 0} |
| 60 | + <div class="relative flex flex-col gap-y-1 rounded-lg bg-white p-4 dark:bg-gray-800"> |
| 61 | + <div class="mb-2 flex items-center justify-between"> |
| 62 | + <h3 class="font-medium text-gray-700 dark:text-white"> |
| 63 | + {$t('service_alerts.service_alerts')} ({serviceAlerts.length}) |
| 64 | + </h3> |
| 65 | + <button |
| 66 | + class="text-sm font-medium text-green-600 hover:text-green-700 dark:text-green-400 dark:hover:text-green-500" |
| 67 | + onclick={toggleAlerts} |
| 68 | + > |
| 69 | + {isAlertsHidden ? $t('service_alerts.show') : $t('service_alerts.hide')} |
| 70 | + </button> |
| 71 | + </div> |
| 72 | + |
| 73 | + {#if !isAlertsHidden} |
| 74 | + <div class="space-y-2"> |
| 75 | + {#each paginatedAlerts as alert} |
| 76 | + <ServiceAlertItem {alert} {openModal} /> |
| 77 | + {/each} |
| 78 | + </div> |
| 79 | + |
| 80 | + {#if totalPages > 1} |
| 81 | + <div class="mt-3 flex items-center justify-center gap-4"> |
| 82 | + <button |
| 83 | + class="p-1 text-gray-500 hover:text-gray-700 disabled:opacity-50 dark:text-gray-400 dark:hover:text-gray-200" |
| 84 | + onclick={() => goToPage(currentPage - 1)} |
| 85 | + disabled={currentPage === 1} |
| 86 | + > |
| 87 | + <FontAwesomeIcon icon={faChevronLeft} class="h-4 w-4" /> |
| 88 | + </button> |
| 89 | + <span class="text-sm text-gray-700 dark:text-gray-300"> |
| 90 | + {$t('service_alerts.page')} |
| 91 | + {currentPage} of {totalPages} |
| 92 | + </span> |
| 93 | + <button |
| 94 | + class="p-1 text-gray-500 hover:text-gray-700 disabled:opacity-50 dark:text-gray-400 dark:hover:text-gray-200" |
| 95 | + onclick={() => goToPage(currentPage + 1)} |
| 96 | + disabled={currentPage === totalPages} |
| 97 | + > |
| 98 | + <FontAwesomeIcon icon={faChevronRightPagination} class="h-4 w-4" /> |
| 99 | + </button> |
| 100 | + </div> |
| 101 | + {/if} |
| 102 | + {/if} |
| 103 | + </div> |
| 104 | +{/if} |
| 105 | + |
| 106 | +{#if $modalOpen && modalAlert} |
| 107 | + <div class="center" onkeydown={handleKeydown} role="button" tabindex="0"> |
| 108 | + <Modal |
| 109 | + outsideclose={true} |
| 110 | + title={modalAlert?.summary?.value} |
| 111 | + bind:open={$modalOpen} |
| 112 | + size="3xl" |
| 113 | + class="relative w-full max-w-3xl rounded-xl bg-white p-8 text-gray-900 shadow-2xl dark:bg-gray-800 dark:text-gray-100" |
| 114 | + > |
| 115 | + <p class="mt-3 text-base leading-relaxed text-gray-800 dark:text-gray-200"> |
| 116 | + {modalAlert?.description?.value} |
| 117 | + </p> |
| 118 | + </Modal> |
| 119 | + </div> |
| 120 | +{/if} |
0 commit comments