@umang-thakkar/base-template 1.0.8 → 1.0.9
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 +107 -38
- package/package.json +1 -1
- package/template/.eslintignore +12 -0
- package/template/.eslintrc +130 -0
- package/template/App.tsx +32 -31
- package/template/Main.tsx +11 -11
- package/template/package.json +2 -1
- package/template/src/components/ErrorBoundary.tsx +154 -0
- package/template/src/navigation/AppStackParams.ts +4 -3
- package/template/src/navigation/screens.ts +9 -7
- package/template/src/redux/slices/authSlice.ts +45 -0
- package/template/src/redux/store.ts +24 -7
- package/template/src/redux/types.d.ts +40 -1
- package/template/src/screens/LoginScreen.tsx +105 -0
- package/template/src/screens/SplashScreen.tsx +32 -23
- package/template/src/theme/darkTheme.ts +5 -4
- package/template/src/theme/lightTheme.ts +6 -5
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
A React Native boilerplate to kickstart your mobile app development faster, with preconfigured essentials like:
|
|
4
4
|
|
|
5
5
|
- 🔁 Redux Toolkit for scalable state management
|
|
6
|
+
- 💾 Redux Persist for auth persistence across sessions
|
|
6
7
|
- 📍 React Navigation for robust navigation
|
|
7
8
|
- 🔗 React Query for powerful API data fetching and caching
|
|
8
9
|
- 🌙 Dark/Light theme support with system preference detection
|
|
@@ -14,6 +15,7 @@ A React Native boilerplate to kickstart your mobile app development faster, with
|
|
|
14
15
|
## 📦 Features
|
|
15
16
|
|
|
16
17
|
- ✅ Redux Toolkit with example slices and hooks
|
|
18
|
+
- ✅ Redux Persist with auth slice persistence (user data + access token)
|
|
17
19
|
- ✅ React Navigation with Stack Navigator setup
|
|
18
20
|
- ✅ React Query for API management (see `src/api/queries.ts`)
|
|
19
21
|
- ✅ Dark/Light theme support with automatic system detection
|
|
@@ -62,12 +64,14 @@ template/
|
|
|
62
64
|
│ │ └── screens.ts
|
|
63
65
|
│ ├── redux/ # Redux store, slices, types
|
|
64
66
|
│ │ ├── action.ts
|
|
65
|
-
│ │ ├──
|
|
66
|
-
│ │
|
|
67
|
+
│ │ ├── slices/
|
|
68
|
+
│ │ │ └── authSlice.ts # Auth slice with user data + token state
|
|
69
|
+
│ │ ├── store.ts # Store with redux-persist configuration
|
|
67
70
|
│ │ └── types.d.ts
|
|
68
71
|
│ ├── screens/ # App screens
|
|
69
72
|
│ │ ├── HomeScreen.tsx
|
|
70
|
-
│ │
|
|
73
|
+
│ │ ├── LoginScreen.tsx # Demo login screen — saves user + token to auth slice
|
|
74
|
+
│ │ └── SplashScreen.tsx # Reads accessToken from Redux to decide initial route
|
|
71
75
|
│ ├── theme/ # Theme configuration
|
|
72
76
|
│ │ ├── darkTheme.ts # Dark theme colors
|
|
73
77
|
│ │ ├── lightTheme.ts # Light theme colors
|
|
@@ -81,30 +85,32 @@ template/
|
|
|
81
85
|
|
|
82
86
|
## 🚀 Getting Started
|
|
83
87
|
|
|
84
|
-
1. Create a new project using this template:
|
|
88
|
+
1. Create a new project using this template:
|
|
85
89
|
```sh
|
|
86
90
|
npx @react-native-community/cli init MyApp --template @umang-thakkar/base-template
|
|
87
91
|
```
|
|
92
|
+
|
|
88
93
|
```
|
|
89
94
|
Note: Replace MyApp with your own project name.
|
|
90
95
|
```
|
|
91
96
|
|
|
92
97
|
2. Install dependencies:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
98
|
+
```sh
|
|
99
|
+
npm install
|
|
100
|
+
```
|
|
96
101
|
3. Start Metro server:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
102
|
+
```sh
|
|
103
|
+
npx react-native start
|
|
104
|
+
```
|
|
100
105
|
4. Run the app:
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
# For Android
|
|
109
|
+
npx react-native run-android
|
|
110
|
+
|
|
111
|
+
# For iOS
|
|
112
|
+
npx react-native run-ios
|
|
113
|
+
```
|
|
108
114
|
|
|
109
115
|
> **Note**: With React Native 0.60+, iOS dependencies are automatically linked using autolinking. Manual pod installation is no longer required for most cases.
|
|
110
116
|
|
|
@@ -122,30 +128,91 @@ Note: Replace MyApp with your own project name.
|
|
|
122
128
|
<Button title="Increment" onPress={() => dispatch(increment())} />;
|
|
123
129
|
```
|
|
124
130
|
|
|
131
|
+
### 💾 Redux Persist (Auth Persistence)
|
|
132
|
+
|
|
133
|
+
Redux Persist is pre-configured in the template to persist the `auth` slice across app restarts. The user's data and `accessToken` survive kills and relaunches — no need to log in again.
|
|
134
|
+
|
|
135
|
+
> `redux-persist` and `@react-native-async-storage/async-storage` are already installed and configured.
|
|
136
|
+
|
|
137
|
+
**What's included out of the box:**
|
|
138
|
+
|
|
139
|
+
`src/redux/slices/authSlice.ts` stores `user` and `accessToken` with the following actions:
|
|
140
|
+
|
|
141
|
+
| Action | Description |
|
|
142
|
+
| ---------------- | ---------------------------------------------- |
|
|
143
|
+
| `setCredentials` | Sets both `user` and `accessToken` after login |
|
|
144
|
+
| `setUser` | Updates only the `user` object |
|
|
145
|
+
| `setAccessToken` | Updates only the `accessToken` |
|
|
146
|
+
| `clearAuth` | Clears all auth state on logout |
|
|
147
|
+
|
|
148
|
+
`src/redux/store.ts` configures `persistReducer` to persist only the `auth` slice via `AsyncStorage` and exports `persistor`.
|
|
149
|
+
|
|
150
|
+
`App.tsx` wraps the app with `PersistGate` so persisted state is fully rehydrated before the UI renders.
|
|
151
|
+
|
|
152
|
+
`src/screens/SplashScreen.tsx` reads `accessToken` from Redux on startup — if present, navigates to `HomeScreen`, otherwise to `LoginScreen`.
|
|
153
|
+
|
|
154
|
+
`src/screens/LoginScreen.tsx` is a demo login screen that dispatches `setCredentials` with dummy `user` and `accessToken` data to demonstrate the persistence flow.
|
|
155
|
+
|
|
156
|
+
**Usage example — login:**
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
import { useAppDispatch } from "../redux/store";
|
|
160
|
+
import { setCredentials } from "../redux/slices/authSlice";
|
|
161
|
+
|
|
162
|
+
const dispatch = useAppDispatch();
|
|
163
|
+
|
|
164
|
+
// After a successful login API call:
|
|
165
|
+
dispatch(setCredentials({ user: userData, accessToken: token }));
|
|
166
|
+
// State is automatically persisted to AsyncStorage
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**Usage example — logout:**
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
import { useAppDispatch } from "../redux/store";
|
|
173
|
+
import { clearAuth } from "../redux/slices/authSlice";
|
|
174
|
+
|
|
175
|
+
const dispatch = useAppDispatch();
|
|
176
|
+
dispatch(clearAuth()); // Clears Redux state and AsyncStorage
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**Usage example — read auth state:**
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
import { useAppSelector } from "../redux/store";
|
|
183
|
+
|
|
184
|
+
const { user, accessToken } = useAppSelector((state) => state.auth);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**How it works end-to-end:**
|
|
188
|
+
|
|
189
|
+
- On login → dispatch `setCredentials` → persisted to `AsyncStorage`
|
|
190
|
+
- On app restart → `PersistGate` rehydrates `auth` → `SplashScreen` sees `accessToken` → navigates directly to `HomeScreen`
|
|
191
|
+
- On logout → dispatch `clearAuth` → persisted data cleared → `SplashScreen` routes to `LoginScreen`
|
|
192
|
+
|
|
125
193
|
### 🌙 Dark/Light Theme Support
|
|
126
194
|
|
|
127
195
|
The template includes a complete theming system with automatic system preference detection:
|
|
128
196
|
|
|
129
197
|
- **Theme files**: Located in `src/theme/`
|
|
130
198
|
- `darkTheme.ts` - Dark theme colors
|
|
131
|
-
- `lightTheme.ts` - Light theme colors
|
|
199
|
+
- `lightTheme.ts` - Light theme colors
|
|
132
200
|
- `index.ts` - Theme exports and TypeScript types
|
|
133
201
|
|
|
134
202
|
- **Theme Context**: `src/hooks/ThemeContext.tsx` provides theme management
|
|
135
203
|
|
|
136
204
|
- **Usage in components**:
|
|
205
|
+
|
|
137
206
|
```tsx
|
|
138
|
-
import { useTheme } from
|
|
139
|
-
|
|
207
|
+
import { useTheme } from "../hooks/ThemeContext";
|
|
208
|
+
|
|
140
209
|
const MyComponent = () => {
|
|
141
210
|
const { theme, mode, toggleTheme } = useTheme();
|
|
142
|
-
|
|
211
|
+
|
|
143
212
|
return (
|
|
144
213
|
<View style={{ backgroundColor: theme.background }}>
|
|
145
|
-
<Text style={{ color: theme.text }}>
|
|
146
|
-
|
|
147
|
-
</Text>
|
|
148
|
-
<TouchableOpacity
|
|
214
|
+
<Text style={{ color: theme.text }}>Current theme: {mode}</Text>
|
|
215
|
+
<TouchableOpacity
|
|
149
216
|
style={{ backgroundColor: theme.primary }}
|
|
150
217
|
onPress={toggleTheme}
|
|
151
218
|
>
|
|
@@ -172,9 +239,9 @@ The template includes a complete theming system with automatic system preference
|
|
|
172
239
|
- API calls and React Query hooks are in `src/api/queries.ts`.
|
|
173
240
|
- Example usage:
|
|
174
241
|
```ts
|
|
175
|
-
import { useQuery } from
|
|
176
|
-
import { fetchData } from
|
|
177
|
-
const { data, isLoading } = useQuery(
|
|
242
|
+
import { useQuery } from "react-query";
|
|
243
|
+
import { fetchData } from "../api/queries";
|
|
244
|
+
const { data, isLoading } = useQuery("data", fetchData);
|
|
178
245
|
```
|
|
179
246
|
|
|
180
247
|
### 🧰 Utility Functions
|
|
@@ -192,6 +259,8 @@ The template includes a complete theming system with automatic system preference
|
|
|
192
259
|
- Keep shared components in a `components/` folder (add as needed)
|
|
193
260
|
- Customize theme colors in `src/theme/darkTheme.ts` and `src/theme/lightTheme.ts`
|
|
194
261
|
- Extend theme properties by updating the theme type in `src/theme/index.ts`
|
|
262
|
+
- To persist additional slices, create a separate `persistConfig` per slice and wrap with `persistReducer`
|
|
263
|
+
- To exclude sensitive fields from persistence, use the `blacklist` array in the persist config instead of `whitelist`
|
|
195
264
|
|
|
196
265
|
## 🎨 Theme Customization
|
|
197
266
|
|
|
@@ -200,19 +269,19 @@ To customize the theme colors, edit the theme files:
|
|
|
200
269
|
```ts
|
|
201
270
|
// src/theme/lightTheme.ts
|
|
202
271
|
export const lightTheme = {
|
|
203
|
-
background:
|
|
204
|
-
text:
|
|
205
|
-
primary:
|
|
206
|
-
secondary:
|
|
272
|
+
background: "#FFFFFF",
|
|
273
|
+
text: "#000000",
|
|
274
|
+
primary: "#4045EF",
|
|
275
|
+
secondary: "#F4F4F4",
|
|
207
276
|
// Add your custom colors here
|
|
208
277
|
};
|
|
209
278
|
|
|
210
|
-
// src/theme/darkTheme.ts
|
|
279
|
+
// src/theme/darkTheme.ts
|
|
211
280
|
export const darkTheme = {
|
|
212
|
-
background:
|
|
213
|
-
text:
|
|
214
|
-
primary:
|
|
215
|
-
secondary:
|
|
281
|
+
background: "#000000",
|
|
282
|
+
text: "#FFFFFF",
|
|
283
|
+
primary: "#4045EF",
|
|
284
|
+
secondary: "#1A1A1A",
|
|
216
285
|
// Add your custom colors here
|
|
217
286
|
};
|
|
218
287
|
```
|
|
@@ -227,4 +296,4 @@ Created with ❤️ by **[Umang Thakkar](https://github.com/Umang2809)**
|
|
|
227
296
|
|
|
228
297
|
## 📜 License
|
|
229
298
|
|
|
230
|
-
MIT © [Umang Thakkar](https://github.com/Umang2809)
|
|
299
|
+
MIT © [Umang Thakkar](https://github.com/Umang2809)
|
package/package.json
CHANGED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"extends": [
|
|
4
|
+
"@react-native",
|
|
5
|
+
"@react-native-community",
|
|
6
|
+
"plugin:@tanstack/eslint-plugin-query/recommended",
|
|
7
|
+
],
|
|
8
|
+
"parser": "@typescript-eslint/parser",
|
|
9
|
+
"parserOptions": {
|
|
10
|
+
"ecmaVersion": 2021,
|
|
11
|
+
"sourceType": "module",
|
|
12
|
+
"ecmaFeatures": {
|
|
13
|
+
"jsx": true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
"plugins": [
|
|
17
|
+
"react",
|
|
18
|
+
"react-native",
|
|
19
|
+
"react-hooks",
|
|
20
|
+
"@typescript-eslint",
|
|
21
|
+
"@tanstack/query",
|
|
22
|
+
],
|
|
23
|
+
"env": {
|
|
24
|
+
"browser": true,
|
|
25
|
+
"es2021": true,
|
|
26
|
+
"node": true,
|
|
27
|
+
"react-native/react-native": true,
|
|
28
|
+
},
|
|
29
|
+
"rules": {
|
|
30
|
+
// React best practices
|
|
31
|
+
"react/react-in-jsx-scope": "off",
|
|
32
|
+
"react/prop-types": "off",
|
|
33
|
+
"react/display-name": "warn",
|
|
34
|
+
"react-hooks/rules-of-hooks": "error",
|
|
35
|
+
"react-hooks/exhaustive-deps": "warn",
|
|
36
|
+
|
|
37
|
+
// TypeScript best practices
|
|
38
|
+
"@typescript-eslint/no-unused-vars": [
|
|
39
|
+
"warn",
|
|
40
|
+
{
|
|
41
|
+
"argsIgnorePattern": "^_",
|
|
42
|
+
"varsIgnorePattern": "^_",
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
"@typescript-eslint/no-explicit-any": "warn",
|
|
46
|
+
"@typescript-eslint/explicit-function-return-types": "off",
|
|
47
|
+
"@typescript-eslint/no-non-null-assertion": "warn",
|
|
48
|
+
|
|
49
|
+
// React Native best practices
|
|
50
|
+
"react-native/no-unused-styles": "warn",
|
|
51
|
+
"react-native/split-platform-components": "warn",
|
|
52
|
+
"react-native/no-inline-styles": "warn",
|
|
53
|
+
"react-native/no-color-literals": "warn",
|
|
54
|
+
|
|
55
|
+
// General best practices
|
|
56
|
+
"no-console": [
|
|
57
|
+
"warn",
|
|
58
|
+
{
|
|
59
|
+
"allow": ["warn", "error", "info"],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
"no-debugger": "warn",
|
|
63
|
+
"no-var": "error",
|
|
64
|
+
"prefer-const": "error",
|
|
65
|
+
"eqeqeq": ["error", "always"],
|
|
66
|
+
"curly": ["error", "all"],
|
|
67
|
+
"brace-style": ["error", "1tbs"],
|
|
68
|
+
"space-before-function-paren": [
|
|
69
|
+
"error",
|
|
70
|
+
{
|
|
71
|
+
"anonymous": "always",
|
|
72
|
+
"named": "never",
|
|
73
|
+
"asyncArrow": "always",
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
"semi": ["error", "always"],
|
|
77
|
+
"quotes": ["error", "single", { "avoidEscape": true }],
|
|
78
|
+
"indent": ["error", 2],
|
|
79
|
+
|
|
80
|
+
// Import organization
|
|
81
|
+
"sort-imports": "off",
|
|
82
|
+
"import/order": [
|
|
83
|
+
"warn",
|
|
84
|
+
{
|
|
85
|
+
"groups": [
|
|
86
|
+
"builtin",
|
|
87
|
+
"external",
|
|
88
|
+
"internal",
|
|
89
|
+
"parent",
|
|
90
|
+
"sibling",
|
|
91
|
+
"index",
|
|
92
|
+
],
|
|
93
|
+
"pathGroups": [
|
|
94
|
+
{
|
|
95
|
+
"pattern": "react*",
|
|
96
|
+
"group": "external",
|
|
97
|
+
"position": "before",
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
"alphabeticalOrder": true,
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
|
|
104
|
+
// React Query best practices
|
|
105
|
+
"@tanstack/query/exhaustive-deps": "error",
|
|
106
|
+
"@tanstack/query/prefer-query-object-syntax": "warn",
|
|
107
|
+
},
|
|
108
|
+
"settings": {
|
|
109
|
+
"react": {
|
|
110
|
+
"version": "detect",
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
"overrides": [
|
|
114
|
+
{
|
|
115
|
+
"files": ["**/__tests__/**", "**/*.test.ts", "**/*.test.tsx"],
|
|
116
|
+
"env": {
|
|
117
|
+
"jest": true,
|
|
118
|
+
},
|
|
119
|
+
"rules": {
|
|
120
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
"files": ["**/*.d.ts"],
|
|
125
|
+
"rules": {
|
|
126
|
+
"@typescript-eslint/no-unused-vars": "off",
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
}
|
package/template/App.tsx
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
import { QueryClient, QueryClientProvider } from
|
|
2
|
-
import { StyleSheet
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import { ThemeProvider } from './src/hooks/ThemeContext';
|
|
11
|
-
const queryClient = new QueryClient()
|
|
12
|
-
function App() {
|
|
13
|
-
const isDarkMode = useColorScheme() === 'dark';
|
|
14
|
-
|
|
15
|
-
return (
|
|
16
|
-
<QueryClientProvider client={queryClient}>
|
|
17
|
-
<Provider store={store}>
|
|
18
|
-
<SafeAreaProvider>
|
|
19
|
-
<ThemeProvider>
|
|
20
|
-
<Main />
|
|
21
|
-
</ThemeProvider>
|
|
22
|
-
</SafeAreaProvider>
|
|
23
|
-
</Provider>
|
|
24
|
-
</QueryClientProvider>
|
|
25
|
-
);
|
|
26
|
-
}
|
|
1
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
2
|
+
import { StyleSheet } from "react-native";
|
|
3
|
+
import { SafeAreaProvider } from "react-native-safe-area-context";
|
|
4
|
+
import { Provider } from "react-redux";
|
|
5
|
+
import store, { persistor } from "./src/redux/store";
|
|
6
|
+
import { PersistGate } from "redux-persist/integration/react";
|
|
7
|
+
import Main from "./Main";
|
|
8
|
+
import { ThemeProvider } from "./src/hooks/ThemeContext";
|
|
9
|
+
import { ErrorBoundary } from "./src/components/ErrorBoundary";
|
|
27
10
|
|
|
28
|
-
|
|
29
|
-
|
|
11
|
+
const queryClient = new QueryClient({
|
|
12
|
+
defaultOptions: {
|
|
13
|
+
queries: {
|
|
14
|
+
retry: 1,
|
|
15
|
+
staleTime: 1000 * 60 * 5, // 5 minutes
|
|
16
|
+
gcTime: 1000 * 60 * 10, // 10 minutes (formerly cacheTime)
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|
|
30
20
|
|
|
21
|
+
function App() {
|
|
31
22
|
return (
|
|
32
|
-
<
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
<ErrorBoundary>
|
|
24
|
+
<QueryClientProvider client={queryClient}>
|
|
25
|
+
<Provider store={store}>
|
|
26
|
+
<PersistGate loading={null} persistor={persistor}>
|
|
27
|
+
<SafeAreaProvider>
|
|
28
|
+
<ThemeProvider>
|
|
29
|
+
<Main />
|
|
30
|
+
</ThemeProvider>
|
|
31
|
+
</SafeAreaProvider>
|
|
32
|
+
</PersistGate>
|
|
33
|
+
</Provider>
|
|
34
|
+
</QueryClientProvider>
|
|
35
|
+
</ErrorBoundary>
|
|
35
36
|
);
|
|
36
37
|
}
|
|
37
38
|
|
package/template/Main.tsx
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { NavigationContainer } from
|
|
2
|
-
import React from
|
|
3
|
-
import AppStack from
|
|
4
|
-
|
|
1
|
+
import { NavigationContainer } from "@react-navigation/native";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import AppStack from "./src/navigation/AppStack";
|
|
4
|
+
import { navigationRef } from "./src/helper/navigation-service";
|
|
5
5
|
|
|
6
6
|
const Main = () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
7
|
+
return (
|
|
8
|
+
<NavigationContainer ref={navigationRef}>
|
|
9
|
+
<AppStack />
|
|
10
|
+
</NavigationContainer>
|
|
11
|
+
);
|
|
12
|
+
};
|
|
13
13
|
|
|
14
|
-
export default Main
|
|
14
|
+
export default Main;
|
package/template/package.json
CHANGED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import React, { ReactNode, ErrorInfo } from "react";
|
|
2
|
+
import { View, Text, StyleSheet, TouchableOpacity } from "react-native";
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface State {
|
|
9
|
+
hasError: boolean;
|
|
10
|
+
error: Error | null;
|
|
11
|
+
errorInfo: ErrorInfo | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Error Boundary Component
|
|
16
|
+
* Catches JavaScript errors anywhere in the child component tree
|
|
17
|
+
* and displays a fallback UI instead of crashing the app
|
|
18
|
+
*/
|
|
19
|
+
export class ErrorBoundary extends React.Component<Props, State> {
|
|
20
|
+
constructor(props: Props) {
|
|
21
|
+
super(props);
|
|
22
|
+
this.state = {
|
|
23
|
+
hasError: false,
|
|
24
|
+
error: null,
|
|
25
|
+
errorInfo: null,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static getDerivedStateFromError(error: Error): Partial<State> {
|
|
30
|
+
return { hasError: true };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
34
|
+
// Log error details for debugging
|
|
35
|
+
console.error("ErrorBoundary caught an error:", error);
|
|
36
|
+
console.error("Error Info:", errorInfo);
|
|
37
|
+
|
|
38
|
+
this.setState({
|
|
39
|
+
error,
|
|
40
|
+
errorInfo,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
resetError = () => {
|
|
45
|
+
this.setState({
|
|
46
|
+
hasError: false,
|
|
47
|
+
error: null,
|
|
48
|
+
errorInfo: null,
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
render() {
|
|
53
|
+
if (this.state.hasError) {
|
|
54
|
+
return (
|
|
55
|
+
<View style={styles.container}>
|
|
56
|
+
<View style={styles.content}>
|
|
57
|
+
<Text style={styles.title}>⚠️ Something Went Wrong</Text>
|
|
58
|
+
<Text style={styles.message}>
|
|
59
|
+
The app encountered an unexpected error. Please try restarting.
|
|
60
|
+
</Text>
|
|
61
|
+
|
|
62
|
+
{__DEV__ && this.state.error && (
|
|
63
|
+
<View style={styles.errorDetails}>
|
|
64
|
+
<Text style={styles.errorTitle}>Error Details:</Text>
|
|
65
|
+
<Text style={styles.errorText}>
|
|
66
|
+
{this.state.error.toString()}
|
|
67
|
+
</Text>
|
|
68
|
+
{this.state.errorInfo && (
|
|
69
|
+
<Text style={styles.stackTrace}>
|
|
70
|
+
{this.state.errorInfo.componentStack}
|
|
71
|
+
</Text>
|
|
72
|
+
)}
|
|
73
|
+
</View>
|
|
74
|
+
)}
|
|
75
|
+
|
|
76
|
+
<TouchableOpacity style={styles.button} onPress={this.resetError}>
|
|
77
|
+
<Text style={styles.buttonText}>Try Again</Text>
|
|
78
|
+
</TouchableOpacity>
|
|
79
|
+
</View>
|
|
80
|
+
</View>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return this.props.children;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const styles = StyleSheet.create({
|
|
89
|
+
container: {
|
|
90
|
+
flex: 1,
|
|
91
|
+
backgroundColor: "#f5f5f5",
|
|
92
|
+
justifyContent: "center",
|
|
93
|
+
alignItems: "center",
|
|
94
|
+
padding: 20,
|
|
95
|
+
},
|
|
96
|
+
content: {
|
|
97
|
+
backgroundColor: "#ffffff",
|
|
98
|
+
borderRadius: 12,
|
|
99
|
+
padding: 24,
|
|
100
|
+
alignItems: "center",
|
|
101
|
+
shadowColor: "#000",
|
|
102
|
+
shadowOffset: { width: 0, height: 2 },
|
|
103
|
+
shadowOpacity: 0.1,
|
|
104
|
+
shadowRadius: 8,
|
|
105
|
+
elevation: 5,
|
|
106
|
+
},
|
|
107
|
+
title: {
|
|
108
|
+
fontSize: 20,
|
|
109
|
+
fontWeight: "bold",
|
|
110
|
+
color: "#333333",
|
|
111
|
+
marginBottom: 12,
|
|
112
|
+
textAlign: "center",
|
|
113
|
+
},
|
|
114
|
+
message: {
|
|
115
|
+
fontSize: 16,
|
|
116
|
+
color: "#666666",
|
|
117
|
+
textAlign: "center",
|
|
118
|
+
marginBottom: 20,
|
|
119
|
+
lineHeight: 22,
|
|
120
|
+
},
|
|
121
|
+
errorDetails: {
|
|
122
|
+
backgroundColor: "#ffe6e6",
|
|
123
|
+
borderRadius: 8,
|
|
124
|
+
padding: 12,
|
|
125
|
+
marginBottom: 20,
|
|
126
|
+
maxHeight: 200,
|
|
127
|
+
},
|
|
128
|
+
errorTitle: {
|
|
129
|
+
fontWeight: "600",
|
|
130
|
+
color: "#d32f2f",
|
|
131
|
+
marginBottom: 8,
|
|
132
|
+
},
|
|
133
|
+
errorText: {
|
|
134
|
+
color: "#c62828",
|
|
135
|
+
fontSize: 12,
|
|
136
|
+
marginBottom: 8,
|
|
137
|
+
},
|
|
138
|
+
stackTrace: {
|
|
139
|
+
color: "#c62828",
|
|
140
|
+
fontSize: 10,
|
|
141
|
+
fontFamily: "Courier New",
|
|
142
|
+
},
|
|
143
|
+
button: {
|
|
144
|
+
backgroundColor: "#4045EF",
|
|
145
|
+
paddingHorizontal: 32,
|
|
146
|
+
paddingVertical: 12,
|
|
147
|
+
borderRadius: 8,
|
|
148
|
+
},
|
|
149
|
+
buttonText: {
|
|
150
|
+
color: "#ffffff",
|
|
151
|
+
fontSize: 16,
|
|
152
|
+
fontWeight: "600",
|
|
153
|
+
},
|
|
154
|
+
});
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { StackNavigationOptions } from "@react-navigation/stack";
|
|
2
2
|
import HomeScreen from "../screens/HomeScreen";
|
|
3
3
|
import SplashScreen from "../screens/SplashScreen";
|
|
4
|
+
import LoginScreen from "../screens/LoginScreen";
|
|
4
5
|
import { AppStackParams } from "./AppStackParams";
|
|
5
6
|
|
|
6
7
|
export const screens: Array<{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
name: keyof AppStackParams;
|
|
9
|
+
component: React.ComponentType<any>;
|
|
10
|
+
options?: StackNavigationOptions;
|
|
10
11
|
}> = [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
{ name: "SplashScreen", component: SplashScreen },
|
|
13
|
+
{ name: "LoginScreen", component: LoginScreen },
|
|
14
|
+
{ name: "HomeScreen", component: HomeScreen },
|
|
15
|
+
// Add your other screens here
|
|
16
|
+
];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
|
2
|
+
|
|
3
|
+
type User = { [key: string]: any } | null;
|
|
4
|
+
|
|
5
|
+
interface AuthState {
|
|
6
|
+
user: User;
|
|
7
|
+
accessToken: string | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const initialState: AuthState = {
|
|
11
|
+
user: null,
|
|
12
|
+
accessToken: null,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const authSlice = createSlice({
|
|
16
|
+
name: "auth",
|
|
17
|
+
initialState,
|
|
18
|
+
reducers: {
|
|
19
|
+
setCredentials(
|
|
20
|
+
state,
|
|
21
|
+
action: PayloadAction<{ user: User; accessToken: string }>,
|
|
22
|
+
) {
|
|
23
|
+
state.user = action.payload.user;
|
|
24
|
+
state.accessToken = action.payload.accessToken;
|
|
25
|
+
},
|
|
26
|
+
setUser(state, action: PayloadAction<User>) {
|
|
27
|
+
state.user = action.payload;
|
|
28
|
+
},
|
|
29
|
+
setAccessToken(state, action: PayloadAction<string | null>) {
|
|
30
|
+
state.accessToken = action.payload;
|
|
31
|
+
},
|
|
32
|
+
clearAuth(state) {
|
|
33
|
+
state.user = null;
|
|
34
|
+
state.accessToken = null;
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const { setCredentials, setUser, setAccessToken, clearAuth } =
|
|
40
|
+
authSlice.actions;
|
|
41
|
+
|
|
42
|
+
export const selectCurrentUser = (state: any) => state.auth?.user;
|
|
43
|
+
export const selectAccessToken = (state: any) => state.auth?.accessToken;
|
|
44
|
+
|
|
45
|
+
export default authSlice.reducer;
|
|
@@ -1,14 +1,31 @@
|
|
|
1
|
-
import { configureStore } from
|
|
2
|
-
import { TypedUseSelectorHook, useDispatch, useSelector } from
|
|
3
|
-
import
|
|
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 AsyncStorage from "@react-native-async-storage/async-storage";
|
|
7
|
+
const persistConfig = {
|
|
8
|
+
key: "root",
|
|
9
|
+
storage: AsyncStorage,
|
|
10
|
+
whitelist: ["auth"],
|
|
11
|
+
};
|
|
4
12
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
const rootReducer = combineReducers({
|
|
14
|
+
global: reducer,
|
|
15
|
+
auth: authReducer,
|
|
16
|
+
});
|
|
17
|
+
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
|
18
|
+
export const store = configureStore({
|
|
19
|
+
reducer: persistedReducer,
|
|
20
|
+
middleware: (getDefaultMiddleware) =>
|
|
21
|
+
getDefaultMiddleware({
|
|
22
|
+
serializableCheck: false,
|
|
23
|
+
}),
|
|
9
24
|
});
|
|
10
25
|
export default store;
|
|
11
26
|
|
|
27
|
+
export const persistor = persistStore(store);
|
|
28
|
+
|
|
12
29
|
export type RootStore = ReturnType<typeof store.getState>;
|
|
13
30
|
export type AppDispatch = typeof store.dispatch;
|
|
14
31
|
|
|
@@ -1 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* API Error Response Type
|
|
3
|
+
*/
|
|
4
|
+
export interface ApiErrorResponse {
|
|
5
|
+
message: string;
|
|
6
|
+
statusCode?: number;
|
|
7
|
+
code?: string;
|
|
8
|
+
details?: Record<string, any>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* API Success Response Type
|
|
13
|
+
*/
|
|
14
|
+
export interface ApiSuccessResponse<T> {
|
|
15
|
+
data: T;
|
|
16
|
+
message?: string;
|
|
17
|
+
statusCode?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Todo Item Type
|
|
22
|
+
*/
|
|
23
|
+
export interface TodoItem {
|
|
24
|
+
id: number;
|
|
25
|
+
title: string;
|
|
26
|
+
completed: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Pagination Type
|
|
31
|
+
*/
|
|
32
|
+
export interface PaginationOptions {
|
|
33
|
+
page: number;
|
|
34
|
+
limit: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Generic API Response
|
|
39
|
+
*/
|
|
40
|
+
export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse;
|
|
@@ -0,0 +1,105 @@
|
|
|
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";
|
|
16
|
+
|
|
17
|
+
type Props = {
|
|
18
|
+
navigation: NativeStackNavigationProp<AppStackParams, "LoginScreen">;
|
|
19
|
+
route: RouteProp<AppStackParams, "LoginScreen">;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const LoginScreen = ({ navigation }: Props) => {
|
|
23
|
+
const { theme } = useTheme();
|
|
24
|
+
const dispatch = useAppDispatch();
|
|
25
|
+
const [email, setEmail] = useState("");
|
|
26
|
+
const [password, setPassword] = useState("");
|
|
27
|
+
|
|
28
|
+
const onLogin = () => {
|
|
29
|
+
// store dummy data in auth reducer
|
|
30
|
+
dispatch(
|
|
31
|
+
setCredentials({
|
|
32
|
+
user: { id: "1", name: "Demo User", email: email || "demo@local" },
|
|
33
|
+
accessToken: "dummy-access-token-123",
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
navigation.reset({ index: 0, routes: [{ name: "HomeScreen" }] });
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
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
|
+
/>
|
|
66
|
+
|
|
67
|
+
<TouchableOpacity
|
|
68
|
+
style={[styles.button, { backgroundColor: theme.primary }]}
|
|
69
|
+
onPress={onLogin}
|
|
70
|
+
>
|
|
71
|
+
<Text style={{ color: theme.text }}>Sign in</Text>
|
|
72
|
+
</TouchableOpacity>
|
|
73
|
+
</View>
|
|
74
|
+
</SafeAreaView>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default LoginScreen;
|
|
79
|
+
|
|
80
|
+
const styles = StyleSheet.create({
|
|
81
|
+
container: {
|
|
82
|
+
flex: 1,
|
|
83
|
+
justifyContent: "center",
|
|
84
|
+
padding: 24,
|
|
85
|
+
},
|
|
86
|
+
title: {
|
|
87
|
+
fontSize: 28,
|
|
88
|
+
fontWeight: "600",
|
|
89
|
+
marginBottom: 24,
|
|
90
|
+
textAlign: "center",
|
|
91
|
+
},
|
|
92
|
+
input: {
|
|
93
|
+
height: 48,
|
|
94
|
+
borderRadius: 8,
|
|
95
|
+
paddingHorizontal: 12,
|
|
96
|
+
marginBottom: 12,
|
|
97
|
+
},
|
|
98
|
+
button: {
|
|
99
|
+
height: 48,
|
|
100
|
+
borderRadius: 8,
|
|
101
|
+
alignItems: "center",
|
|
102
|
+
justifyContent: "center",
|
|
103
|
+
marginTop: 12,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
@@ -1,33 +1,42 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
import { Text, View, StyleSheet } from
|
|
3
|
-
import { NativeStackNavigationProp } from
|
|
4
|
-
import { RouteProp } from
|
|
5
|
-
import { AppStackParams } from
|
|
6
|
-
import { useEffect } from
|
|
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";
|
|
8
|
+
|
|
9
|
+
import { selectAccessToken } from "../redux/slices/authSlice";
|
|
7
10
|
type Props = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
navigation: NativeStackNavigationProp<
|
|
12
|
+
AppStackParams,
|
|
13
|
+
"SplashScreen",
|
|
14
|
+
"SplashScreen"
|
|
15
|
+
>;
|
|
16
|
+
route: RouteProp<AppStackParams, "SplashScreen">;
|
|
14
17
|
};
|
|
15
18
|
|
|
16
19
|
const SplashScreen = ({ navigation }: Props) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
);
|
|
20
|
+
const accessToken = useAppSelector(selectAccessToken);
|
|
21
|
+
|
|
22
|
+
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]);
|
|
31
|
+
return (
|
|
32
|
+
<View style={styles.container}>
|
|
33
|
+
<Text>SplashScreen</Text>
|
|
34
|
+
</View>
|
|
35
|
+
);
|
|
27
36
|
};
|
|
28
37
|
|
|
29
38
|
export default SplashScreen;
|
|
30
39
|
|
|
31
40
|
const styles = StyleSheet.create({
|
|
32
|
-
|
|
41
|
+
container: {},
|
|
33
42
|
});
|