Change main page and modify flows
This commit is contained in:
@@ -3,10 +3,11 @@ import { useDataFromRef } from "react-native-minuit/src/hooks";
|
||||
import useMinuit from "react-native-minuit/src/hooks/useMinuit";
|
||||
import { createContext, useContext, useGlobal } from "reactn";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { checkIfEmailIsValid } from "../actions/signupActions";
|
||||
import firebase, {
|
||||
followsRef,
|
||||
arrayRemove,
|
||||
arrayUnion,
|
||||
playlistsRef,
|
||||
projectsRef,
|
||||
usersRef,
|
||||
@@ -24,26 +25,18 @@ export default ({ children }) => {
|
||||
const [currentUID] = useGlobal("currentUID");
|
||||
const [currentUserData, setCurrentUserData] = useGlobal("currentUserData");
|
||||
const [currentUserRoles] = useGlobal("currentUserRoles");
|
||||
const [userProjects, setUserProjects] = useState([]);
|
||||
const [userLikedProjects, setUserLikedProjects] = useState([]);
|
||||
const [userPlaylists, setUserPlaylists] = useState([]);
|
||||
useDataFromRef({
|
||||
// Current user document (live)
|
||||
const { data: currentUserDoc = null } = useDataFromRef({
|
||||
ref: currentUID ? usersRef.doc(currentUID) : null,
|
||||
updateGlobalState: "currentUserData",
|
||||
simpleRef: true,
|
||||
listener: true,
|
||||
condition: currentUID,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
onUpdate: (data) => {
|
||||
if (data) {
|
||||
setCurrentUserData(data);
|
||||
} else {
|
||||
setCurrentUserData(null);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Subscribe to user's projects (musics)
|
||||
useDataFromRef({
|
||||
const { data: userProjects = [] } = useDataFromRef({
|
||||
ref: currentUID
|
||||
? projectsRef
|
||||
.where("userId", "==", currentUID)
|
||||
@@ -53,11 +46,10 @@ export default ({ children }) => {
|
||||
listener: true,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
onUpdate: (list) => setUserProjects(Array.isArray(list) ? list : []),
|
||||
});
|
||||
|
||||
// Subscribe to user's liked projects
|
||||
useDataFromRef({
|
||||
const { data: userLikedProjects = [] } = useDataFromRef({
|
||||
ref: currentUID
|
||||
? projectsRef
|
||||
.where("likedBy", "array-contains", currentUID)
|
||||
@@ -67,17 +59,26 @@ export default ({ children }) => {
|
||||
listener: true,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
onUpdate: (list) => setUserLikedProjects(Array.isArray(list) ? list : []),
|
||||
});
|
||||
|
||||
// Subscribe to user's playlists
|
||||
useDataFromRef({
|
||||
const { data: userPlaylists = [] } = useDataFromRef({
|
||||
ref: currentUID ? playlistsRef.where("createdBy", "==", currentUID) : null,
|
||||
simpleRef: false,
|
||||
listener: true,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
onUpdate: (list) => setUserPlaylists(Array.isArray(list) ? list : []),
|
||||
});
|
||||
|
||||
// Live list of users that the current user is following ("abonnements")
|
||||
const { data: userFollowing = [] } = useDataFromRef({
|
||||
ref: currentUID
|
||||
? usersRef.where("followedBy", "array-contains", currentUID)
|
||||
: null,
|
||||
simpleRef: false,
|
||||
listener: true,
|
||||
condition: !!currentUID,
|
||||
refreshArray: [currentUID],
|
||||
});
|
||||
|
||||
const updatePendingUserData = (newData) => {
|
||||
@@ -89,13 +90,12 @@ export default ({ children }) => {
|
||||
const followUser = async (userId) => {
|
||||
try {
|
||||
if (currentUID && userId) {
|
||||
await followsRef.doc(`${currentUID}_${userId}`).set(
|
||||
await usersRef.doc(userId).set(
|
||||
{
|
||||
followerId: currentUID,
|
||||
followeeId: userId,
|
||||
createdAt: new Date(),
|
||||
followedBy: arrayUnion(currentUID),
|
||||
lastFollowersUpdateAt: new Date(),
|
||||
},
|
||||
{ merge: true }
|
||||
{ merge: true },
|
||||
);
|
||||
setTooltip({ type: "success", text: "Abonnement mis à jour" });
|
||||
}
|
||||
@@ -108,7 +108,13 @@ export default ({ children }) => {
|
||||
const unfollowUser = async (userId) => {
|
||||
try {
|
||||
if (currentUID && userId) {
|
||||
await followsRef.doc(`${currentUID}_${userId}`).delete();
|
||||
await usersRef.doc(userId).set(
|
||||
{
|
||||
followedBy: arrayRemove(currentUID),
|
||||
lastFollowersUpdateAt: new Date(),
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
setTooltip({
|
||||
type: "success",
|
||||
text: "Vous ne suivez plus cet utilisateur",
|
||||
@@ -234,10 +240,11 @@ export default ({ children }) => {
|
||||
|
||||
currentUserRoles,
|
||||
currentUID,
|
||||
currentUserData,
|
||||
currentUserData: currentUserDoc,
|
||||
userProjects,
|
||||
userLikedProjects,
|
||||
userPlaylists,
|
||||
followingCount: userFollowing?.length || 0,
|
||||
|
||||
setCurrentUserData,
|
||||
getUserByUid,
|
||||
|
||||
Reference in New Issue
Block a user