Fakultas Ilmu Komputer UI

Commit 762df2c4 authored by Ahmad Izzudin Alifyandra's avatar Ahmad Izzudin Alifyandra Committed by Bagus Prabowo
Browse files

Add some redux handling & image upload bugfix

parent f2776161
...@@ -2,6 +2,8 @@ import { Dispatch } from "react-redux/node_modules/@types/react"; ...@@ -2,6 +2,8 @@ import { Dispatch } from "react-redux/node_modules/@types/react";
import * as userService from "../../service/firestore/user"; import * as userService from "../../service/firestore/user";
import { IUser } from "../../types/firestore/User"; import { IUser } from "../../types/firestore/User";
import * as authService from "../../service/firebase/auth"; import * as authService from "../../service/firebase/auth";
import { ActionErrorRes } from "../../types/redux";
import { AnyAction } from "redux";
export const getUser = (uid: string) => { export const getUser = (uid: string) => {
return async (dispatch: Dispatch<any>) => { return async (dispatch: Dispatch<any>) => {
...@@ -32,17 +34,13 @@ export const setProfilePic = (url: string, id: string) => { ...@@ -32,17 +34,13 @@ export const setProfilePic = (url: string, id: string) => {
export const loginUser = (email: string, password: string) => { export const loginUser = (email: string, password: string) => {
return async (dispatch: Dispatch<any>) => { return async (dispatch: Dispatch<any>) => {
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)); return dispatch(getUser(uid));
} catch (e) {
console.log(e);
}
}; };
}; };
...@@ -68,6 +66,7 @@ export const changePassword = (oldPassword: string, newPassword: string) => { ...@@ -68,6 +66,7 @@ export const changePassword = (oldPassword: string, newPassword: string) => {
return dispatch({ type: "CHANGE_PASSWORD" }); return dispatch({ type: "CHANGE_PASSWORD" });
} catch (e) { } catch (e) {
console.log(e); console.log(e);
return { error: e };
} }
}; };
}; };
......
...@@ -18,8 +18,10 @@ const userReducer = (state = initialState, action: AnyAction): IUser => { ...@@ -18,8 +18,10 @@ const userReducer = (state = initialState, action: AnyAction): IUser => {
return action.payload as IUser; return action.payload as IUser;
case "LOGOUT": case "LOGOUT":
return initialState; return initialState;
case "SET_PIC": case "SET_PIC": {
return { ...state, pic: action.payload } as IUser; const payload = action.payload as { pic: string };
return { ...state, pic: payload.pic } as IUser;
}
case "CHANGE_PASSWORD": case "CHANGE_PASSWORD":
return state; return state;
case "SIGNUP": case "SIGNUP":
......
import { useNavigation } from "@react-navigation/core"; import { useNavigation } from "@react-navigation/core";
import * as React from "react"; import * as React from "react";
import { useState } from "react"; import { useState } from "react";
import { StyleSheet } from "react-native"; import { Alert, StyleSheet } from "react-native";
import MainButton from "../../components/button/MainButton"; import MainButton from "../../components/button/MainButton";
import PlainForm from "../../components/Forms/PlainForm"; import PlainForm from "../../components/Forms/PlainForm";
import IconForm from "../../components/Forms/IconForm"; import IconForm from "../../components/Forms/IconForm";
...@@ -11,6 +11,8 @@ import { RootTabScreenProps } from "../../types/navigation"; ...@@ -11,6 +11,8 @@ import { RootTabScreenProps } from "../../types/navigation";
import Spacer from "../../components/Spacer/Spacer"; import Spacer from "../../components/Spacer/Spacer";
import { useAppDispatch } from "../../hooks/reduxHooks"; import { useAppDispatch } from "../../hooks/reduxHooks";
import { loginUser } from "../../redux/user/actions"; import { loginUser } from "../../redux/user/actions";
import { IUser } from "../../types/firestore/User";
import { ActionErrorRes } from "../../types/redux";
const LoginScreen = ({ navigation }: RootTabScreenProps<"TabOne">) => { const LoginScreen = ({ navigation }: RootTabScreenProps<"TabOne">) => {
const nav = useNavigation(); const nav = useNavigation();
...@@ -19,7 +21,10 @@ const LoginScreen = ({ navigation }: RootTabScreenProps<"TabOne">) => { ...@@ -19,7 +21,10 @@ const LoginScreen = ({ navigation }: RootTabScreenProps<"TabOne">) => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const handleSubmit = () => { 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 ( return (
......
...@@ -9,6 +9,7 @@ import Colors from "../../constants/Colors"; ...@@ -9,6 +9,7 @@ import Colors from "../../constants/Colors";
import { useAppDispatch } from "../../hooks/reduxHooks"; import { useAppDispatch } from "../../hooks/reduxHooks";
import { RootTabScreenProps } from "../../types/navigation"; import { RootTabScreenProps } from "../../types/navigation";
import { changePassword } from "../../redux/user/actions"; import { changePassword } from "../../redux/user/actions";
import { ActionErrorRes } from "../../types/redux";
const ChangePasswordScreen = ({ const ChangePasswordScreen = ({
navigation, navigation,
...@@ -23,8 +24,9 @@ const ChangePasswordScreen = ({ ...@@ -23,8 +24,9 @@ const ChangePasswordScreen = ({
const handleSubmit = () => { const handleSubmit = () => {
Keyboard.dismiss(); Keyboard.dismiss();
// eslint-disable-next-line @typescript-eslint/no-unsafe-call // eslint-disable-next-line @typescript-eslint/no-unsafe-call
dispatch(changePassword(oldPassword, newPassword)).then(() => dispatch(changePassword(oldPassword, newPassword)).then(
nav.navigate("SuccessChangePassword") (res: ActionErrorRes) =>
!res.error && nav.navigate("SuccessChangePassword")
); );
}; };
......
export type ActionErrorRes = {
error: any;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment