|
| 1 | +<script lang="ts"> |
| 2 | + import { getContextClient, queryStore } from '@urql/svelte'; |
| 3 | + import { |
| 4 | + GetAnnouncementsDocument, |
| 5 | + CreateAnnouncementDocument, |
| 6 | + type Announcement, |
| 7 | + AnnouncementImportance, |
| 8 | + UpdateAnnouncementDocument, |
| 9 | + DeleteAnnouncementDocument |
| 10 | + } from '$lib/generated'; |
| 11 | + import { Accordion, AccordionItem, getToastStore } from '@skeletonlabs/skeleton'; |
| 12 | + const client = getContextClient(); |
| 13 | +
|
| 14 | + let announcements: Announcement[] = []; |
| 15 | + const nameFields = {}; |
| 16 | + let announcementNegativeID = -1; |
| 17 | + const defaultNewAnnouncementMessage = 'New Announcement'; |
| 18 | +
|
| 19 | + const annQuery = queryStore({ |
| 20 | + query: GetAnnouncementsDocument, |
| 21 | + client |
| 22 | + }); |
| 23 | +
|
| 24 | + $: announcements = $annQuery.data?.getAnnouncements || []; |
| 25 | +
|
| 26 | + const toastStore = getToastStore(); |
| 27 | +
|
| 28 | + function newAnnouncement() { |
| 29 | + if (!announcements.find((announcement) => announcement.message == defaultNewAnnouncementMessage)) { |
| 30 | + const announcement = { |
| 31 | + id: announcementNegativeID--, |
| 32 | + message: defaultNewAnnouncementMessage, |
| 33 | + importance: AnnouncementImportance.Info |
| 34 | + } as Announcement; |
| 35 | + announcements.push(announcement); |
| 36 | + announcements = announcements; |
| 37 | + setTimeout(() => { |
| 38 | + const field = nameFields[announcement.id]; |
| 39 | + field.focus(); |
| 40 | + const input = field.getElement().querySelectorAll('input')[0] as HTMLInputElement; |
| 41 | + input.select(); |
| 42 | + }, 0); |
| 43 | + } else { |
| 44 | + nameFields[announcements[announcements.length - 1].id].focus(); |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + async function announcementChange(announcement: Announcement) { |
| 49 | + if (announcement.message == defaultNewAnnouncementMessage) { |
| 50 | + return; |
| 51 | + } |
| 52 | +
|
| 53 | + let success = false; |
| 54 | + if (announcement.id < 0) { |
| 55 | + // Create new tag & update tag.id with new DB id or re-fetch all tags |
| 56 | + try { |
| 57 | + const result = await client |
| 58 | + .mutation(CreateAnnouncementDocument, { |
| 59 | + announcement: { importance: announcement.importance, message: announcement.message } |
| 60 | + }) |
| 61 | + .toPromise(); |
| 62 | + if (result.data) { |
| 63 | + announcement.id = result.data.createAnnouncement.id; |
| 64 | + success = true; |
| 65 | + } |
| 66 | + } catch (err) { |
| 67 | + console.log(err); |
| 68 | + } |
| 69 | + if (!success) { |
| 70 | + toastStore.trigger({ |
| 71 | + message: `Failed to create Announcement '${announcement.message}'!`, |
| 72 | + background: 'variant-filled-error', |
| 73 | + timeout: 2000 |
| 74 | + }); |
| 75 | + return; |
| 76 | + } |
| 77 | + } else { |
| 78 | + // Update existing tag |
| 79 | + try { |
| 80 | + if (announcement.message.length > 0) { |
| 81 | + success = |
| 82 | + ( |
| 83 | + await client |
| 84 | + .mutation(UpdateAnnouncementDocument, { |
| 85 | + id: announcement.id, |
| 86 | + announcement: { importance: announcement.importance, message: announcement.message } |
| 87 | + }) |
| 88 | + .toPromise() |
| 89 | + ).data.updateAnnouncement != null; |
| 90 | + } |
| 91 | + } catch { |
| 92 | + // nothing |
| 93 | + } |
| 94 | + if (!success) { |
| 95 | + toastStore.trigger({ |
| 96 | + message: `Failed to update Announcement '${announcement.message}'!`, |
| 97 | + background: 'variant-filled-error', |
| 98 | + timeout: 2000 |
| 99 | + }); |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + toastStore.trigger({ |
| 105 | + message: `Announcement '${announcement.message}' saved!`, |
| 106 | + background: 'variant-filled-success', |
| 107 | + timeout: 2000 |
| 108 | + }); |
| 109 | + } |
| 110 | +
|
| 111 | + async function deleteAnnouncement(announcement: Announcement) { |
| 112 | + if (announcement.message != defaultNewAnnouncementMessage) { |
| 113 | + let success = false; |
| 114 | + try { |
| 115 | + const result = await client.mutation(DeleteAnnouncementDocument, { id: announcement.id }).toPromise(); |
| 116 | + success = result.data.deleteAnnouncement; |
| 117 | + } catch { |
| 118 | + success = false; |
| 119 | + } |
| 120 | + if (!success) { |
| 121 | + toastStore.trigger({ |
| 122 | + message: `Failed to remove Announcement '${announcement.message}'!`, |
| 123 | + background: 'variant-filled-error', |
| 124 | + timeout: 2000 |
| 125 | + }); |
| 126 | + return; |
| 127 | + } |
| 128 | + } else { |
| 129 | + announcements.splice(announcements.indexOf(announcement), 1); |
| 130 | + } |
| 131 | +
|
| 132 | + toastStore.trigger({ |
| 133 | + message: `Tag '${announcement.message}' removed!`, |
| 134 | + background: 'variant-filled-success', |
| 135 | + timeout: 2000 |
| 136 | + }); |
| 137 | + } |
| 138 | +
|
| 139 | + function onDeleteClick(e: Event, announcement: Announcement) { |
| 140 | + e.stopPropagation(); |
| 141 | + deleteAnnouncement(announcement); |
| 142 | + } |
| 143 | +</script> |
| 144 | + |
| 145 | +<h1>Announcements</h1> |
| 146 | + |
| 147 | +<div class="card"> |
| 148 | + {#if $annQuery.fetching} |
| 149 | + <h1>Loading announcements...</h1> |
| 150 | + {:else if $annQuery.error} |
| 151 | + <h1>Failed to load announcements: {$annQuery.error.message}</h1> |
| 152 | + {:else} |
| 153 | + <Accordion> |
| 154 | + {#each announcements as announcement} |
| 155 | + <AccordionItem> |
| 156 | + <svelte:fragment slot="summary" |
| 157 | + >({announcement.importance}) {(() => { |
| 158 | + const trimTo = 144; |
| 159 | + const trimmed = announcement.message.substring(0, trimTo); |
| 160 | + return announcement.message.length > trimTo ? trimmed + '...' : trimmed; |
| 161 | + })()}</svelte:fragment> |
| 162 | + <svelte:fragment slot="content"> |
| 163 | + <div> |
| 164 | + <div>Message</div> |
| 165 | + <input |
| 166 | + type="text" |
| 167 | + class="input p-2" |
| 168 | + bind:value={announcement.message} |
| 169 | + placeholder="This is the text of the announcement" |
| 170 | + bind:this={nameFields[announcement.id]} |
| 171 | + on:change={() => announcementChange(announcement)} /> |
| 172 | + <div>Importance (Fix, Info, Warning, Alert)</div> |
| 173 | + <input |
| 174 | + type="text" |
| 175 | + class="input p-2" |
| 176 | + bind:value={announcement.importance} |
| 177 | + placeholder="Importance level" |
| 178 | + on:change={() => announcementChange(announcement)} /> |
| 179 | + </div> |
| 180 | + |
| 181 | + <button class="variant-ghost-error btn" on:click={(e) => onDeleteClick(e, announcement)}> |
| 182 | + <span>Delete</span> |
| 183 | + </button> |
| 184 | + </svelte:fragment> |
| 185 | + </AccordionItem> |
| 186 | + {/each} |
| 187 | + </Accordion> |
| 188 | + |
| 189 | + <section class="p-4"> |
| 190 | + <button class="variant-ghost-primary btn" on:click={newAnnouncement}> |
| 191 | + <span>Add new announcement</span> |
| 192 | + <span class="material-icons">add</span> |
| 193 | + </button> |
| 194 | + </section> |
| 195 | + {/if} |
| 196 | +</div> |
| 197 | + |
| 198 | +<style lang="postcss"> |
| 199 | + h1 { |
| 200 | + @apply my-4 text-4xl font-bold; |
| 201 | + } |
| 202 | +</style> |
0 commit comments