Fakultas Ilmu Komputer UI

actions.tsx 2.42 KB
Newer Older
1
import { Dispatch } from "react-redux/node_modules/@types/react";
2
import * as userService from "../../service/firestore/user";
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
3
import * as authService from "../../service/firebase/auth";
4

5
export const getUser = (uid: string) => {
6
7
  return async (dispatch: Dispatch<any>) => {
    try {
8
      const user = await userService.getUser(uid);
9

10
11
12
13
      return dispatch({
        type: "LOGIN",
        payload: { ...user.data(), id: user.id },
      });
14
15
16
17
18
19
    } catch (e) {
      console.log(e);
    }
  };
};

20
export const setProfilePic = (url: string, id: string) => {
21
  return async (dispatch: Dispatch<any>) => {
22
    await userService.addProfilePic(url, id);
23

24
    return dispatch({ type: "SET_PIC", payload: { pic: url } });
25
26
27
  };
};

Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
28
29
export const loginUser = (email: string, password: string) => {
  return async (dispatch: Dispatch<any>) => {
30
    const credential = authService.createCredential(email, password);
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
31

32
    const userCredential = await authService.loginUser(credential);
33

34
    const uid = userCredential.user.uid;
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
35

36
    return dispatch(getUser(uid));
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
37
38
39
40
41
42
43
44
45
46
47
48
49
  };
};

export const logoutUser = () => {
  return async (dispatch: Dispatch<any>) => {
    try {
      await authService.logoutUser();
      dispatch({ type: "LOGOUT" });
    } catch (e) {
      console.log(e);
    }
  };
};
50

Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
51
52
53
54
55
56
57
58
export const changePassword = (oldPassword: string, newPassword: string) => {
  return async (dispatch: Dispatch<any>) => {
    try {
      const email = authService.getUserEmail();
      const credential = authService.createCredential(email, oldPassword);
      await authService.reauthenticateUser(credential);
      await authService.changePassword(newPassword);

59
      return dispatch({ type: "CHANGE_PASSWORD" });
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
60
61
    } catch (e) {
      console.log(e);
62
      return { error: e };
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
63
64
65
66
67
68
69
70
    }
  };
};

export const signupUser = (
  firstName: string,
  lastName: string,
  email: string,
Ahmad Izzudin Alifyandra's avatar
Ahmad Izzudin Alifyandra committed
71
72
73
  password: string,
  phone: string,
  businessType: string
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
74
75
76
77
78
) => {
  return async (dispatch: Dispatch<any>) => {
    try {
      const userAuth = await authService.signUpAuth(email, password);
      const uid = userAuth.user.uid;
Ahmad Izzudin Alifyandra's avatar
Ahmad Izzudin Alifyandra committed
79
80
81
82
83
84
85
86
87
      await userService.createUser(uid, {
        firstName,
        lastName,
        email,
        phone,
        businessType,
        pic: "",
      });
      return dispatch({
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
88
        type: "SIGNUP",
Ahmad Izzudin Alifyandra's avatar
Ahmad Izzudin Alifyandra committed
89
        payload: { email, firstName, lastName, id: uid, phone, businessType },
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
90
91
92
      });
    } catch (e) {
      console.log(e);
Ahmad Izzudin Alifyandra's avatar
Ahmad Izzudin Alifyandra committed
93
      return { error: e };
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
94
95
96
    }
  };
};