Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/hello-zero-nuxt.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259<script setup lang="ts">import { escapeLike } from '@rocicorp/zero'import { useQuery } from 'zero-vue'
import { randomMessage } from '~/db/data/test-data'
const z = useZero()
const { data: users } = useQuery(z, z.query.user)const { data: mediums } = useQuery(z, z.query.medium)const { data: allMessages } = useQuery(z, z.query.message)
const filterUser = ref('')const filterText = ref('')const action = ref<'add' | 'remove' | undefined>(undefined)
const { data: filteredMessages } = useQuery(z, () => { let filtered = z.query.message .related('medium') .related('sender') .orderBy('timestamp', 'desc')
if (filterUser.value) { filtered = filtered.where('senderID', filterUser.value) }
if (filterText.value) { filtered = filtered.where('body', 'LIKE', `%${escapeLike(filterText.value)}%`) }
return filtered})
const hasFilters = computed(() => filterUser.value || filterText.value)
const deleteRandomMessage = () => { if (allMessages.value.length === 0) { return false } const index = randInt(allMessages.value.length) z.mutate.message.delete({ id: allMessages.value[index]!.id })
return true}
const addRandomMessage = () => { z.mutate.message.insert(randomMessage(users.value, mediums.value)) return true}
const handleAction = () => { if (action.value === 'add') { return addRandomMessage() } else if (action.value === 'remove') { return deleteRandomMessage() }
return false}
useInterval( () => { if (!handleAction()) { action.value = undefined } }, action.value !== undefined ? 1000 / 60 : null,)
const INITIAL_HOLD_DELAY_MS = 300let holdTimer: NodeJS.Timeout | null = nullconst handleAddAction = () => { addRandomMessage() holdTimer = setTimeout(() => { action.value = 'add' }, INITIAL_HOLD_DELAY_MS)}
const handleRemoveAction = (e: MouseEvent | TouchEvent) => { if (z.userID === 'anon' && 'shiftKey' in e && !e.shiftKey) { alert('You must be logged in to delete. Hold shift to try anyway.') return } deleteRandomMessage()
holdTimer = setTimeout(() => { action.value = 'remove' }, INITIAL_HOLD_DELAY_MS)}
const stopAction = () => { if (holdTimer) { clearTimeout(holdTimer) holdTimer = null }
action.value = undefined}
const editMessage = (e: MouseEvent, id: string, senderID: string, prev: string) => { if (senderID !== z.userID && !e.shiftKey) { alert( 'You aren\'t logged in as the sender of this message. Editing won\'t be permitted. Hold the shift key to try anyway.', ) return } const body = prompt('Edit message', prev) z.mutate.message.update({ id, body: body ?? prev, })}
const jwt = useCookie('jwt')const toggleLogin = async () => { if (z.userID === 'anon') { await fetch('/api/login') } else { jwt.value = null } location.reload()}
const user = computed(() => users.value.find(user => user.id === z.userID)?.name ?? 'anon')</script>
<template> <div> <div class="controls"> <div> <button @mousedown="handleAddAction" @mouseup="stopAction" @mouseleave="stopAction" @touchstart="handleAddAction" @touchend="stopAction" > Add Messages </button> <button @mousedown="handleRemoveAction" @mouseup="stopAction" @mouseleave="stopAction" @touchstart="handleRemoveAction" @touchend="stopAction" > Remove Messages </button> <em>(hold down buttons to repeat)</em> </div> <div :style="{ justifyContent: 'end' }"> {{ user === 'anon' ? '' : `Logged in as ${user}` }} <button @mousedown="toggleLogin"> {{ user === 'anon' ? 'Login' : 'Logout' }} </button> </div> </div> <div class="controls"> <div> From: <select v-model="filterUser" :style="{ flex: 1 }" > <option key="" value="" > Sender </option> <option v-for="choice in users" :key="choice.id" :value="choice.id" > {{ choice.name }} </option> </select> </div> <div> Contains: <input v-model="filterText" type="text" placeholder="message" :style="{ flex: 1 }" > </div> </div> <div class="controls"> <em> <template v-if="!hasFilters"> Showing all {{ filteredMessages.length }} messages </template> <template v-else> Showing {{ filteredMessages.length }} of {{ allMessages.length }} messages. Try opening <a href="/" target="_blank" >another tab</a> to see them all! </template> </em> </div> <template v-if="filteredMessages.length === 0"> <h3><em>No posts found 😢</em></h3> </template> <template v-else> <table border="1" cellspacing="0" cellpadding="6" width="100%" > <thead> <tr> <th>Sender</th> <th>Medium</th> <th>Message</th> <th>Sent</th> <th>Edit</th> </tr> </thead> <tbody> <tr v-for="message in filteredMessages" :key="message.id" > <td align="left"> {{ message.sender?.name }} </td> <td align="left"> {{ message.medium?.name }} </td> <td align="left"> {{ message.body }} </td> <td align="right"> {{ formatDate(message.timestamp) }} </td> <td @mousedown="(e: MouseEvent) => editMessage(e, message.id, message.senderID, message.body)"> ✏️ </td> </tr> </tbody> </table> </template> </div></template>
<style scoped>.controls { display: flex; justify-content: space-between; margin-bottom: 1em;}</style>