-
Notifications
You must be signed in to change notification settings - Fork 2
fix: device lock status disappearing when unlocked #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4ab7e32
e616d3f
f865f2f
b796131
333a4a5
4868165
489fa9d
dee4b20
aaef70e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import type { LockDevice } from 'lib/seam/locks/is-lock-device.js' | ||
| import { useLock } from 'lib/seam/locks/use-lock.js' | ||
| import { useToggleLock } from 'lib/seam/locks/use-toggle-lock.js' | ||
| import { useUnlock } from 'lib/seam/locks/use-unlock.js' | ||
| import { Button } from 'lib/ui/Button.js' | ||
| import type { SnackbarVariant } from 'lib/ui/Snackbar/Snackbar.js' | ||
|
|
||
| interface LockDeviceLockButtonsProps { | ||
| device: LockDevice | ||
| disableLockUnlock: boolean | ||
| setSnackbarVisible: (visible: boolean) => void | ||
| setSnackbarVariant: (variant: SnackbarVariant) => void | ||
| } | ||
|
|
||
| export function LockDeviceLockButtons({ | ||
| device, | ||
| setSnackbarVariant, | ||
| setSnackbarVisible, | ||
| disableLockUnlock, | ||
| }: LockDeviceLockButtonsProps): JSX.Element | null { | ||
| const lockStatus = device.properties.locked ? t.locked : t.unlocked | ||
| const toggleLockLabel = device.properties.locked ? t.unlock : t.lock | ||
|
|
||
| const toggleLock = useToggleLock({ | ||
| onSuccess: () => { | ||
| setSnackbarVisible(true) | ||
| setSnackbarVariant('success') | ||
| }, | ||
| onError: () => { | ||
| setSnackbarVisible(true) | ||
| setSnackbarVariant('error') | ||
| }, | ||
| }) | ||
|
|
||
| const lock = useLock({ | ||
| onSuccess: () => { | ||
| setSnackbarVisible(true) | ||
| setSnackbarVariant('success') | ||
| }, | ||
| onError: () => { | ||
| setSnackbarVisible(true) | ||
| setSnackbarVariant('error') | ||
| }, | ||
| }) | ||
|
|
||
| const unlock = useUnlock({ | ||
| onSuccess: () => { | ||
| setSnackbarVisible(true) | ||
| setSnackbarVariant('success') | ||
| }, | ||
| onError: () => { | ||
| setSnackbarVisible(true) | ||
| setSnackbarVariant('error') | ||
| }, | ||
| }) | ||
|
|
||
| if (!device.properties.online) { | ||
| return null | ||
| } | ||
|
|
||
| if (disableLockUnlock) { | ||
| return null | ||
| } | ||
|
|
||
| if ( | ||
| device.can_remotely_lock === true && | ||
| device.can_remotely_unlock === true | ||
| ) { | ||
| return ( | ||
| <div className='seam-content seam-lock-status'> | ||
| <div> | ||
| <span className='seam-label'>{t.lockStatus}</span> | ||
| <span className='seam-value'>{lockStatus}</span> | ||
| </div> | ||
| <div className='seam-right'> | ||
| <Button | ||
| size='small' | ||
| onClick={() => { | ||
| toggleLock.mutate(device) | ||
| }} | ||
| > | ||
| {toggleLockLabel} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| if (device.can_remotely_lock === true) { | ||
| return ( | ||
| <div className='seam-content seam-lock-status'> | ||
| <div> | ||
| <span className='seam-label'>{t.lockStatus}</span> | ||
| <span className='seam-value'>{lockStatus}</span> | ||
| </div> | ||
| <div className='seam-right'> | ||
| <Button | ||
| size='small' | ||
| onClick={() => { | ||
| lock.mutate(device) | ||
| }} | ||
| > | ||
| {t.lock} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| if (device.can_remotely_unlock === true) { | ||
| return ( | ||
| <div className='seam-content seam-lock-status'> | ||
| <div> | ||
| <span className='seam-label'>{t.lockStatus}</span> | ||
| <span className='seam-value'>{lockStatus}</span> | ||
| </div> | ||
| <div className='seam-right'> | ||
| <Button | ||
| size='small' | ||
| onClick={() => { | ||
| unlock.mutate(device) | ||
| }} | ||
| > | ||
| {t.unlock} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <div className='seam-content seam-lock-status'> | ||
| <div> | ||
| <span className='seam-label'>{t.lockStatus}</span> | ||
| <span className='seam-value'>{t.statusUnknown}</span> | ||
| </div> | ||
| <div className='seam-right'> | ||
| <Button | ||
| size='small' | ||
| onClick={() => { | ||
| lock.mutate(device) | ||
| }} | ||
| > | ||
| {t.lock} | ||
| </Button> | ||
| <Button | ||
| size='small' | ||
| onClick={() => { | ||
| unlock.mutate(device) | ||
| }} | ||
| > | ||
| {t.unlock} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
||
|
|
||
| const t = { | ||
| unlock: 'Unlock', | ||
| lock: 'Lock', | ||
| locked: 'Locked', | ||
| unlocked: 'Unlocked', | ||
| lockStatus: 'Lock status', | ||
| statusUnknown: 'Unknown', | ||
| } | ||
mikewuu marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import type { | ||
| SeamActionAttemptFailedError, | ||
| SeamActionAttemptTimeoutError, | ||
| SeamHttpApiError, | ||
| } from '@seamapi/http/connect' | ||
| import { NullSeamClientError, useSeamClient } from '@seamapi/react-query' | ||
| import type { ActionAttempt, Device } from '@seamapi/types/connect' | ||
| import { | ||
| useMutation, | ||
| type UseMutationResult, | ||
| useQueryClient, | ||
| } from '@tanstack/react-query' | ||
|
|
||
| export type UseLockData = undefined | ||
|
|
||
| export type UseLockMutationVariables = Pick<Device, 'device_id'> & { | ||
| properties: Required<Pick<Device['properties'], 'locked'>> | ||
| } | ||
|
|
||
| type LockActionAttempt = Extract<ActionAttempt, { action_type: 'LOCK_DOOR' }> | ||
|
|
||
| type MutationError = | ||
| | SeamHttpApiError | ||
| | SeamActionAttemptFailedError<LockActionAttempt> | ||
| | SeamActionAttemptTimeoutError<LockActionAttempt> | ||
|
|
||
| interface UseLockParams { | ||
| onError?: () => void | ||
| onSuccess?: () => void | ||
| } | ||
|
|
||
| export function useLock( | ||
| params: UseLockParams = {} | ||
| ): UseMutationResult<UseLockData, MutationError, UseLockMutationVariables> { | ||
| const { client } = useSeamClient() | ||
| const queryClient = useQueryClient() | ||
|
|
||
| return useMutation<UseLockData, MutationError, UseLockMutationVariables>({ | ||
| mutationFn: async (variables) => { | ||
| const { | ||
| device_id: deviceId, | ||
| properties: { locked }, | ||
| } = variables | ||
| if (client === null) throw new NullSeamClientError() | ||
| if (locked == null) return | ||
| await client.locks.lockDoor({ device_id: deviceId }) | ||
| }, | ||
| onMutate: (variables) => { | ||
| queryClient.setQueryData<Device[]>(['devices', 'list', {}], (devices) => { | ||
| if (devices == null) { | ||
| return devices | ||
| } | ||
|
|
||
| return devices.map((device) => { | ||
| if ( | ||
| device.device_id !== variables.device_id || | ||
| device.properties.locked == null | ||
| ) { | ||
| return device | ||
| } | ||
|
|
||
| return { | ||
| ...device, | ||
| properties: { | ||
| ...device.properties, | ||
| locked: !variables.properties.locked, | ||
| }, | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| queryClient.setQueryData<Device>( | ||
| ['devices', 'get', { device_id: variables.device_id }], | ||
| (device) => { | ||
| if (device?.properties.locked == null) return device | ||
|
|
||
| return { | ||
| ...device, | ||
| properties: { | ||
| ...device.properties, | ||
| locked: !variables.properties.locked, | ||
| }, | ||
| } | ||
| } | ||
| ) | ||
| }, | ||
| onError: async (_error, variables) => { | ||
| params.onError?.() | ||
|
|
||
| await queryClient.invalidateQueries({ | ||
| queryKey: ['devices', 'list'], | ||
| }) | ||
| await queryClient.invalidateQueries({ | ||
| queryKey: ['devices', 'get', { device_id: variables.device_id }], | ||
| }) | ||
| }, | ||
| onSuccess() { | ||
| params.onSuccess?.() | ||
| }, | ||
| }) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.