|
| 1 | +<script setup> |
| 2 | +import { createItem, updateItem } from '@directus/sdk'; |
| 3 | +
|
| 4 | +const { $directusTv } = useNuxtApp(); |
| 5 | +const ariaId = useId(); |
| 6 | +
|
| 7 | +const props = defineProps({ |
| 8 | + episodeId: { |
| 9 | + type: String, |
| 10 | + required: true, |
| 11 | + }, |
| 12 | +}); |
| 13 | +
|
| 14 | +const visitorId = useCookie('visitor_id'); |
| 15 | +
|
| 16 | +const loading = ref(false); |
| 17 | +const error = ref(null); |
| 18 | +const success = ref(false); |
| 19 | +
|
| 20 | +const reaction = reactive({ |
| 21 | + comments: '', |
| 22 | +}); |
| 23 | +
|
| 24 | +const textarea = ref(null); |
| 25 | +
|
| 26 | +const isOpen = ref(false); |
| 27 | +
|
| 28 | +const ratingMessages = { |
| 29 | + dislike: 'Woof! 🤦♂️ How can we do better?', |
| 30 | + like: 'Nice! 👍 Anything we can improve upon?', |
| 31 | + love: `Awesome! The whole team is rejoicing in celebration! 🥳🎉🎊 Anything you'd like to say to them?`, |
| 32 | +}; |
| 33 | +
|
| 34 | +async function submitReaction(type) { |
| 35 | + isOpen.value = true; |
| 36 | + loading.value = true; |
| 37 | +
|
| 38 | + if (type) { |
| 39 | + reaction.type = type; |
| 40 | + } |
| 41 | +
|
| 42 | + try { |
| 43 | + if (reaction.id) { |
| 44 | + const response = await $directusTv.request( |
| 45 | + updateItem('tv_episode_reactions', reaction.id, { |
| 46 | + episode: props.episodeId, |
| 47 | + type: reaction.type, |
| 48 | + comments: reaction.comments, |
| 49 | + visitor_id: visitorId.value, |
| 50 | + }), |
| 51 | + ); |
| 52 | +
|
| 53 | + if (response.comments) { |
| 54 | + success.value = true; |
| 55 | +
|
| 56 | + await setTimeout(() => { |
| 57 | + isOpen.value = false; |
| 58 | + }, 2000); |
| 59 | + } |
| 60 | + } else { |
| 61 | + const data = await $directusTv.request( |
| 62 | + createItem('tv_episode_reactions', { |
| 63 | + episode: props.episodeId, |
| 64 | + type: reaction.type, |
| 65 | + comments: reaction.comments, |
| 66 | + visitor_id: visitorId.value, |
| 67 | + }), |
| 68 | + ); |
| 69 | +
|
| 70 | + reaction.id = data.id; |
| 71 | + } |
| 72 | + } catch (e) { |
| 73 | + error.value = e; |
| 74 | + } finally { |
| 75 | + loading.value = false; |
| 76 | + } |
| 77 | +} |
| 78 | +
|
| 79 | +watch(isOpen, (value) => { |
| 80 | + if (value) { |
| 81 | + setTimeout(() => { |
| 82 | + textarea.value.focus(); |
| 83 | + }, 100); |
| 84 | + } |
| 85 | +}); |
| 86 | +
|
| 87 | +onKeyStroke('Escape', () => { |
| 88 | + isOpen.value = false; |
| 89 | +}); |
| 90 | +</script> |
| 91 | +<template> |
| 92 | + <div> |
| 93 | + <VDropdown :aria-id class="reactions" :triggers="[]" :shown="isOpen" :auto-hide="false"> |
| 94 | + <button |
| 95 | + v-tooltip="`I do not like this`" |
| 96 | + class="feedback-button" |
| 97 | + :aria-pressed="reaction.type === 'dislike'" |
| 98 | + @click="() => submitReaction('dislike')" |
| 99 | + > |
| 100 | + <BaseIcon name="thumb_down" /> |
| 101 | + </button> |
| 102 | + <button |
| 103 | + v-tooltip="'I like this'" |
| 104 | + class="feedback-button" |
| 105 | + :aria-pressed="reaction.type === 'like'" |
| 106 | + @click="() => submitReaction('like')" |
| 107 | + > |
| 108 | + <BaseIcon name="thumb_up" /> |
| 109 | + </button> |
| 110 | + <button |
| 111 | + v-tooltip="'I freaking love this'" |
| 112 | + :aria-pressed="reaction.type === 'love'" |
| 113 | + class="feedback-button" |
| 114 | + @click="() => submitReaction('love')" |
| 115 | + > |
| 116 | + <BaseIcon name="favorite" /> |
| 117 | + </button> |
| 118 | + <template #popper> |
| 119 | + <ThemeProvider variant="dark"> |
| 120 | + <div class="popover"> |
| 121 | + <template v-if="!success"> |
| 122 | + <p>{{ ratingMessages[reaction.type] }}</p> |
| 123 | + <textarea ref="textarea" v-model="reaction.comments" class="input" placeholder="Give us your feedback" /> |
| 124 | + <BaseButton |
| 125 | + type="button" |
| 126 | + :loading="loading" |
| 127 | + :disabled="loading" |
| 128 | + color="primary" |
| 129 | + label="Send Your Feedback" |
| 130 | + @click="submitReaction(reaction.type)" |
| 131 | + /> |
| 132 | + </template> |
| 133 | + <template v-else-if="error"> |
| 134 | + <p>Whoops! There was an error submitting your feedback.</p> |
| 135 | + </template> |
| 136 | + <template v-else-if="success"> |
| 137 | + <p>Thank you for your feedback!</p> |
| 138 | + </template> |
| 139 | + <button class="close-button" @click="() => (isOpen = false)"> |
| 140 | + <BaseIcon name="close" /> |
| 141 | + </button> |
| 142 | + </div> |
| 143 | + </ThemeProvider> |
| 144 | + </template> |
| 145 | + </VDropdown> |
| 146 | + </div> |
| 147 | +</template> |
| 148 | + |
| 149 | +<style lang="scss" scoped> |
| 150 | +.reactions { |
| 151 | + display: flex; |
| 152 | + align-content: center; |
| 153 | + border-radius: var(--rounded-full); |
| 154 | + background: var(--gray-100); |
| 155 | + padding: var(--space-1); |
| 156 | + gap: var(--space-1); |
| 157 | +} |
| 158 | +
|
| 159 | +.feedback-button { |
| 160 | + transition: background-color 0.2s; |
| 161 | + background: none; |
| 162 | + border: none; |
| 163 | + cursor: pointer; |
| 164 | + padding: var(--space-2); |
| 165 | + &:hover { |
| 166 | + background: var(--gray-200); |
| 167 | + } |
| 168 | + border-radius: var(--rounded-full); |
| 169 | + &[aria-pressed='true'] { |
| 170 | + background: var(--primary-500); |
| 171 | + color: var(--white); |
| 172 | + } |
| 173 | +} |
| 174 | +
|
| 175 | +.popover { |
| 176 | + position: relative; |
| 177 | + width: 350px; |
| 178 | + padding: var(--space-5); |
| 179 | + background-color: var(--gray-100); |
| 180 | + border-radius: var(--rounded-xl); |
| 181 | + border: 2px solid var(--gray-200); |
| 182 | + display: flex; |
| 183 | + flex-direction: column; |
| 184 | + gap: var(--space-2); |
| 185 | + box-shadow: var(--shadow-lg); |
| 186 | +
|
| 187 | + button { |
| 188 | + width: auto; |
| 189 | + } |
| 190 | +
|
| 191 | + .close-button { |
| 192 | + position: absolute; |
| 193 | + top: var(--space-2); |
| 194 | + right: var(--space-2); |
| 195 | + background: none; |
| 196 | + border: none; |
| 197 | + cursor: pointer; |
| 198 | + padding: var(--space-2); |
| 199 | + border-radius: var(--rounded-full); |
| 200 | + &:hover { |
| 201 | + background: var(--gray-200); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | +
|
| 206 | +.input { |
| 207 | + color: var(--gray-900); |
| 208 | + width: 100%; |
| 209 | + height: 100px; |
| 210 | + border-radius: 4px; |
| 211 | + padding: 0.375rem 0.75rem; |
| 212 | + background-color: var(--gray-50); |
| 213 | + border: 1px solid var(--gray-200); |
| 214 | + &:focus { |
| 215 | + border-color: var(--primary); |
| 216 | + outline: none; |
| 217 | + box-shadow: 0px 0px var(--space-1) 0px var(--primary-100); |
| 218 | + } |
| 219 | +} |
| 220 | +</style> |
| 221 | + |
| 222 | +<style lang="css"> |
| 223 | +.v-popper--theme-dropdown { |
| 224 | + .v-popper__inner { |
| 225 | + background-color: transparent !important; |
| 226 | + border: none !important; |
| 227 | + border-radius: 6px; |
| 228 | + } |
| 229 | +
|
| 230 | + .v-popper__arrow-container { |
| 231 | + .v-popper__arrow-outer { |
| 232 | + border-color: #334155; |
| 233 | + } |
| 234 | + .v-popper__arrow-inner { |
| 235 | + border-color: #334155; |
| 236 | + } |
| 237 | + } |
| 238 | +} |
| 239 | +</style> |
0 commit comments