Skip to content

Commit d9df3ee

Browse files
authored
examples with fake (#22)
* Update dev deps * Remove cli from vitest config * Add real example
1 parent 90b5df0 commit d9df3ee

5 files changed

Lines changed: 501 additions & 391 deletions

File tree

examples/src/App.tsx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,83 @@
11
import type { JSX } from 'react'
22

3+
import {
4+
SeamQueryProvider,
5+
useSeamMutation,
6+
useSeamQuery,
7+
} from '@seamapi/react-query'
8+
39
export function App(): JSX.Element {
10+
return (
11+
<SeamQueryProvider
12+
endpoint={import.meta.env.SEAM_ENDPOINT}
13+
publishableKey={import.meta.env.SEAM_PUBLISHABLE_KEY}
14+
userIdentifierKey={import.meta.env.SEAM_USER_IDENTIFIER_KEY}
15+
>
16+
<Locks />
17+
</SeamQueryProvider>
18+
)
19+
}
20+
21+
function Locks(): JSX.Element {
22+
const locks = useSeamQuery('/locks/list')
23+
24+
const lockDoor = useSeamMutation('/locks/lock_door', {
25+
waitForActionAttempt: true,
26+
onSuccess: async () => await locks.refetch(),
27+
})
28+
29+
const unlockDoor = useSeamMutation('/locks/unlock_door', {
30+
waitForActionAttempt: true,
31+
onSuccess: async () => await locks.refetch(),
32+
})
33+
34+
const error = locks.error ?? lockDoor.error ?? unlockDoor.error
35+
const isMutating = lockDoor.isPending || unlockDoor.isPending
36+
const getButtonLabel = (
37+
deviceId: string,
38+
isLocked: boolean | undefined,
39+
): string => {
40+
if (lockDoor.isPending && lockDoor.variables.device_id === deviceId) {
41+
return 'Locking…'
42+
}
43+
if (unlockDoor.isPending && unlockDoor.variables.device_id === deviceId) {
44+
return 'Unlocking…'
45+
}
46+
if (isLocked == null) return 'Unknown'
47+
return isLocked ? 'Unlock' : 'Lock'
48+
}
49+
450
return (
551
<main>
652
<h1>Seam Query</h1>
53+
<h2>Locks</h2>
54+
{locks.isPending ? <p>Loading…</p> : null}
55+
{error == null ? null : <p role='alert'>{error.message}</p>}
56+
{locks.data?.length === 0 ? <p>No locks found.</p> : null}
57+
<ul>
58+
{locks.data?.map((device) => {
59+
const isLocked = device.properties.locked
60+
const canToggle = isLocked
61+
? device.can_remotely_unlock
62+
: device.can_remotely_lock
63+
64+
return (
65+
<li key={device.device_id}>
66+
{device.display_name}{' '}
67+
<button
68+
type='button'
69+
disabled={isLocked == null || canToggle !== true || isMutating}
70+
onClick={() => {
71+
const mutation = isLocked ? unlockDoor : lockDoor
72+
mutation.mutate({ device_id: device.device_id })
73+
}}
74+
>
75+
{getButtonLabel(device.device_id, isLocked)}
76+
</button>
77+
</li>
78+
)
79+
})}
80+
</ul>
781
</main>
882
)
983
}

0 commit comments

Comments
 (0)