follow users
This commit is contained in:
@@ -6,6 +6,7 @@ import { createContext, useContext, useGlobal } from "reactn";
|
||||
import { useState } from "react";
|
||||
import { checkIfEmailIsValid } from "../actions/signupActions";
|
||||
import firebase, {
|
||||
followsRef,
|
||||
playlistsRef,
|
||||
projectsRef,
|
||||
usersRef,
|
||||
@@ -26,7 +27,6 @@ export default ({ children }) => {
|
||||
const [userProjects, setUserProjects] = useState([]);
|
||||
const [userLikedProjects, setUserLikedProjects] = useState([]);
|
||||
const [userPlaylists, setUserPlaylists] = useState([]);
|
||||
|
||||
useDataFromRef({
|
||||
ref: currentUID ? usersRef.doc(currentUID) : null,
|
||||
simpleRef: true,
|
||||
@@ -86,6 +86,50 @@ export default ({ children }) => {
|
||||
...newData,
|
||||
}));
|
||||
};
|
||||
const followUser = async (userId) => {
|
||||
try {
|
||||
if (currentUID && userId) {
|
||||
await followsRef.doc(`${currentUID}_${userId}`).set(
|
||||
{
|
||||
followerId: currentUID,
|
||||
followeeId: userId,
|
||||
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
setTooltip({ type: "success", text: "Abonnement mis à jour" });
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
setTooltip({ type: "error", text: e?.message || "Action impossible" });
|
||||
}
|
||||
};
|
||||
|
||||
const unfollowUser = async (userId) => {
|
||||
try {
|
||||
if (currentUID && userId) {
|
||||
await followsRef.doc(`${currentUID}_${userId}`).delete();
|
||||
setTooltip({ type: "success", text: "Vous ne suivez plus cet utilisateur" });
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
setTooltip({ type: "error", text: e?.message || "Action impossible" });
|
||||
}
|
||||
};
|
||||
const getUserByUid = async (uid) => {
|
||||
try {
|
||||
const userDoc = await usersRef.doc(uid).get();
|
||||
return userDoc.exists
|
||||
? {
|
||||
id: userDoc.id,
|
||||
...userDoc.data(),
|
||||
}
|
||||
: null;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const updateUserData = async ({ data, shouldSetTooltip = true }) => {
|
||||
try {
|
||||
@@ -193,13 +237,15 @@ export default ({ children }) => {
|
||||
userPlaylists,
|
||||
|
||||
setCurrentUserData,
|
||||
|
||||
getUserByUid,
|
||||
onSignOut,
|
||||
|
||||
pendingUserData,
|
||||
updatePendingUserData,
|
||||
|
||||
updateUserData,
|
||||
followUser,
|
||||
unfollowUser,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user