Fakultas Ilmu Komputer UI

Commit 49ab697b authored by Ahmad Izzudin Alifyandra's avatar Ahmad Izzudin Alifyandra
Browse files

feat: add set user pic action

parent eef41091
......@@ -44,7 +44,8 @@
"react-native-screens": "~3.4.0",
"react-native-web": "~0.13.12",
"react-redux": "^7.2.5",
"redux": "^4.1.1"
"redux": "^4.1.1",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"@babel/core": "^7.9.0",
......
import { combineReducers, createStore } from "redux";
import { combineReducers, createStore, applyMiddleware } from "redux";
import userReducer from "./user/reducer";
import thunkMiddleware from "redux-thunk";
const rootReducer = combineReducers({
user: userReducer,
});
const store = createStore(rootReducer);
const middleware = applyMiddleware(thunkMiddleware);
const store = createStore(rootReducer, middleware);
export type RootState = ReturnType<typeof rootReducer>;
......
import firebase from "firebase/app";
import { Dispatch } from "react-redux/node_modules/@types/react";
import * as userService from "../../service/firestore/user";
import { IUser } from "../../types/firebase/User";
const db = firebase.firestore();
export const getUser = (uid) => {
export const getUser = (uid: string) => {
return async (dispatch: Dispatch<any>) => {
try {
const user = await db.collection("users").doc(uid).get();
const user = await userService.getUser(uid);
dispatch({ type: "LOGIN", payload: { ...user.data(), id: user.id } });
} catch (e) {
......@@ -15,6 +15,18 @@ export const getUser = (uid) => {
};
};
export const setProfilePic = (url: string, id: string) => {
return async (dispatch: Dispatch<any>, getState: IUser) => {
try {
await userService.addProfilePic(url, id);
dispatch({ type: "SET_PIC", payload: { ...getState, pic: url } });
} catch (e) {
console.log(e);
}
};
};
//TODO add loginUser
//TODO add logoutUser
......
......@@ -3,7 +3,7 @@ import { IUser } from "../../types/firebase/User";
const initialState: IUser = {
id: "",
firstName: "",
firstName: "james",
lastName: "",
phone: "",
email: "",
......@@ -16,6 +16,8 @@ const userReducer = (state = initialState, action: AnyAction): IUser => {
return action.payload as IUser;
case "LOGOUT":
return initialState;
case "SET_PIC":
return action.payload as IUser;
default:
return state;
}
......
import firebase from "firebase/app";
import { useSelector } from "react-redux";
import { RootState } from "../../redux/store";
import userReducer from "../../redux/user/reducer";
import { IUser } from "../../types/firebase/User";
export const addProfilePic = async (url: string, userId: string) => {
const db = firebase.firestore();
return await db.collection("users").doc(userId).update({
pic: url,
});
};
export const getUser = async (id) => {
const db = firebase.firestore();
return await db.collection("users").doc(id).get();
};
......@@ -9324,6 +9324,11 @@ realpath-native@^2.0.0:
resolved "https://registry.yarnpkg.com/realpath-native/-/realpath-native-2.0.0.tgz"
integrity sha512-v1SEYUOXXdbBZK8ZuNgO4TBjamPsiSgcFr0aP+tEKpQZK8vooEUqV6nm6Cv502mX4NF2EfsnVqtNAHG+/6Ur1Q==
redux-thunk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"
integrity sha512-km6dclyFnmcvxhAcrQV2AkZmPQjzPDjgVlQtR0EQjxZPyJ0BnMf3in1ryuR8A2qU0HldVRfxYXbFSKlI3N7Slw==
redux@^4.0.0, redux@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.1.1.tgz#76f1c439bb42043f985fbd9bf21990e60bd67f47"
......
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