From ce0f7c457653a5eca8e9111e6edce48edc8906ef Mon Sep 17 00:00:00 2001 From: Ahmad Izzudin Alifyandra Date: Sun, 3 Oct 2021 13:43:08 +0700 Subject: [PATCH 1/2] chore: add some redux dispatch handling in screens --- src/redux/user/actions.tsx | 15 +++++++-------- src/screens/auth/LoginScreen.tsx | 9 +++++++-- src/screens/profile/ChangePasswordScreen.tsx | 6 ++++-- src/types/redux.ts | 3 +++ 4 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 src/types/redux.ts diff --git a/src/redux/user/actions.tsx b/src/redux/user/actions.tsx index 16e1eee..7e232c4 100644 --- a/src/redux/user/actions.tsx +++ b/src/redux/user/actions.tsx @@ -2,6 +2,8 @@ import { Dispatch } from "react-redux/node_modules/@types/react"; import * as userService from "../../service/firestore/user"; import { IUser } from "../../types/firestore/User"; import * as authService from "../../service/firebase/auth"; +import { ActionErrorRes } from "../../types/redux"; +import { AnyAction } from "redux"; export const getUser = (uid: string) => { return async (dispatch: Dispatch) => { @@ -32,17 +34,13 @@ export const setProfilePic = (url: string, id: string) => { export const loginUser = (email: string, password: string) => { return async (dispatch: Dispatch) => { - try { - const credential = authService.createCredential(email, password); + const credential = authService.createCredential(email, password); - const userCredential = await authService.loginUser(credential); + const userCredential = await authService.loginUser(credential); - const uid = userCredential.user.uid; + const uid = userCredential.user.uid; - return dispatch(getUser(uid)); - } catch (e) { - console.log(e); - } + return dispatch(getUser(uid)); }; }; @@ -68,6 +66,7 @@ export const changePassword = (oldPassword: string, newPassword: string) => { return dispatch({ type: "CHANGE_PASSWORD" }); } catch (e) { console.log(e); + return { error: e }; } }; }; diff --git a/src/screens/auth/LoginScreen.tsx b/src/screens/auth/LoginScreen.tsx index b2217c9..450eca6 100644 --- a/src/screens/auth/LoginScreen.tsx +++ b/src/screens/auth/LoginScreen.tsx @@ -1,7 +1,7 @@ import { useNavigation } from "@react-navigation/core"; import * as React from "react"; import { useState } from "react"; -import { StyleSheet } from "react-native"; +import { Alert, StyleSheet } from "react-native"; import MainButton from "../../components/button/MainButton"; import PlainForm from "../../components/Forms/PlainForm"; import IconForm from "../../components/Forms/IconForm"; @@ -11,6 +11,8 @@ import { RootTabScreenProps } from "../../types/navigation"; import Spacer from "../../components/Spacer/Spacer"; import { useAppDispatch } from "../../hooks/reduxHooks"; import { loginUser } from "../../redux/user/actions"; +import { IUser } from "../../types/firestore/User"; +import { ActionErrorRes } from "../../types/redux"; const LoginScreen = ({ navigation }: RootTabScreenProps<"TabOne">) => { const nav = useNavigation(); @@ -19,7 +21,10 @@ const LoginScreen = ({ navigation }: RootTabScreenProps<"TabOne">) => { const dispatch = useAppDispatch(); const handleSubmit = () => { - dispatch(loginUser(email, password)); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + dispatch(loginUser(email, password)) + .then((e: IUser) => nav) + .catch((e: ActionErrorRes) => Alert.alert("Invalid credentials")); }; return ( diff --git a/src/screens/profile/ChangePasswordScreen.tsx b/src/screens/profile/ChangePasswordScreen.tsx index 48a26c4..c13cc0a 100644 --- a/src/screens/profile/ChangePasswordScreen.tsx +++ b/src/screens/profile/ChangePasswordScreen.tsx @@ -9,6 +9,7 @@ import Colors from "../../constants/Colors"; import { useAppDispatch } from "../../hooks/reduxHooks"; import { RootTabScreenProps } from "../../types/navigation"; import { changePassword } from "../../redux/user/actions"; +import { ActionErrorRes } from "../../types/redux"; const ChangePasswordScreen = ({ navigation, @@ -23,8 +24,9 @@ const ChangePasswordScreen = ({ const handleSubmit = () => { Keyboard.dismiss(); // eslint-disable-next-line @typescript-eslint/no-unsafe-call - dispatch(changePassword(oldPassword, newPassword)).then(() => - nav.navigate("SuccessChangePassword") + dispatch(changePassword(oldPassword, newPassword)).then( + (res: ActionErrorRes) => + !res.error && nav.navigate("SuccessChangePassword") ); }; diff --git a/src/types/redux.ts b/src/types/redux.ts new file mode 100644 index 0000000..4487054 --- /dev/null +++ b/src/types/redux.ts @@ -0,0 +1,3 @@ +export type ActionErrorRes = { + error: any; +}; -- GitLab From 8c72202c50461391ac11be4e55b59ebc8708ffb8 Mon Sep 17 00:00:00 2001 From: Ahmad Izzudin Alifyandra Date: Sun, 3 Oct 2021 13:43:46 +0700 Subject: [PATCH 2/2] fix: redux pic url dispatch assignment bug --- src/redux/user/reducer.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/redux/user/reducer.tsx b/src/redux/user/reducer.tsx index 6b2b4a7..43e78af 100644 --- a/src/redux/user/reducer.tsx +++ b/src/redux/user/reducer.tsx @@ -18,8 +18,10 @@ const userReducer = (state = initialState, action: AnyAction): IUser => { return action.payload as IUser; case "LOGOUT": return initialState; - case "SET_PIC": - return { ...state, pic: action.payload } as IUser; + case "SET_PIC": { + const payload = action.payload as { pic: string }; + return { ...state, pic: payload.pic } as IUser; + } case "CHANGE_PASSWORD": return state; case "SIGNUP": -- GitLab