-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ab7e32
fix: device lock status disappearing when unlocked
mikewuu e616d3f
create separate LockDeviceToggleLockButton
mikewuu f865f2f
conditionally render lock/unlock
mikewuu b796131
add unknown status
mikewuu 333a4a5
Update src/lib/seam/components/DeviceDetails/LockDeviceLockButtons.tsx
mikewuu 4868165
revert rename is-lock-device
mikewuu 489fa9d
rename to use-lock-door and use-unlock-door
mikewuu dee4b20
ci: Format code
seambot aaef70e
fix import name
mikewuu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/lib/seam/components/DeviceDetails/LockDeviceLockButtons.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import type { LockDevice } from 'lib/seam/locks/lock-device.js' | ||
| import { useLock } from 'lib/seam/locks/use-lock-door.js' | ||
| import { useToggleLock } from 'lib/seam/locks/use-toggle-lock.js' | ||
| import { useUnlock } from 'lib/seam/locks/use-unlock-door.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 (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 null | ||
| } | ||
|
|
||
| const t = { | ||
| unlock: 'Unlock', | ||
| lock: 'Lock', | ||
| locked: 'Locked', | ||
| unlocked: 'Unlocked', | ||
| lockStatus: 'Lock status', | ||
| statusUnknown: 'Unknown', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?.() | ||
| }, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useLockDoor/useUnlockDoor / use-(un)lock-door.ts
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed in https://github.com/seamapi/react/%20pull/709/commits/489fa9d63f4bf36bd6ace1613f702f5b5b3c2c53