Skip to content

Commit 2906360

Browse files
committed
Refactor query/index.ts to handle unauthorized responses
1 parent 754e555 commit 2906360

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/query/index.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { navigationStore } from '@/zustand/navigationStore'
12
import NetInfo from '@react-native-community/netinfo'
23
import { useFocusEffect } from '@react-navigation/native'
34
import { onlineManager, QueryClient, type NotifyOnChangeProps } from '@tanstack/react-query'
45
import React from 'react'
6+
import { Alert } from 'react-native'
57

68
// Online Status Manager
79
onlineManager.setEventListener((setOnline) => {
@@ -79,14 +81,41 @@ export function useQueryFocusAware() {
7981
return () => focusedRef.current
8082
}
8183

84+
type ServerResponse = {
85+
message: string
86+
status: boolean
87+
}
88+
const navigation = navigationStore.getState().navigation
8289
export const queryClient = new QueryClient({
8390
defaultOptions: {
84-
queries: {},
91+
queries: {
92+
select(data) {
93+
console.log(data)
94+
handleUnauthorized(data as ServerResponse)
95+
return data
96+
},
97+
},
8598
mutations: {
8699
onSuccess: (data) => {
87100
console.log(data)
88-
console.log('Mutation Success')
101+
handleUnauthorized(data as ServerResponse)
89102
},
90103
},
91104
},
92105
})
106+
107+
function handleUnauthorized(data: ServerResponse) {
108+
if (data.message !== 'Unauthorized') return
109+
navigation?.reset({
110+
index: 0,
111+
routes: [{ name: 'Login' }],
112+
})
113+
Alert.alert('Unauthorized', 'Please login again', [
114+
{
115+
text: 'OK',
116+
onPress: () => {
117+
navigation?.navigate('Login')
118+
},
119+
},
120+
])
121+
}

src/screens/Settings/Devices/Devices.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default function Devices({ navigation }: NavProp) {
6969
<>
7070
{data.data.devices.map((device, i) => (
7171
<Animated.View key={device?.id} entering={delayedFadeAnimation(i)}>
72-
<Device key={i} navigation={navigation} device={device} isSelf />
72+
<Device key={i} navigation={navigation} device={device} isSelf={false} />
7373
</Animated.View>
7474
))}
7575
</>

0 commit comments

Comments
 (0)