Fakultas Ilmu Komputer UI

Commit 390be7dc authored by Bagus Prabowo's avatar Bagus Prabowo
Browse files

Merge branch 'redux-handling' into 'master'

Add some redux handling & image upload bugfix

See merge request !61
parents a0d86ab9 762df2c4
Pipeline #85917 passed with stage
in 7 minutes and 59 seconds
......@@ -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<any>) => {
......@@ -32,17 +34,13 @@ export const setProfilePic = (url: string, id: string) => {
export const loginUser = (email: string, password: string) => {
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));
} 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 };
}
};
};
......
......@@ -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":
......
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 (
......
......@@ -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")
);
};
......
export type ActionErrorRes = {
error: any;
};
Markdown is supported
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