Skip to content

Commit 6463dcd

Browse files
author
Guillermo Machado
committed
feat: rebase + improvements
1 parent 2a85224 commit 6463dcd

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

src/api/auth/use-user.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createQuery } from 'react-query-kit';
2+
3+
import { client } from '../common';
4+
5+
export type User = {
6+
id: number;
7+
name: string;
8+
email: string;
9+
picture: string | null;
10+
birthday: Date | null;
11+
};
12+
13+
const getUser = async () => {
14+
const { data } = await client({
15+
url: '/v1/users',
16+
method: 'GET',
17+
});
18+
return data;
19+
};
20+
21+
export const useUser = createQuery<User>({
22+
queryKey: ['getUser'],
23+
fetcher: getUser,
24+
});

src/api/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ export type PaginateQuery<T> = {
44
next: string | null;
55
previous: string | null;
66
};
7+
8+
export type ApiResponse<T> =
9+
| {
10+
errors: string[];
11+
}
12+
| T;

src/app/(app)/settings.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/* eslint-disable max-lines-per-function */
12
import { Env } from '@env';
23
import { Link } from 'expo-router';
34
import { useColorScheme } from 'nativewind';
45
import React from 'react';
56

7+
import { useUser } from '@/api/auth/use-user';
68
import { useAuth } from '@/components/providers/auth';
79
import { Item } from '@/components/settings/item';
810
import { ItemsContainer } from '@/components/settings/items-container';
@@ -13,6 +15,7 @@ import { colors, FocusAwareStatusBar, ScrollView, Text, View } from '@/ui';
1315
import { Website } from '@/ui/icons';
1416

1517
export default function Settings() {
18+
const { data: userData } = useUser();
1619
const { logout } = useAuth();
1720
const { colorScheme } = useColorScheme();
1821
const iconColor =
@@ -23,10 +26,17 @@ export default function Settings() {
2326
<FocusAwareStatusBar />
2427

2528
<ScrollView>
26-
<View className="flex-1 px-4 pt-16 ">
29+
<View className="flex-1 gap-2 p-4">
2730
<Text className="text-xl font-bold">
2831
{translate('settings.title')}
2932
</Text>
33+
<ItemsContainer title="settings.account.title">
34+
<Item text={'settings.account.name'} value={userData?.name ?? ''} />
35+
<Item
36+
text={'settings.account.email'}
37+
value={userData?.email ?? ''}
38+
/>
39+
</ItemsContainer>
3040
<ItemsContainer title="settings.generale">
3141
<LanguageItem />
3242
<ThemeItem />

src/components/providers/auth.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import React, {
99
import { MMKV } from 'react-native-mmkv';
1010

1111
import { client } from '@/api';
12-
import { storage } from '@/core/storage';
12+
13+
const unauthorizedHttpStatusCode = 401;
1314

1415
const storageKey = 'auth-storage';
1516

@@ -125,17 +126,20 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
125126
}, []);
126127

127128
const logout = () => {
128-
storage.delete(HEADER_KEYS.ACCESS_TOKEN);
129-
storage.delete(HEADER_KEYS.REFRESH_TOKEN);
130-
storage.delete(HEADER_KEYS.USER_ID);
131-
storage.delete(HEADER_KEYS.EXPIRY);
129+
authStorage.delete(HEADER_KEYS.ACCESS_TOKEN);
130+
authStorage.delete(HEADER_KEYS.REFRESH_TOKEN);
131+
authStorage.delete(HEADER_KEYS.USER_ID);
132+
authStorage.delete(HEADER_KEYS.EXPIRY);
132133
setToken(null);
133134
};
134135

135136
useEffect(() => {
136137
checkToken();
137138
const requestInterceptor = client.interceptors.response.use(
138139
(config) => {
140+
if (config.status === unauthorizedHttpStatusCode) {
141+
logout();
142+
}
139143
checkToken();
140144
return config;
141145
},

src/components/settings/items-container.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ type Props = {
99
};
1010

1111
export const ItemsContainer = ({ children, title }: Props) => (
12-
<>
13-
{title && <Text className="pb-2 pt-4 text-lg" tx={title} />}
14-
{
15-
<View className=" rounded-md border border-neutral-200 dark:border-neutral-700 dark:bg-neutral-800">
16-
{children}
17-
</View>
18-
}
19-
</>
20-
);
12+
<View className="gap-2">
13+
{title && <Text className="text-lg" tx={title} />}
14+
{
15+
<View className=" rounded-md border border-neutral-200 dark:border-neutral-700 dark:bg-neutral-800">
16+
{children}
17+
</View>
18+
}
19+
</View>
20+
);

src/translations/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
},
6666
"settings": {
6767
"about": "About",
68+
"account": {
69+
"email": "Email",
70+
"name": "Name",
71+
"title": "Account"
72+
},
6873
"app_name": "App Name",
6974
"arabic": "Arabic",
7075
"english": "English",

0 commit comments

Comments
 (0)