@umang-thakkar/base-template 1.1.1 → 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.
Files changed (45) hide show
  1. package/README.md +40 -282
  2. package/package.json +11 -2
  3. package/template/.eslintrc +16 -46
  4. package/template/.prettierrc.js +8 -0
  5. package/template/App.tsx +11 -15
  6. package/template/Main.tsx +29 -5
  7. package/template/__mocks__/react-native-mmkv.ts +59 -0
  8. package/template/__tests__/App.test.tsx +6 -1
  9. package/template/__tests__/helpers.test.ts +43 -0
  10. package/template/babel.config.js +2 -1
  11. package/template/constants/API_Routes.ts +19 -0
  12. package/template/constants/AppConstants.tsx +1 -1
  13. package/template/constants/AppString.ts +36 -0
  14. package/template/constants/StoragePreferences.ts +120 -57
  15. package/template/jest.config.js +5 -0
  16. package/template/jest.setup.js +5 -0
  17. package/template/package.json +4 -4
  18. package/template/src/api/AxiosActions.ts +65 -28
  19. package/template/src/api/AxiosClient.ts +13 -7
  20. package/template/src/api/queries.ts +59 -29
  21. package/template/src/api/service.ts +23 -9
  22. package/template/src/components/ErrorBoundary.tsx +27 -31
  23. package/template/src/components/SnackbarHost.tsx +86 -0
  24. package/template/src/helper/colors.ts +22 -22
  25. package/template/src/helper/common-functions.ts +56 -10
  26. package/template/src/helper/matrics.ts +30 -0
  27. package/template/src/helper/navigation-service.ts +21 -24
  28. package/template/src/helper/snackbar.ts +41 -0
  29. package/template/src/helper/typography.ts +2 -2
  30. package/template/src/helper/utility-functions.ts +41 -69
  31. package/template/src/hooks/ThemeContext.tsx +60 -30
  32. package/template/src/hooks/usePaginatedAPI.ts +96 -0
  33. package/template/src/navigation/AppStack.tsx +36 -27
  34. package/template/src/navigation/screens.ts +17 -11
  35. package/template/src/redux/slice.ts +17 -12
  36. package/template/src/redux/slices/authSlice.ts +11 -14
  37. package/template/src/redux/store.ts +14 -10
  38. package/template/src/redux/types.d.ts +40 -19
  39. package/template/src/screens/HomeScreen.tsx +103 -29
  40. package/template/src/screens/LoginScreen.tsx +101 -69
  41. package/template/src/screens/SplashScreen.tsx +36 -25
  42. package/template/src/theme/darkTheme.ts +13 -6
  43. package/template/src/theme/index.ts +4 -3
  44. package/template/src/theme/lightTheme.ts +13 -5
  45. package/template/ios/.xcode.env.local +0 -1
@@ -1,76 +1,101 @@
1
- import React, { useState } from "react";
2
- import {
3
- View,
4
- Text,
5
- StyleSheet,
6
- TextInput,
7
- TouchableOpacity,
8
- } from "react-native";
9
- import { SafeAreaView } from "react-native-safe-area-context";
10
- import { useTheme } from "../hooks/ThemeContext";
11
- import { useAppDispatch } from "../redux/store";
12
- import { setCredentials } from "../redux/slices/authSlice";
13
- import { NativeStackNavigationProp } from "@react-navigation/native-stack";
14
- import { RouteProp } from "@react-navigation/native";
15
- import { AppStackParams } from "../navigation/AppStackParams";
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, "LoginScreen">;
19
- route: RouteProp<AppStackParams, "LoginScreen">;
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
- // store dummy data in auth reducer
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: "1", name: "Demo User", email: email || "demo@local" },
33
- accessToken: "dummy-access-token-123",
49
+ user: { id: '1', name: 'Demo User', email: email || 'demo@local' },
50
+ accessToken: 'dummy-access-token-123',
34
51
  }),
35
52
  );
36
- navigation.reset({ index: 0, routes: [{ name: "HomeScreen" }] });
53
+ showSnackbar(AppString.loginSuccess);
54
+ navigation.reset({ index: 0, routes: [{ name: 'HomeScreen' }] });
37
55
  };
38
56
 
39
57
  return (
40
- <SafeAreaView style={{ flex: 1, backgroundColor: theme.background }}>
41
- <View style={[styles.container, { backgroundColor: theme.background }]}>
42
- <Text style={[styles.title, { color: theme.text }]}>Welcome back</Text>
43
- <TextInput
44
- placeholder="Email"
45
- placeholderTextColor={theme.text + "88"}
46
- style={[
47
- styles.input,
48
- { backgroundColor: theme.card, color: theme.text },
49
- ]}
50
- value={email}
51
- onChangeText={setEmail}
52
- keyboardType="email-address"
53
- autoCapitalize="none"
54
- />
55
- <TextInput
56
- placeholder="Password"
57
- placeholderTextColor={theme.text + "88"}
58
- style={[
59
- styles.input,
60
- { backgroundColor: theme.card, color: theme.text },
61
- ]}
62
- value={password}
63
- onChangeText={setPassword}
64
- secureTextEntry
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
- <TouchableOpacity
68
- style={[styles.button, { backgroundColor: theme.primary }]}
89
+ <Pressable
69
90
  onPress={onLogin}
70
- >
71
- <Text style={{ color: theme.text }}>Sign in</Text>
72
- </TouchableOpacity>
73
- </View>
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: "center",
84
- padding: 24,
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
- title: {
87
- fontSize: 28,
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: 8,
95
- paddingHorizontal: 12,
96
- marginBottom: 12,
126
+ borderRadius: moderateScale(12),
127
+ borderWidth: 1,
128
+ paddingHorizontal: horizontalScale(16),
97
129
  },
98
130
  button: {
99
131
  height: 48,
100
- borderRadius: 8,
101
- alignItems: "center",
102
- justifyContent: "center",
103
- marginTop: 12,
132
+ borderRadius: moderateScale(12),
133
+ alignItems: 'center',
134
+ justifyContent: 'center',
135
+ marginTop: verticalScale(24),
104
136
  },
105
137
  });
@@ -1,36 +1,40 @@
1
- import * as React from "react";
2
- import { Text, View, StyleSheet } from "react-native";
3
- import { NativeStackNavigationProp } from "@react-navigation/native-stack";
4
- import { RouteProp } from "@react-navigation/native";
5
- import { AppStackParams } from "../navigation/AppStackParams";
6
- import { useEffect } from "react";
7
- import { useAppSelector } from "../redux/store";
1
+ import React, { useEffect } from 'react';
2
+ import { ActivityIndicator, StyleSheet, Text, View } from 'react-native';
3
+ import { NativeStackNavigationProp } from '@react-navigation/native-stack';
4
+ import { RouteProp } from '@react-navigation/native';
5
+ import { AppStackParams } from '../navigation/AppStackParams';
6
+ import { useAppSelector } from '../redux/store';
7
+ import { selectAccessToken } from '../redux/slices/authSlice';
8
+ import { useTheme } from '../hooks/ThemeContext';
9
+ import { Typography } from '../helper/typography';
10
+ import { verticalScale } from '../helper/matrics';
11
+ import { name as appName } from '../../app.json';
8
12
 
9
- import { selectAccessToken } from "../redux/slices/authSlice";
10
13
  type Props = {
11
- navigation: NativeStackNavigationProp<
12
- AppStackParams,
13
- "SplashScreen",
14
- "SplashScreen"
15
- >;
16
- route: RouteProp<AppStackParams, "SplashScreen">;
14
+ navigation: NativeStackNavigationProp<AppStackParams, 'SplashScreen'>;
15
+ route: RouteProp<AppStackParams, 'SplashScreen'>;
17
16
  };
18
17
 
18
+ const SPLASH_DELAY_MS = 800;
19
+
19
20
  const SplashScreen = ({ navigation }: Props) => {
20
21
  const accessToken = useAppSelector(selectAccessToken);
22
+ const { theme } = useTheme();
21
23
 
22
24
  useEffect(() => {
23
- setTimeout(() => {
24
- if (accessToken) {
25
- navigation.reset({ index: 0, routes: [{ name: "HomeScreen" }] });
26
- } else {
27
- navigation.reset({ index: 0, routes: [{ name: "LoginScreen" }] });
28
- }
29
- }, 800);
30
- }, [accessToken]);
25
+ const timer = setTimeout(() => {
26
+ navigation.reset({
27
+ index: 0,
28
+ routes: [{ name: accessToken ? 'HomeScreen' : 'LoginScreen' }],
29
+ });
30
+ }, SPLASH_DELAY_MS);
31
+ return () => clearTimeout(timer);
32
+ }, [accessToken, navigation]);
33
+
31
34
  return (
32
- <View style={styles.container}>
33
- <Text>SplashScreen</Text>
35
+ <View style={[styles.container, { backgroundColor: theme.background }]}>
36
+ <Text style={[Typography.title1, { color: theme.text }]}>{appName}</Text>
37
+ <ActivityIndicator style={styles.loader} color={theme.primary} />
34
38
  </View>
35
39
  );
36
40
  };
@@ -38,5 +42,12 @@ const SplashScreen = ({ navigation }: Props) => {
38
42
  export default SplashScreen;
39
43
 
40
44
  const styles = StyleSheet.create({
41
- container: {},
45
+ container: {
46
+ flex: 1,
47
+ alignItems: 'center',
48
+ justifyContent: 'center',
49
+ },
50
+ loader: {
51
+ marginTop: verticalScale(24),
52
+ },
42
53
  });
@@ -1,7 +1,14 @@
1
- export const darkTheme = {
2
- background: "#000000",
3
- text: "#FFFFFF",
4
- primary: "#4045EF",
5
- secondary: "#1A1A1A",
6
- card: "#FFFF",
1
+ import type { ThemeType } from './index';
2
+
3
+ /** Dark theme colors. Must have the same keys as lightTheme.ts. */
4
+ export const darkTheme: ThemeType = {
5
+ background: '#000000',
6
+ card: '#18181B',
7
+ text: '#FFFFFF',
8
+ muted: '#A1A1AA',
9
+ border: '#27272A',
10
+ primary: '#4045EF',
11
+ onPrimary: '#FFFFFF',
12
+ secondary: '#1A1A1A',
13
+ error: '#F87171',
7
14
  };
@@ -2,8 +2,9 @@ import { lightTheme } from './lightTheme';
2
2
  import { darkTheme } from './darkTheme';
3
3
 
4
4
  export type ThemeType = typeof lightTheme;
5
+ export type ThemeMode = 'light' | 'dark';
5
6
 
6
- export const themes = {
7
- light: lightTheme,
8
- dark: darkTheme,
7
+ export const themes: Record<ThemeMode, ThemeType> = {
8
+ light: lightTheme,
9
+ dark: darkTheme,
9
10
  };
@@ -1,7 +1,15 @@
1
+ /**
2
+ * Light theme colors.
3
+ * Adding a token? Add it to darkTheme.ts too (same keys).
4
+ */
1
5
  export const lightTheme = {
2
- background: "#FFFFFF",
3
- text: "#000000",
4
- primary: "#4045EF",
5
- secondary: "#F4F4F4",
6
- card: "#FFFF",
6
+ background: '#FFFFFF',
7
+ card: '#F4F4F5',
8
+ text: '#000000',
9
+ muted: '#71717A',
10
+ border: '#E4E4E7',
11
+ primary: '#4045EF',
12
+ onPrimary: '#FFFFFF',
13
+ secondary: '#F4F4F4',
14
+ error: '#DC2626',
7
15
  };
@@ -1 +0,0 @@
1
- export NODE_BINARY=/opt/homebrew/Cellar/node/24.1.0/bin/node