From eed7c411db04cbac9ea1da505383fcbad70bbb6d Mon Sep 17 00:00:00 2001 From: Szymon Koper <2790570+szymonkoper@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:32:47 +0200 Subject: [PATCH 01/13] Installs TanStack Query --- template/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/template/package.json b/template/package.json index b6e347d..878d925 100644 --- a/template/package.json +++ b/template/package.json @@ -24,6 +24,7 @@ "@react-navigation/native": "^6.0.10", "@react-navigation/native-stack": "^6.6.2", "@reduxjs/toolkit": "^1.6.1", + "@tanstack/react-query": "5.0.0-beta.15", "dayjs": "^1.10.6", "i18next": "^20.3.5", "react": "18.2.0", From 4601391154162b90eba16f1c649fc86bcd9f3380 Mon Sep 17 00:00:00 2001 From: Szymon Koper <2790570+szymonkoper@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:35:26 +0200 Subject: [PATCH 02/13] Configures @remote alias --- template/.prettierrc.js | 1 + template/README.md | 1 + template/babel.config.js | 1 + template/tsconfig.json | 1 + 4 files changed, 4 insertions(+) diff --git a/template/.prettierrc.js b/template/.prettierrc.js index bd329d6..065698b 100644 --- a/template/.prettierrc.js +++ b/template/.prettierrc.js @@ -16,6 +16,7 @@ module.exports = { '^@localization/(.*)$', '^@navigation/(.*)$', '^@redux/(.*)$', + '^@remote/(.*)$', '^@screens/(.*)$', '^@utils/(.*)$', '^[./]', diff --git a/template/README.md b/template/README.md index e42c651..ed1e2b3 100644 --- a/template/README.md +++ b/template/README.md @@ -30,6 +30,7 @@ This app has been generated using [react-native-template-redbeard](https://githu - `localization/` - Things related to user locale - `navigation/` - Navigators, routes - `redux/` - Actions, reducers, sagas +- `remote/` - Remote state (via TanStack Query) - `screens/` - App screens - `utils/` - Universal helpers diff --git a/template/babel.config.js b/template/babel.config.js index f165bc9..c87b96f 100644 --- a/template/babel.config.js +++ b/template/babel.config.js @@ -31,6 +31,7 @@ module.exports = { '@localization': './src/localization', '@navigation': './src/navigation', '@redux': './src/redux', + '@remote': './src/remote', '@screens': './src/screens', '@utils': './src/utils', }, diff --git a/template/tsconfig.json b/template/tsconfig.json index a836892..c632317 100644 --- a/template/tsconfig.json +++ b/template/tsconfig.json @@ -48,6 +48,7 @@ "@localization/*": ["./localization/*"], "@navigation/*": ["./navigation/*"], "@redux/*": ["./redux/*"], + "@remote/*": ["./remote/*"], "@screens/*": ["./screens/*"], "@utils/*": ["./utils/*"] }, From e49848846914680faffa2dc98858074055e35153 Mon Sep 17 00:00:00 2001 From: Szymon Koper <2790570+szymonkoper@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:35:55 +0200 Subject: [PATCH 03/13] Migrates logIn to query --- template/src/App.tsx | 19 ++++++++++++------- template/src/remote/auth.ts | 16 ++++++++++++++++ template/src/screens/LoginScreen.tsx | 12 ++++-------- 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 template/src/remote/auth.ts diff --git a/template/src/App.tsx b/template/src/App.tsx index c93c04f..e295174 100644 --- a/template/src/App.tsx +++ b/template/src/App.tsx @@ -1,4 +1,5 @@ import { NavigationContainer } from '@react-navigation/native' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import StoreProvider from 'providers/StoreProvider' import React from 'react' import 'react-native-gesture-handler' @@ -7,15 +8,19 @@ import SplashScreen from 'react-native-splash-screen' import '@localization/i18n' import RootStackNavigator from '@navigation/navigators/RootStackNavigator' +const queryClient = new QueryClient() + const App = () => { return ( - - - - - - - + + + + + + + + + ) } diff --git a/template/src/remote/auth.ts b/template/src/remote/auth.ts new file mode 100644 index 0000000..b73a530 --- /dev/null +++ b/template/src/remote/auth.ts @@ -0,0 +1,16 @@ +import { useMutation } from '@tanstack/react-query' +import { logIn } from '@api/auth' +import { logInAsyncSuccess } from '@api/authSlice' +import useAppDispatch from '@hooks/useAppDispatch' + +export const useLogInMutation = () => { + const dispatch = useAppDispatch() + + return useMutation({ + mutationKey: ['auth', 'logIn'], + mutationFn: logIn, + onSuccess: tokens => { + dispatch(logInAsyncSuccess(tokens)) + }, + }) +} diff --git a/template/src/screens/LoginScreen.tsx b/template/src/screens/LoginScreen.tsx index b3235bd..b55b999 100644 --- a/template/src/screens/LoginScreen.tsx +++ b/template/src/screens/LoginScreen.tsx @@ -1,15 +1,12 @@ import React from 'react' import { useTranslation } from 'react-i18next' import { ActivityIndicator, Button, StyleSheet } from 'react-native' -import { logInAsync, selectAuthTokens } from '@api/authSlice' import DemoTextInput from '@components/inputs/DemoTextInput' import MainScreenLayout from '@components/layouts/MainScreenLayout' import DemoCard from '@components/surfaces/DemoCard' -import useAppDispatch from '@hooks/useAppDispatch' -import useAppSelector from '@hooks/useAppSelector' import { RootStackScreenProps } from '@navigation/navigators/RootStackNavigator' import Routes from '@navigation/routes' -import { isLoading } from '@utils/api' +import { useLogInMutation } from '@remote/auth' const username = 'FAKE_USERNAME' const password = 'FAKE_PASSWORD' @@ -17,20 +14,19 @@ const password = 'FAKE_PASSWORD' export type LoginScreenParams = undefined const LoginScreen: React.FC> = () => { const { t } = useTranslation() - const dispatch = useAppDispatch() - const authTokensRequest = useAppSelector(selectAuthTokens) + const { isPending, mutate } = useLogInMutation() return ( - {isLoading(authTokensRequest) ? ( + {isPending ? ( ) : (