|
| 1 | +<script setup lang="ts"> |
| 2 | + import { |
| 3 | + useNotification |
| 4 | + } from '@kyvg/vue3-notification'; |
| 5 | + import { |
| 6 | + useStatusStore |
| 7 | + } from '@/ts/stores/status'; |
| 8 | + import { |
| 9 | + useSurveyStore |
| 10 | + } from '@/ts/stores/admin'; |
| 11 | + import { |
| 12 | + deleteSurvey, exportSurvey |
| 13 | + } from '@/ts/surveys'; |
| 14 | + import { useRoute } from 'vue-router'; |
| 15 | + import { watch } from 'vue'; |
| 16 | +
|
| 17 | + // TODO: Download magic links from server to display (if this is supported) |
| 18 | + // --> Likely won't be due to bcrypt or the like being used for passwords |
| 19 | +
|
| 20 | + const route = useRoute(); |
| 21 | + const surveyStore = useSurveyStore(); |
| 22 | + const maxSurveyLength = 50; |
| 23 | + const notifications = useNotification(); |
| 24 | + const status = useStatusStore(); |
| 25 | +
|
| 26 | + const copyLinkToClipboard = ( linkStr: string ) => { |
| 27 | + navigator.clipboard.writeText( linkStr ); |
| 28 | + notifications.notify( { |
| 29 | + 'text': 'Copied magic link to clipboard', |
| 30 | + 'type': 'success', |
| 31 | + 'title': 'Copied' |
| 32 | + } ); |
| 33 | + }; |
| 34 | +
|
| 35 | + const downloadMagicLinks = () => { |
| 36 | + const textContent = surveyStore.links.join( '\n' ); |
| 37 | + const blob = new Blob( |
| 38 | + [ textContent ], |
| 39 | + { |
| 40 | + 'type': 'text/plain' |
| 41 | + } |
| 42 | + ); |
| 43 | + const url = URL.createObjectURL( blob ); |
| 44 | + const a: HTMLAnchorElement = document.getElementById( 'linkDownloadAnchor' )! as HTMLAnchorElement; |
| 45 | +
|
| 46 | + a.href = url; |
| 47 | + a.download = 'magicLinks.txt'; |
| 48 | + a.click(); |
| 49 | + URL.revokeObjectURL( url ); |
| 50 | + }; |
| 51 | +
|
| 52 | + const truncate = ( text: string, limit: number ) => { |
| 53 | + if ( text.length < limit ) return text; |
| 54 | + else return text.slice( 0, limit - 3 ) + '...'; |
| 55 | + }; |
| 56 | +
|
| 57 | + const useTestData = () => { |
| 58 | + let links = []; |
| 59 | +
|
| 60 | + for ( let i = 0; i < 20; i++ ) |
| 61 | + links.push( 'link' + String( i ) ); |
| 62 | +
|
| 63 | + links.push( 'linkWhichIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong' ); |
| 64 | + surveyStore.setLinks( links ); |
| 65 | +
|
| 66 | + notifications.notify( { |
| 67 | + 'text': 'Populated link list using testing data for frontend dev.', |
| 68 | + 'type': 'warn', |
| 69 | + 'title': 'Loaded Testing Data' |
| 70 | + } ); |
| 71 | + }; |
| 72 | +
|
| 73 | + if ( status.devMode ) useTestData(); |
| 74 | +</script> |
| 75 | + |
| 76 | +<template> |
| 77 | + <div class="magic-links"> |
| 78 | + <div class="top-bar"> |
| 79 | + <h2 class="title"> |
| 80 | + Magic Links |
| 81 | + </h2> |
| 82 | + </div> |
| 83 | + <div class="content"> |
| 84 | + <p class="warn-text"> |
| 85 | + <strong>WARNING</strong> Magic Links are only shown once after creation. |
| 86 | + </p> |
| 87 | + |
| 88 | + <div |
| 89 | + v-if="surveyStore.links.length > 0" |
| 90 | + class="link-table" |
| 91 | + > |
| 92 | + <table> |
| 93 | + <tbody> |
| 94 | + <tr |
| 95 | + v-for="link, index in surveyStore.links" |
| 96 | + :key="index" |
| 97 | + @click="copyLinkToClipboard( link )" |
| 98 | + > |
| 99 | + <td> |
| 100 | + <i class="fa-lg fa-regular fa-copy copy-icon"></i> |
| 101 | + {{ truncate(link, 50) }} |
| 102 | + </td> |
| 103 | + </tr> |
| 104 | + </tbody> |
| 105 | + </table> |
| 106 | + </div> |
| 107 | + <div |
| 108 | + v-else |
| 109 | + class="placeholder" |
| 110 | + > |
| 111 | + <p>Select a Survey to view properties</p> |
| 112 | + </div> |
| 113 | + <a id="linkDownloadAnchor"> |
| 114 | + <button |
| 115 | + class="button primary" |
| 116 | + :class="surveyStore.links.length > 0 ? 'undefined' : 'disabled'" |
| 117 | + @click="surveyStore.links.length > 0 ? downloadMagicLinks() : 'undefined'" |
| 118 | + > |
| 119 | + Download Links |
| 120 | + </button> |
| 121 | + </a> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | +</template> |
| 125 | + |
| 126 | +<style scoped lang="scss"> |
| 127 | +@use '@/scss/admin/general'; |
| 128 | +@use '@/scss/admin/top-bar'; |
| 129 | +
|
| 130 | +.magic-links { |
| 131 | + // Mainly for wide displays |
| 132 | + .top-bar { |
| 133 | + justify-content: left; |
| 134 | + } |
| 135 | +
|
| 136 | + .warn-text { |
| 137 | + font-size: 1rem; |
| 138 | + color: var(--theme-bg-3-20); |
| 139 | + margin: 1rem; |
| 140 | + } |
| 141 | +
|
| 142 | + strong { |
| 143 | + color: var(--theme-warning); |
| 144 | + background-color: var(--theme-bg-1-shade); |
| 145 | + padding: 0.7rem; |
| 146 | + border-radius: 10px; |
| 147 | + } |
| 148 | +
|
| 149 | + >div.content { |
| 150 | + overflow-y: auto; |
| 151 | + scrollbar-color: var( --theme-interactable-text ) var( --theme-bg-3 ); |
| 152 | + max-height: 70vh; |
| 153 | +
|
| 154 | + >div.link-table { |
| 155 | + margin-left: 1rem; |
| 156 | + width: max(200px, 35vw); |
| 157 | + height: max(200px, 50vh); |
| 158 | +
|
| 159 | + overflow-y: auto; |
| 160 | + scrollbar-color: var( --theme-interactable-text ) var( --theme-bg-3 ); |
| 161 | + } |
| 162 | +
|
| 163 | + >a>button { |
| 164 | + margin-left: 1rem; |
| 165 | + margin-top: 2rem; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +</style> |
0 commit comments