Skip to content

Commit 88b821d

Browse files
committed
Implement logout functionality and enhance popup alert system, remove logout screen
1 parent 79b0b74 commit 88b821d

File tree

9 files changed

+75
-121
lines changed

9 files changed

+75
-121
lines changed

App.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import type { DeviceParamList } from '@screens/Settings/Devices/Device'
2929
import Device from '@screens/Settings/Devices/Device'
3030
import Devices from '@screens/Settings/Devices/Devices'
3131
import About from '@screens/Settings/Extra/About'
32-
import Logout from '@screens/Settings/Logout/Logout'
3332
import ManageStorage from '@screens/Settings/ManageStorage'
3433
import Settings from '@screens/Settings/Settings/Settings'
3534
import UiAndComponents from '@screens/Settings/UiAndComponents'
@@ -151,7 +150,6 @@ export type RootStackParamList = {
151150
Devices: undefined
152151
Device: DeviceParamList
153152
AllUsers: undefined
154-
Logout: undefined
155153
EditVersion: undefined
156154
Story: undefined
157155
User: UserParamList
@@ -185,7 +183,6 @@ function Navigation() {
185183
<Stack.Screen name='Home' component={Home} options={SMOOTH_ANIMATION} />
186184
<Stack.Screen name='Test' component={Test} options={GestureEnabled} />
187185
<Stack.Screen name='AllUsers' component={AllUsers} options={GestureEnabled} />
188-
<Stack.Screen name='Logout' component={Logout} options={IOS_BOTTOM_STYLE} />
189186
{/* <Stack.Screen name='Location' component={Location} /> */}
190187
{/* <Stack.Screen name='CompassAnimation' component={CompassAnimation} /> */}
191188
{/* <Stack.Screen name='ParallaxWallpaper' component={ParallaxWallpaper} /> */}

src/assets/images/src/logout.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/components/Popup.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ type PopupT = {
1212
text: string
1313
onPress?: () => void
1414
}[]
15+
noClose?: boolean
1516
}
1617

17-
const Popup = React.memo<PopupT>(({ text, title, buttons, index }) => {
18+
const Popup = React.memo<PopupT>(({ text, title, buttons, index, noClose }) => {
1819
const removePopup = popupStore((store) => store.removePopup)
1920

2021
return (
@@ -25,7 +26,9 @@ const Popup = React.memo<PopupT>(({ text, title, buttons, index }) => {
2526
visible={true}
2627
hardwareAccelerated
2728
statusBarTranslucent
28-
onRequestClose={() => removePopup(index)}
29+
onRequestClose={() => {
30+
if (!noClose) removePopup(index)
31+
}}
2932
>
3033
<View className='flex-1 items-center justify-center bg-black/40 dark:bg-black/50'>
3134
<View className='w-[85%] rounded-xl bg-white dark:bg-zinc-900'>
@@ -61,7 +64,7 @@ export default Popup
6164
const PopupButton = React.memo<{ text: string; onPress?: () => void }>(({ text, onPress }) => {
6265
return (
6366
<TouchableOpacity
64-
className='min-w-20 items-center justify-center rounded-lg px-5 py-3 active:bg-black/5 dark:active:bg-white/10'
67+
className='min-w-20 items-center justify-center rounded-lg px-3 py-3 active:bg-black/5 dark:active:bg-white/10'
6568
onPress={onPress}
6669
activeOpacity={1}
6770
>
@@ -82,6 +85,7 @@ export const Popups = React.memo((props) => {
8285
index={index}
8386
text={popup.text}
8487
title={popup.title}
88+
noClose={popup.noClose}
8589
buttons={popup.buttons || [{ text: 'OK' }]}
8690
/>
8791
))}

src/screens/Settings/Devices/Device.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SettText from '@components/Settings/SettText'
77
import SettWrapper from '@components/Settings/SettWrapper'
88
import { queryClient } from '@query/index'
99
import type { RouteProp } from '@react-navigation/native'
10+
import { handleLogout, logout } from '@screens/auth/utils'
1011
import { useMutation } from '@tanstack/react-query'
1112
import { client } from '@utils/client'
1213
import { F, Medium } from '@utils/fonts'
@@ -39,6 +40,16 @@ export default function Device({ navigation, route }: { navigation: StackNav; ro
3940
mutationFn: async () =>
4041
await (await client.api.devices.delete.$post({ form: { device: device?.id || '' } })).json(),
4142
})
43+
44+
const { mutate: logoutMutation } = useMutation({
45+
mutationKey: ['logout'],
46+
mutationFn: async () => await (await client.api.logout.$post()).json(),
47+
onSuccess: (d) => {
48+
if (!d.status) ToastAndroid.show('Error logging out!', ToastAndroid.SHORT)
49+
logout()
50+
},
51+
})
52+
4253
const relativeTime = useMemo(() => getRelativeTime(device?.time), [device?.time])
4354

4455
const DeviceIcon = useMemo(() => getDeviceIcon(device?.os, device?.name), [device?.os, device?.name])
@@ -62,7 +73,10 @@ export default function Device({ navigation, route }: { navigation: StackNav; ro
6273
}, [data])
6374

6475
function handleRemove() {
65-
if (route.params.device?.isSelf) return navigation.navigate('Logout')
76+
if (route.params.device?.isSelf) {
77+
handleLogout(logoutMutation)
78+
return
79+
}
6680
alert('Remove Device', 'Are you sure you want to remove this device?', [
6781
{ text: 'Cancel' },
6882
{ text: 'Remove', onPress: () => mutate() },

src/screens/Settings/Logout/Logout.tsx

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/screens/Settings/Settings/Settings.tsx

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ import { SettOption } from '@components/Settings/SettOption'
3333
import SettText from '@components/Settings/SettText'
3434
import { Txt, TxtAcc } from '@components/Text'
3535
import { useIsFocused } from '@react-navigation/native'
36+
import { handleLogout, logout } from '@screens/auth/utils'
37+
import { useMutation } from '@tanstack/react-query'
38+
import { client } from '@utils/client'
3639
import { Colors } from '@utils/colors'
3740
import { APP_VERSION, APP_VERSION_CODE, ask_a_question, join_telegram_channel } from '@utils/constants'
3841
import { Bold } from '@utils/fonts'
3942
import { Caches, clearStorage, getStartWithSize, getStorageSize } from '@utils/storage'
4043
import type { NavProp } from '@utils/types'
4144
import { screenDelay, toReadableSize } from '@utils/utils'
4245
import React, { useEffect } from 'react'
43-
import { View, useColorScheme } from 'react-native'
46+
import { ToastAndroid, View, useColorScheme } from 'react-native'
4447
import { ScrollView } from 'react-native-gesture-handler'
4548
import Animated, { FadeIn } from 'react-native-reanimated'
4649
import AdminSettings from './AdminSettings'
47-
// function getTransparentCardStyle(scheme: ColorSchemeName) {
48-
// return scheme === 'dark' ? 'aa' : '77'
49-
// }
5050

5151
function SettingsHeader({ title, Title }: { title?: string; Title?: React.ReactNode }) {
5252
const [search, setSearch] = React.useState('')
@@ -71,20 +71,29 @@ export default function Settings({ navigation }: NavProp) {
7171

7272
const focused = useIsFocused()
7373

74+
const { mutate: logoutMutation } = useMutation({
75+
mutationKey: ['logout'],
76+
mutationFn: async () => await (await client.api.logout.$post()).json(),
77+
onSuccess: (d) => {
78+
if (!d.status) ToastAndroid.show('Error logging out!', ToastAndroid.SHORT)
79+
logout()
80+
},
81+
})
82+
7483
useEffect(() => {
7584
focused &&
7685
screenDelay(() => {
7786
setTotalSize(getStartWithSize(''))
7887
setTotalCache(getStorageSize(Caches))
7988
})
80-
// eslint-disable-next-line react-hooks/exhaustive-deps
8189
}, [])
8290

8391
function clearCache() {
8492
clearStorage(Caches)
8593
setTotalCache(0)
8694
}
8795

96+
8897
return (
8998
<View className='flex-1 bg-white dark:bg-zinc-950'>
9099
<PaddingTop />
@@ -95,8 +104,6 @@ export default function Settings({ navigation }: NavProp) {
95104
keyboardShouldPersistTaps='always'
96105
>
97106
<SettingsHeader title='Settings' />
98-
{/* <SettingsHeader navigation={navigation} title='Settings' back={false}/> */}
99-
{/* <SettingBackHeader title='Computer Science' navigation={navigation} /> */}
100107
<Gap20>
101108
<Gap12>
102109
<SettText className='mt-3'>
@@ -105,34 +112,6 @@ export default function Settings({ navigation }: NavProp) {
105112
</SettText>
106113
<UpdateSettings navigation={navigation} />
107114
<SettGroup title='General'>
108-
{/* <SettOption
109-
title='App Update'
110-
Icon={<RoundedIcon Icon={Setting07SolidIcon} className='bg-slate-500' />}
111-
arrow
112-
Right={<RoundNotification n={4} />}
113-
onPress={() =>
114-
navigation.navigate('Update', {
115-
version: '2.7.3',
116-
size: '10MB',
117-
whatsNew: ['New features', 'Bug fixes', 'Performance improvements'],
118-
})
119-
}
120-
/> */}
121-
{/* <SettOption
122-
title='App Update'
123-
Icon={<RoundedIcon Icon={Setting07SolidIcon} className='bg-slate-500' />}
124-
arrow
125-
Right={<RoundNotification n={1} />}
126-
onPress={() =>
127-
navigation.navigate('ForceUpdate', {
128-
shouldGoBack: true,
129-
version: '2.7.3',
130-
size: '10MB',
131-
whatsNew: ['New features', 'Bug fixes', 'Performance improvements'],
132-
})
133-
}
134-
/> */}
135-
136115
<SettOption
137116
title='Your Profile'
138117
Icon={<RoundedIcon Icon={UserSolidIcon} className='bg-green-500' />}
@@ -172,7 +151,7 @@ export default function Settings({ navigation }: NavProp) {
172151
arrow
173152
title='Log Out'
174153
Icon={<RoundedIcon Icon={Logout02SolidIcon} className='bg-red-500' />}
175-
onPress={() => navigation.navigate('Logout')}
154+
onPress={() => handleLogout(logoutMutation)}
176155
/>
177156
</SettGroup>
178157
</Gap12>

src/screens/auth/Login.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { SettOption } from '@components/Settings/SettOption'
1515
import SettText from '@components/Settings/SettText'
1616
import { useMutation } from '@tanstack/react-query'
1717
import { client, updateClientHeader } from '@utils/client'
18+
import { W } from '@utils/dimensions'
1819
import { Bold, SemiBold } from '@utils/fonts'
1920
import type { NavProp } from '@utils/types'
2021
import { useMemo, useState } from 'react'
@@ -68,7 +69,7 @@ export default function Login({ navigation }: NavProp) {
6869
},
6970
})
7071

71-
function handelSubmit() {
72+
function handleSubmit() {
7273
const { error, data } = loginZodValidator.safeParse({ username, password, deviceName, deviceOs })
7374
if (error) {
7475
alert('Error', error.errors[0]?.message || '')
@@ -81,9 +82,9 @@ export default function Login({ navigation }: NavProp) {
8182
<KeyboardAvoid className='bg flex-1' contentContainerStyle={{ paddingBottom: 20 }}>
8283
<View className='pb-5'>
8384
<PaddingTop />
84-
<View className='px-5 py-5 pb-10'>
85-
<Bold className='text-3xl text-black dark:text-white'>Welcome to Tech Triangle</Bold>
86-
<Lottie source={Animations['welcome']} />
85+
<View className='px-5 py-5 pb-8'>
86+
<Bold className='pb-3 text-3xl text-black dark:text-white'>Welcome to Tech Triangle</Bold>
87+
<Lottie source={Animations['welcome']} style={{ width: W * 0.6, height: W * 0.6 }} />
8788
</View>
8889
<Gap12>
8990
<SettGroup title='Email or Username'>
@@ -121,7 +122,7 @@ export default function Login({ navigation }: NavProp) {
121122
title={isPending ? 'Logging in...' : 'Login'}
122123
className='w-full'
123124
disabled={isPending}
124-
onPress={handelSubmit}
125+
onPress={handleSubmit}
125126
/>
126127
</View>
127128
<SettGroup title='More Options' className='mt-5'>

src/screens/auth/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import authStore from '@/zustand/authStore'
2+
import { navigationStore } from '@/zustand/navigationStore'
3+
import popupStore from '@/zustand/popupStore'
4+
import { queryClient } from '@query/index'
5+
6+
export function logout() {
7+
const navigation = navigationStore.getState().navigation
8+
authStore.getState().removeToken()
9+
popupStore.getState().clear()
10+
queryClient.clear()
11+
navigation?.reset({ index: 0, routes: [{ name: 'Login' }] })
12+
}
13+
14+
export function handleLogout(logoutMutation: () => void) {
15+
const alert = popupStore.getState().alert
16+
alert('Log out', 'This is the device you are currently using. Are you sure you want to log out?', [
17+
{
18+
text: 'Log out',
19+
onPress() {
20+
logoutMutation()
21+
alert('Please wait', 'Please wait while we log you out', [], true)
22+
},
23+
},
24+
{ text: 'Cancel' },
25+
])
26+
}

src/zustand/popupStore.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ export type Popup = {
44
text?: string
55
title?: string
66
buttons?: { text: string; onPress?: () => void }[]
7+
noClose?: boolean
78
}
89

910
type PopupStore = {
1011
popups: Popup[]
11-
alert: (title: string, text: string, buttons?: { text: string; onPress?: () => void }[]) => void
12+
alert: (title: string, text: string, buttons?: { text: string; onPress?: () => void }[], noClose?: boolean) => void
1213
removePopup: (index: number) => void
14+
clear: () => void
1315
}
1416

1517
const popupStore = create<PopupStore>((set) => ({
1618
popups: [],
17-
alert: (title, text, buttons = [{ text: 'OK' }]) =>
18-
set((state) => ({ popups: [...state.popups, { title, text, buttons }] })),
19+
alert: (title, text, buttons = [{ text: 'OK' }], noClose = false) =>
20+
set((state) => ({ popups: [...state.popups, { title, text, buttons, noClose }] })),
1921
removePopup: (index) => set((state) => ({ popups: state.popups.filter((_, i) => i !== index) })),
22+
clear: () => set({ popups: [] }),
2023
}))
2124

2225
export default popupStore

0 commit comments

Comments
 (0)