@umang-thakkar/base-template 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -304
- package/package.json +11 -2
- package/template/.eslintrc +16 -46
- package/template/.prettierrc.js +8 -0
- package/template/App.tsx +11 -15
- package/template/Main.tsx +29 -5
- package/template/__mocks__/react-native-mmkv.ts +4 -4
- package/template/__tests__/App.test.tsx +6 -1
- package/template/__tests__/helpers.test.ts +43 -0
- package/template/babel.config.js +2 -1
- package/template/constants/API_Routes.ts +19 -0
- package/template/constants/AppConstants.tsx +1 -1
- package/template/constants/AppString.ts +36 -0
- package/template/constants/StoragePreferences.ts +16 -31
- package/template/jest.config.js +5 -0
- package/template/jest.setup.js +5 -0
- package/template/package.json +2 -3
- package/template/src/api/AxiosActions.ts +65 -28
- package/template/src/api/AxiosClient.ts +13 -7
- package/template/src/api/queries.ts +59 -29
- package/template/src/api/service.ts +23 -9
- package/template/src/components/ErrorBoundary.tsx +27 -31
- package/template/src/components/SnackbarHost.tsx +86 -0
- package/template/src/helper/colors.ts +22 -22
- package/template/src/helper/common-functions.ts +56 -10
- package/template/src/helper/matrics.ts +4 -6
- package/template/src/helper/navigation-service.ts +21 -24
- package/template/src/helper/snackbar.ts +41 -0
- package/template/src/helper/typography.ts +2 -2
- package/template/src/helper/utility-functions.ts +10 -16
- package/template/src/hooks/ThemeContext.tsx +60 -30
- package/template/src/hooks/usePaginatedAPI.ts +96 -0
- package/template/src/navigation/AppStack.tsx +36 -27
- package/template/src/navigation/screens.ts +17 -11
- package/template/src/redux/slice.ts +17 -12
- package/template/src/redux/slices/authSlice.ts +11 -14
- package/template/src/redux/store.ts +13 -9
- package/template/src/redux/types.d.ts +40 -19
- package/template/src/screens/HomeScreen.tsx +103 -29
- package/template/src/screens/LoginScreen.tsx +101 -69
- package/template/src/screens/SplashScreen.tsx +36 -25
- package/template/src/theme/darkTheme.ts +13 -6
- package/template/src/theme/index.ts +4 -3
- package/template/src/theme/lightTheme.ts +13 -5
- package/template/ios/.xcode.env.local +0 -1
|
@@ -1,32 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
import React from "react";
|
|
1
|
+
import React from 'react';
|
|
3
2
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import { AppStackParams } from
|
|
9
|
-
import { screens } from
|
|
3
|
+
createNativeStackNavigator,
|
|
4
|
+
NativeStackNavigationOptions,
|
|
5
|
+
NativeStackNavigationProp,
|
|
6
|
+
} from '@react-navigation/native-stack';
|
|
7
|
+
import { AppStackParams } from './AppStackParams';
|
|
8
|
+
import { screens } from './screens';
|
|
10
9
|
|
|
11
|
-
const
|
|
12
|
-
const options: StackNavigationOptions = { headerShown: false };
|
|
10
|
+
const Stack = createNativeStackNavigator<AppStackParams>();
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
options={options}
|
|
25
|
-
/>
|
|
26
|
-
))}
|
|
27
|
-
{/* 🚀 Later you can add HomeDrawer, Tabs, or other screens separately */}
|
|
28
|
-
</App.Navigator>
|
|
29
|
-
);
|
|
12
|
+
/**
|
|
13
|
+
* Defaults for every screen. Native stack uses UINavigationController on iOS
|
|
14
|
+
* and Fragments on Android, so transitions and swipe-back run natively.
|
|
15
|
+
*/
|
|
16
|
+
export const defaultScreenOptions: NativeStackNavigationOptions = {
|
|
17
|
+
headerShown: false,
|
|
18
|
+
// Same slide on both platforms (Android's default is a fade-from-bottom).
|
|
19
|
+
animation: 'ios_from_right',
|
|
20
|
+
// Off-screen screens stop re-rendering until they're focused again.
|
|
21
|
+
freezeOnBlur: true,
|
|
30
22
|
};
|
|
31
23
|
|
|
32
|
-
|
|
24
|
+
/** Use with `useNavigation<StackNavigation>()` in nested components. */
|
|
25
|
+
export type StackNavigation = NativeStackNavigationProp<AppStackParams>;
|
|
26
|
+
|
|
27
|
+
const AppStack = () => (
|
|
28
|
+
<Stack.Navigator initialRouteName="SplashScreen" screenOptions={defaultScreenOptions}>
|
|
29
|
+
{screens.map(item => (
|
|
30
|
+
<Stack.Screen
|
|
31
|
+
key={item.name}
|
|
32
|
+
name={item.name}
|
|
33
|
+
component={item.component}
|
|
34
|
+
options={item.options}
|
|
35
|
+
/>
|
|
36
|
+
))}
|
|
37
|
+
{/* Tabs or a drawer can be registered here as screens (see docs/navigation.md). */}
|
|
38
|
+
</Stack.Navigator>
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
export default AppStack;
|
|
@@ -1,16 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { NativeStackNavigationOptions } from '@react-navigation/native-stack';
|
|
3
|
+
import HomeScreen from '../screens/HomeScreen';
|
|
4
|
+
import SplashScreen from '../screens/SplashScreen';
|
|
5
|
+
import LoginScreen from '../screens/LoginScreen';
|
|
6
|
+
import { AppStackParams } from './AppStackParams';
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
+
export type ScreenConfig = {
|
|
8
9
|
name: keyof AppStackParams;
|
|
9
10
|
component: React.ComponentType<any>;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
/** Merged over `defaultScreenOptions` in AppStack.tsx. */
|
|
12
|
+
options?: NativeStackNavigationOptions;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const screens: ScreenConfig[] = [
|
|
16
|
+
{ name: 'SplashScreen', component: SplashScreen },
|
|
17
|
+
// Login and Home replace the whole stack (navigation.reset), so a fade
|
|
18
|
+
// reads better than a slide.
|
|
19
|
+
{ name: 'LoginScreen', component: LoginScreen, options: { animation: 'fade' } },
|
|
20
|
+
{ name: 'HomeScreen', component: HomeScreen, options: { animation: 'fade' } },
|
|
15
21
|
// Add your other screens here
|
|
16
22
|
];
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/** Example app-wide slice. Replace or remove once the app has real global state. */
|
|
4
|
+
interface GlobalState {
|
|
5
|
+
value: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const initialState: GlobalState = {
|
|
9
|
+
value: 0,
|
|
5
10
|
};
|
|
6
11
|
|
|
7
12
|
const appSlice = createSlice({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
},
|
|
13
|
+
name: 'global',
|
|
14
|
+
initialState,
|
|
15
|
+
reducers: {
|
|
16
|
+
setValue: (state, action: PayloadAction<number>) => {
|
|
17
|
+
state.value = action.payload;
|
|
18
|
+
},
|
|
19
|
+
resetValue: state => {
|
|
20
|
+
state.value = 0;
|
|
17
21
|
},
|
|
22
|
+
},
|
|
18
23
|
});
|
|
19
24
|
|
|
20
25
|
export const { setValue, resetValue } = appSlice.actions;
|
|
21
|
-
export default appSlice.reducer;
|
|
26
|
+
export default appSlice.reducer;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { createSlice, PayloadAction } from
|
|
2
|
-
|
|
3
|
-
type
|
|
1
|
+
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
2
|
+
import type { User } from '../types';
|
|
3
|
+
import type { RootStore } from '../store';
|
|
4
4
|
|
|
5
5
|
interface AuthState {
|
|
6
|
-
user: User;
|
|
6
|
+
user: User | null;
|
|
7
7
|
accessToken: string | null;
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -13,17 +13,14 @@ const initialState: AuthState = {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
const authSlice = createSlice({
|
|
16
|
-
name:
|
|
16
|
+
name: 'auth',
|
|
17
17
|
initialState,
|
|
18
18
|
reducers: {
|
|
19
|
-
setCredentials(
|
|
20
|
-
state,
|
|
21
|
-
action: PayloadAction<{ user: User; accessToken: string }>,
|
|
22
|
-
) {
|
|
19
|
+
setCredentials(state, action: PayloadAction<{ user: User; accessToken: string }>) {
|
|
23
20
|
state.user = action.payload.user;
|
|
24
21
|
state.accessToken = action.payload.accessToken;
|
|
25
22
|
},
|
|
26
|
-
setUser(state, action: PayloadAction<User>) {
|
|
23
|
+
setUser(state, action: PayloadAction<User | null>) {
|
|
27
24
|
state.user = action.payload;
|
|
28
25
|
},
|
|
29
26
|
setAccessToken(state, action: PayloadAction<string | null>) {
|
|
@@ -36,10 +33,10 @@ const authSlice = createSlice({
|
|
|
36
33
|
},
|
|
37
34
|
});
|
|
38
35
|
|
|
39
|
-
export const { setCredentials, setUser, setAccessToken, clearAuth } =
|
|
40
|
-
authSlice.actions;
|
|
36
|
+
export const { setCredentials, setUser, setAccessToken, clearAuth } = authSlice.actions;
|
|
41
37
|
|
|
42
|
-
export const selectCurrentUser = (state:
|
|
43
|
-
export const selectAccessToken = (state:
|
|
38
|
+
export const selectCurrentUser = (state: RootStore) => state.auth.user;
|
|
39
|
+
export const selectAccessToken = (state: RootStore) => state.auth.accessToken;
|
|
40
|
+
export const selectIsLoggedIn = (state: RootStore) => !!state.auth.accessToken;
|
|
44
41
|
|
|
45
42
|
export default authSlice.reducer;
|
|
@@ -1,23 +1,27 @@
|
|
|
1
|
-
import { combineReducers, configureStore } from
|
|
2
|
-
import { TypedUseSelectorHook, useDispatch, useSelector } from
|
|
3
|
-
import { persistReducer, persistStore } from
|
|
4
|
-
import reducer from
|
|
5
|
-
import authReducer from
|
|
6
|
-
import { reduxStorage } from
|
|
1
|
+
import { combineReducers, configureStore } from '@reduxjs/toolkit';
|
|
2
|
+
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
|
|
3
|
+
import { persistReducer, persistStore } from 'redux-persist';
|
|
4
|
+
import reducer from './slice';
|
|
5
|
+
import authReducer from './slices/authSlice';
|
|
6
|
+
import { reduxStorage } from '../../constants/StoragePreferences';
|
|
7
|
+
|
|
7
8
|
const persistConfig = {
|
|
8
|
-
key:
|
|
9
|
+
key: 'root',
|
|
9
10
|
storage: reduxStorage,
|
|
10
|
-
|
|
11
|
+
// Only these slices are written to MMKV. Server data belongs in React Query.
|
|
12
|
+
whitelist: ['auth'],
|
|
11
13
|
};
|
|
12
14
|
|
|
13
15
|
const rootReducer = combineReducers({
|
|
14
16
|
global: reducer,
|
|
15
17
|
auth: authReducer,
|
|
16
18
|
});
|
|
19
|
+
|
|
17
20
|
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
|
21
|
+
|
|
18
22
|
export const store = configureStore({
|
|
19
23
|
reducer: persistedReducer,
|
|
20
|
-
middleware:
|
|
24
|
+
middleware: getDefaultMiddleware =>
|
|
21
25
|
getDefaultMiddleware({
|
|
22
26
|
serializableCheck: false,
|
|
23
27
|
}),
|
|
@@ -1,40 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* API
|
|
2
|
+
* Shared types: API request bodies, response models and app entities.
|
|
3
|
+
* Component props stay in their component file; route params live in
|
|
4
|
+
* src/navigation/AppStackParams.ts.
|
|
3
5
|
*/
|
|
6
|
+
|
|
7
|
+
// ---------- API envelopes ----------
|
|
8
|
+
|
|
4
9
|
export interface ApiErrorResponse {
|
|
5
10
|
message: string;
|
|
6
11
|
statusCode?: number;
|
|
7
12
|
code?: string;
|
|
8
|
-
details?: Record<string,
|
|
13
|
+
details?: Record<string, unknown>;
|
|
9
14
|
}
|
|
10
15
|
|
|
11
|
-
/**
|
|
12
|
-
* API Success Response Type
|
|
13
|
-
*/
|
|
14
16
|
export interface ApiSuccessResponse<T> {
|
|
15
17
|
data: T;
|
|
16
18
|
message?: string;
|
|
17
19
|
statusCode?: number;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;
|
|
23
|
+
|
|
24
|
+
export interface PaginationOptions {
|
|
25
|
+
page: number;
|
|
26
|
+
limit: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ---------- Auth ----------
|
|
30
|
+
|
|
31
|
+
export interface User {
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
email: string;
|
|
35
|
+
avatarUrl?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface LoginForm {
|
|
39
|
+
email: string;
|
|
40
|
+
password: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface LoginResponse {
|
|
44
|
+
user: User;
|
|
45
|
+
accessToken: string;
|
|
46
|
+
refreshToken?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ---------- Todos (demo) ----------
|
|
50
|
+
|
|
23
51
|
export interface TodoItem {
|
|
24
52
|
id: number;
|
|
25
53
|
title: string;
|
|
26
54
|
completed: boolean;
|
|
27
55
|
}
|
|
28
56
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
page: number;
|
|
34
|
-
limit: number;
|
|
57
|
+
export interface TodoListParams {
|
|
58
|
+
_page: number;
|
|
59
|
+
_limit: number;
|
|
60
|
+
q?: string;
|
|
35
61
|
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Generic API Response
|
|
39
|
-
*/
|
|
40
|
-
export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;
|
|
@@ -1,38 +1,112 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Pressable, StyleSheet, Text, View } from 'react-native';
|
|
3
3
|
import { SafeAreaView } from 'react-native-safe-area-context';
|
|
4
|
+
|
|
5
|
+
// Hooks / state
|
|
4
6
|
import { useTheme } from '../hooks/ThemeContext';
|
|
7
|
+
import { useAppDispatch, useAppSelector } from '../redux/store';
|
|
8
|
+
import { clearAuth, selectCurrentUser } from '../redux/slices/authSlice';
|
|
9
|
+
|
|
10
|
+
// Helpers / constants
|
|
11
|
+
import { navigateAndSimpleReset } from '../helper/navigation-service';
|
|
12
|
+
import { Typography } from '../helper/typography';
|
|
13
|
+
import { horizontalScale, moderateScale, verticalScale } from '../helper/matrics';
|
|
14
|
+
import { AppString } from '../../constants/AppString';
|
|
15
|
+
|
|
16
|
+
const HomeScreen = () => {
|
|
17
|
+
const { theme, mode, preference, toggleTheme, setPreference } = useTheme();
|
|
18
|
+
const user = useAppSelector(selectCurrentUser);
|
|
19
|
+
const dispatch = useAppDispatch();
|
|
20
|
+
|
|
21
|
+
const onLogout = () => {
|
|
22
|
+
dispatch(clearAuth());
|
|
23
|
+
navigateAndSimpleReset('LoginScreen');
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const outlineButton = [styles.button, styles.outline, { borderColor: theme.border }];
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<SafeAreaView style={[styles.safeArea, { backgroundColor: theme.background }]}>
|
|
30
|
+
<View style={styles.container}>
|
|
31
|
+
<Text style={[Typography.title1, { color: theme.text }]}>
|
|
32
|
+
{AppString.hello}
|
|
33
|
+
{user?.name ? `, ${user.name}` : ''}
|
|
34
|
+
</Text>
|
|
35
|
+
|
|
36
|
+
<View style={[styles.card, { backgroundColor: theme.card, borderColor: theme.border }]}>
|
|
37
|
+
<Text style={[Typography.caption1, styles.overline, { color: theme.muted }]}>
|
|
38
|
+
{AppString.currentTheme}
|
|
39
|
+
</Text>
|
|
40
|
+
<Text style={[Typography.headline, styles.capitalize, { color: theme.text }]}>
|
|
41
|
+
{mode} · {preference}
|
|
42
|
+
</Text>
|
|
43
|
+
</View>
|
|
5
44
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
45
|
+
<View style={styles.actions}>
|
|
46
|
+
<Pressable
|
|
47
|
+
onPress={toggleTheme}
|
|
48
|
+
accessibilityRole="button"
|
|
49
|
+
style={[styles.button, { backgroundColor: theme.primary }]}>
|
|
50
|
+
<Text style={[Typography.headline, { color: theme.onPrimary }]}>
|
|
51
|
+
{AppString.toggleTheme}
|
|
52
|
+
</Text>
|
|
53
|
+
</Pressable>
|
|
54
|
+
<Pressable
|
|
55
|
+
onPress={() => setPreference('system')}
|
|
56
|
+
disabled={preference === 'system'}
|
|
57
|
+
accessibilityRole="button"
|
|
58
|
+
style={[outlineButton, preference === 'system' && styles.disabled]}>
|
|
59
|
+
<Text style={[Typography.headline, { color: theme.text }]}>
|
|
60
|
+
{AppString.followSystem}
|
|
61
|
+
</Text>
|
|
62
|
+
</Pressable>
|
|
63
|
+
<Pressable onPress={onLogout} accessibilityRole="button" style={outlineButton}>
|
|
64
|
+
<Text style={[Typography.headline, { color: theme.error }]}>{AppString.logout}</Text>
|
|
65
|
+
</Pressable>
|
|
66
|
+
</View>
|
|
67
|
+
</View>
|
|
68
|
+
</SafeAreaView>
|
|
69
|
+
);
|
|
23
70
|
};
|
|
24
71
|
|
|
25
72
|
export default HomeScreen;
|
|
26
73
|
|
|
27
74
|
const styles = StyleSheet.create({
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
75
|
+
safeArea: {
|
|
76
|
+
flex: 1,
|
|
77
|
+
},
|
|
78
|
+
container: {
|
|
79
|
+
flex: 1,
|
|
80
|
+
justifyContent: 'center',
|
|
81
|
+
paddingHorizontal: horizontalScale(24),
|
|
82
|
+
},
|
|
83
|
+
card: {
|
|
84
|
+
marginTop: verticalScale(32),
|
|
85
|
+
borderRadius: moderateScale(16),
|
|
86
|
+
borderWidth: 1,
|
|
87
|
+
padding: moderateScale(20),
|
|
88
|
+
},
|
|
89
|
+
overline: {
|
|
90
|
+
textTransform: 'uppercase',
|
|
91
|
+
},
|
|
92
|
+
capitalize: {
|
|
93
|
+
marginTop: verticalScale(4),
|
|
94
|
+
textTransform: 'capitalize',
|
|
95
|
+
},
|
|
96
|
+
actions: {
|
|
97
|
+
marginTop: verticalScale(24),
|
|
98
|
+
gap: moderateScale(12),
|
|
99
|
+
},
|
|
100
|
+
button: {
|
|
101
|
+
height: 48,
|
|
102
|
+
borderRadius: moderateScale(12),
|
|
103
|
+
alignItems: 'center',
|
|
104
|
+
justifyContent: 'center',
|
|
105
|
+
},
|
|
106
|
+
outline: {
|
|
107
|
+
borderWidth: 1,
|
|
108
|
+
},
|
|
109
|
+
disabled: {
|
|
110
|
+
opacity: 0.5,
|
|
111
|
+
},
|
|
38
112
|
});
|
|
@@ -1,76 +1,101 @@
|
|
|
1
|
-
import React, { useState } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { KeyboardAvoidingView, Pressable, StyleSheet, Text, TextInput, View } from 'react-native';
|
|
3
|
+
import { SafeAreaView } from 'react-native-safe-area-context';
|
|
4
|
+
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
|
5
|
+
import { RouteProp } from '@react-navigation/native';
|
|
6
|
+
|
|
7
|
+
// Types
|
|
8
|
+
import { AppStackParams } from '../navigation/AppStackParams';
|
|
9
|
+
|
|
10
|
+
// Hooks / state
|
|
11
|
+
import { useTheme } from '../hooks/ThemeContext';
|
|
12
|
+
import { useAppDispatch } from '../redux/store';
|
|
13
|
+
import { setCredentials } from '../redux/slices/authSlice';
|
|
14
|
+
|
|
15
|
+
// Helpers / constants
|
|
16
|
+
import { showSnackbar } from '../helper/common-functions';
|
|
17
|
+
import { isIOS } from '../helper/utility-functions';
|
|
18
|
+
import { Typography } from '../helper/typography';
|
|
19
|
+
import { horizontalScale, moderateScale, verticalScale } from '../helper/matrics';
|
|
20
|
+
import { AppString } from '../../constants/AppString';
|
|
16
21
|
|
|
17
22
|
type Props = {
|
|
18
|
-
navigation: NativeStackNavigationProp<AppStackParams,
|
|
19
|
-
route: RouteProp<AppStackParams,
|
|
23
|
+
navigation: NativeStackNavigationProp<AppStackParams, 'LoginScreen'>;
|
|
24
|
+
route: RouteProp<AppStackParams, 'LoginScreen'>;
|
|
20
25
|
};
|
|
21
26
|
|
|
22
27
|
const LoginScreen = ({ navigation }: Props) => {
|
|
23
28
|
const { theme } = useTheme();
|
|
24
29
|
const dispatch = useAppDispatch();
|
|
25
|
-
const [email, setEmail] = useState(
|
|
26
|
-
const [password, setPassword] = useState(
|
|
30
|
+
const [email, setEmail] = useState('');
|
|
31
|
+
const [password, setPassword] = useState('');
|
|
32
|
+
|
|
33
|
+
const inputStyle = [
|
|
34
|
+
styles.input,
|
|
35
|
+
Typography.body1,
|
|
36
|
+
{ backgroundColor: theme.card, borderColor: theme.border, color: theme.text },
|
|
37
|
+
];
|
|
27
38
|
|
|
28
39
|
const onLogin = () => {
|
|
29
|
-
//
|
|
40
|
+
// Demo only: stores dummy credentials. With a real backend, call the
|
|
41
|
+
// mutation instead:
|
|
42
|
+
// const { mutate: login, isPending } = useLogin();
|
|
43
|
+
// login({ email, password }, {
|
|
44
|
+
// onSuccess: res => dispatch(setCredentials(res)),
|
|
45
|
+
// onError: err => showErrorSnackbar(getErrorMessage(err)),
|
|
46
|
+
// });
|
|
30
47
|
dispatch(
|
|
31
48
|
setCredentials({
|
|
32
|
-
user: { id:
|
|
33
|
-
accessToken:
|
|
49
|
+
user: { id: '1', name: 'Demo User', email: email || 'demo@local' },
|
|
50
|
+
accessToken: 'dummy-access-token-123',
|
|
34
51
|
}),
|
|
35
52
|
);
|
|
36
|
-
|
|
53
|
+
showSnackbar(AppString.loginSuccess);
|
|
54
|
+
navigation.reset({ index: 0, routes: [{ name: 'HomeScreen' }] });
|
|
37
55
|
};
|
|
38
56
|
|
|
39
57
|
return (
|
|
40
|
-
<SafeAreaView style={{
|
|
41
|
-
<
|
|
42
|
-
<Text style={[styles.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
<SafeAreaView style={[styles.safeArea, { backgroundColor: theme.background }]}>
|
|
59
|
+
<KeyboardAvoidingView behavior={isIOS ? 'padding' : undefined} style={styles.container}>
|
|
60
|
+
<Text style={[Typography.title1, styles.center, { color: theme.text }]}>
|
|
61
|
+
{AppString.welcomeBack}
|
|
62
|
+
</Text>
|
|
63
|
+
<Text style={[Typography.body2, styles.center, styles.subtitle, { color: theme.muted }]}>
|
|
64
|
+
{AppString.signInToContinue}
|
|
65
|
+
</Text>
|
|
66
|
+
|
|
67
|
+
<View style={styles.fields}>
|
|
68
|
+
<TextInput
|
|
69
|
+
placeholder={AppString.email}
|
|
70
|
+
placeholderTextColor={theme.muted}
|
|
71
|
+
style={inputStyle}
|
|
72
|
+
value={email}
|
|
73
|
+
onChangeText={setEmail}
|
|
74
|
+
keyboardType="email-address"
|
|
75
|
+
autoCapitalize="none"
|
|
76
|
+
autoComplete="email"
|
|
77
|
+
/>
|
|
78
|
+
<TextInput
|
|
79
|
+
placeholder={AppString.password}
|
|
80
|
+
placeholderTextColor={theme.muted}
|
|
81
|
+
style={inputStyle}
|
|
82
|
+
value={password}
|
|
83
|
+
onChangeText={setPassword}
|
|
84
|
+
secureTextEntry
|
|
85
|
+
autoComplete="password"
|
|
86
|
+
/>
|
|
87
|
+
</View>
|
|
66
88
|
|
|
67
|
-
<
|
|
68
|
-
style={[styles.button, { backgroundColor: theme.primary }]}
|
|
89
|
+
<Pressable
|
|
69
90
|
onPress={onLogin}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
91
|
+
accessibilityRole="button"
|
|
92
|
+
style={({ pressed }) => [
|
|
93
|
+
styles.button,
|
|
94
|
+
{ backgroundColor: theme.primary, opacity: pressed ? 0.8 : 1 },
|
|
95
|
+
]}>
|
|
96
|
+
<Text style={[Typography.headline, { color: theme.onPrimary }]}>{AppString.login}</Text>
|
|
97
|
+
</Pressable>
|
|
98
|
+
</KeyboardAvoidingView>
|
|
74
99
|
</SafeAreaView>
|
|
75
100
|
);
|
|
76
101
|
};
|
|
@@ -78,28 +103,35 @@ const LoginScreen = ({ navigation }: Props) => {
|
|
|
78
103
|
export default LoginScreen;
|
|
79
104
|
|
|
80
105
|
const styles = StyleSheet.create({
|
|
106
|
+
safeArea: {
|
|
107
|
+
flex: 1,
|
|
108
|
+
},
|
|
81
109
|
container: {
|
|
82
110
|
flex: 1,
|
|
83
|
-
justifyContent:
|
|
84
|
-
|
|
111
|
+
justifyContent: 'center',
|
|
112
|
+
paddingHorizontal: horizontalScale(24),
|
|
113
|
+
},
|
|
114
|
+
center: {
|
|
115
|
+
textAlign: 'center',
|
|
116
|
+
},
|
|
117
|
+
subtitle: {
|
|
118
|
+
marginTop: verticalScale(8),
|
|
119
|
+
marginBottom: verticalScale(32),
|
|
85
120
|
},
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
fontWeight: "600",
|
|
89
|
-
marginBottom: 24,
|
|
90
|
-
textAlign: "center",
|
|
121
|
+
fields: {
|
|
122
|
+
gap: moderateScale(12),
|
|
91
123
|
},
|
|
92
124
|
input: {
|
|
93
125
|
height: 48,
|
|
94
|
-
borderRadius:
|
|
95
|
-
|
|
96
|
-
|
|
126
|
+
borderRadius: moderateScale(12),
|
|
127
|
+
borderWidth: 1,
|
|
128
|
+
paddingHorizontal: horizontalScale(16),
|
|
97
129
|
},
|
|
98
130
|
button: {
|
|
99
131
|
height: 48,
|
|
100
|
-
borderRadius:
|
|
101
|
-
alignItems:
|
|
102
|
-
justifyContent:
|
|
103
|
-
marginTop:
|
|
132
|
+
borderRadius: moderateScale(12),
|
|
133
|
+
alignItems: 'center',
|
|
134
|
+
justifyContent: 'center',
|
|
135
|
+
marginTop: verticalScale(24),
|
|
104
136
|
},
|
|
105
137
|
});
|