Skip to content

Commit df092b6

Browse files
committed
update playground to reflect changes in zero-vue
1 parent b3bdf99 commit df092b6

File tree

5 files changed

+156
-45
lines changed

5 files changed

+156
-45
lines changed

playground/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ ZERO_AUTH_SECRET="secretkey"
55
ZERO_REPLICA_FILE="/tmp/zstart_replica.db"
66

77
VITE_PUBLIC_SERVER='http://localhost:4848'
8+
VITE_PUBLIC_AUTH_SECRET='secretkey'

playground/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
"test:types": "vue-tsc --build"
1515
},
1616
"dependencies": {
17+
"@vueuse/integrations": "^13.9.0",
1718
"jose": "^6.0.0",
18-
"js-cookie": "^3.0.5",
19+
"universal-cookie": "^7.2.2",
1920
"vue": "^3.5.13",
2021
"zero-vue": "latest"
2122
},

playground/src/app.vue

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
11
<script setup lang="ts">
2-
import { escapeLike, Zero } from '@rocicorp/zero'
3-
import { decodeJwt } from 'jose'
4-
import Cookies from 'js-cookie'
5-
import { computed, ref } from 'vue'
6-
import { createZero } from 'zero-vue'
2+
import { escapeLike } from '@rocicorp/zero'
3+
import { useCookies } from '@vueuse/integrations/useCookies'
74
5+
import { SignJWT } from 'jose'
6+
import { computed, ref } from 'vue'
87
import { useInterval } from '~/composables/use-interval'
98
import { randomMessage } from '~/db/data/test-data'
10-
import { schema } from '~/db/schema'
119
import { formatDate } from '~/utils/date'
1210
import { randInt } from '~/utils/rand'
11+
import { useQuery, useZero } from './zero'
1312
14-
const encodedJWT = Cookies.get('jwt')
15-
const decodedJWT = encodedJWT && decodeJwt(encodedJWT)
16-
const userID = decodedJWT?.sub ? (decodedJWT.sub as string) : 'anon'
17-
18-
const z = new Zero({
19-
userID,
20-
auth: () => encodedJWT || undefined,
21-
server: import.meta.env.VITE_PUBLIC_SERVER,
22-
schema,
23-
// This is often easier to develop with if you're frequently changing
24-
// the schema. Switch to 'idb' for local-persistence.
25-
kvStore: 'mem',
26-
})
27-
28-
const { useQuery } = createZero({ zero: z })
13+
const cookies = useCookies()
2914
30-
const { data: users } = useQuery(z.query.user)
31-
const { data: mediums } = useQuery(z.query.medium)
32-
const { data: allMessages } = useQuery(z.query.message)
15+
const z = useZero()
16+
const { data: users } = useQuery(() => z.value.query.user)
17+
const { data: mediums } = useQuery(() => z.value.query.medium)
18+
const { data: allMessages } = useQuery(() => z.value.query.message)
3319
3420
const filterUser = ref('')
3521
const filterText = ref('')
3622
const action = ref<'add' | 'remove' | undefined>(undefined)
3723
3824
const { data: filteredMessages } = useQuery(() => {
39-
let filtered = z.query.message
25+
let filtered = z.value.query.message
4026
.related('medium', medium => medium.one())
4127
.related('sender', sender => sender.one())
4228
.orderBy('timestamp', 'desc')
@@ -59,13 +45,13 @@ function deleteRandomMessage() {
5945
return false
6046
}
6147
const index = randInt(allMessages.value.length)
62-
z.mutate.message.delete({ id: allMessages.value[index]!.id })
48+
z.value.mutate.message.delete({ id: allMessages.value[index]!.id })
6349
6450
return true
6551
}
6652
6753
function addRandomMessage() {
68-
z.mutate.message.insert(randomMessage(users.value, mediums.value))
54+
z.value.mutate.message.insert(randomMessage(users.value, mediums.value))
6955
return true
7056
}
7157
@@ -99,7 +85,7 @@ function handleAddAction() {
9985
}
10086
10187
function handleRemoveAction(e: MouseEvent | TouchEvent) {
102-
if (z.userID === 'anon' && 'shiftKey' in e && !e.shiftKey) {
88+
if (z.value.userID === 'anon' && 'shiftKey' in e && !e.shiftKey) {
10389
// eslint-disable-next-line no-alert
10490
alert('You must be logged in to delete. Hold shift to try anyway.')
10591
return
@@ -121,7 +107,7 @@ function stopAction() {
121107
}
122108
123109
function editMessage(e: MouseEvent, id: string, senderID: string, prev: string) {
124-
if (senderID !== z.userID && !e.shiftKey) {
110+
if (senderID !== z.value.userID && !e.shiftKey) {
125111
// eslint-disable-next-line no-alert
126112
alert(
127113
'You aren\'t logged in as the sender of this message. Editing won\'t be permitted. Hold the shift key to try anyway.',
@@ -131,23 +117,25 @@ function editMessage(e: MouseEvent, id: string, senderID: string, prev: string)
131117
132118
// eslint-disable-next-line no-alert
133119
const body = prompt('Edit message', prev)
134-
z.mutate.message.update({
120+
z.value.mutate.message.update({
135121
id,
136122
body: body ?? prev,
137123
})
138124
}
139125
140126
async function toggleLogin() {
141-
if (z.userID === 'anon') {
142-
await fetch('/api/login')
127+
if (z.value.userID === 'anon') {
128+
const jwt = await new SignJWT({ sub: 'ENzoNm7g4E' })
129+
.setProtectedHeader({ alg: 'HS256' })
130+
.sign(new TextEncoder().encode(import.meta.env.VITE_PUBLIC_AUTH_SECRET))
131+
cookies.set('jwt', jwt)
143132
}
144133
else {
145-
Cookies.remove('jwt')
134+
cookies.remove('jwt')
146135
}
147-
location.reload()
148136
}
149137
150-
const user = computed(() => users.value.find(user => user.id === z.userID)?.name ?? 'anon')
138+
const user = computed(() => users.value.find(user => user.id === z.value.userID)?.name ?? 'anon')
151139
</script>
152140

153141
<template>

playground/src/zero.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useCookies } from '@vueuse/integrations/useCookies'
2+
import { decodeJwt } from 'jose'
3+
import { createZero } from 'zero-vue'
4+
5+
import { schema } from '~/db/schema'
6+
7+
const cookies = useCookies()
8+
9+
export const { useZero, useQuery } = createZero(() => {
10+
const encodedJWT = cookies.get('jwt')
11+
const decodedJWT = encodedJWT && decodeJwt(encodedJWT)
12+
const userID = decodedJWT?.sub ? (decodedJWT.sub as string) : 'anon'
13+
14+
return {
15+
userID,
16+
auth: () => encodedJWT || undefined,
17+
server: import.meta.env.VITE_PUBLIC_SERVER,
18+
schema,
19+
// This is often easier to develop with if you're frequently changing
20+
// the schema. Switch to 'idb' for local-persistence.
21+
kvStore: 'mem',
22+
}
23+
})

pnpm-lock.yaml

Lines changed: 107 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)