-
Notifications
You must be signed in to change notification settings - Fork 9
/
Profile.js
116 lines (100 loc) · 3.01 KB
/
Profile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React, { useEffect, useState, useContext } from 'react';
import { ScrollView, StyleSheet } from 'react-native';
import Header from './Header';
import Actions from './Actions';
import Posts from '../posts/Posts';
import Context from '../../context';
import { database, databaseSet, databaseRef, databaseGet, databaseChild } from "../../firebase";
const Profile = () => {
const [profile, setProfile] = useState(null);
const [postCategory, setPostCategory] = useState(null);
const { user, hasNewPost, setHasNewPost } = useContext(Context);
useEffect(() => {
if (user && user.id) {
loadProfile(user.id);
return () => {
setProfile(null);
};
}
}, [user]);
useEffect(() => {
if (hasNewPost) {
loadProfile(user.id);
setHasNewPost(false);
return () => {
setProfile(null);
};
}
}, [hasNewPost]);
const getUser = async (id) => {
if (!id) {
return null;
}
const ref = databaseRef(database);
const snapshot = await databaseGet(databaseChild(ref, `users/${id}`));
if (!snapshot || !snapshot.exists()) {
return null
}
return snapshot.val();
};
const loadProfile = async (id) => {
const profile = await getUser(id);
setProfile(() => profile);
};
const onCategorySelected = (category) => {
if (!category) {
return;
}
setPostCategory(() => category);
}
const updateFolowers = (hasFollowed, profile) => {
if (!profile) {
return;
}
if (hasFollowed) {
return profile.followers && profile.followers.length ? profile.followers.filter(follower => follower !== user.id) : [];
}
return profile.followers && profile.followers.length ? [...profile.followers, user.id] : [user.id];
};
const onFollowToggled = async (profile, hasFollowed) => {
if (!profile) {
return;
}
const latestProfile = await getUser(profile.id);
if (!latestProfile) {
return;
}
const followers = updateFolowers(hasFollowed, profile);
const nFollowers = followers.length;
latestProfile.followers = followers;
latestProfile.nFollowers = nFollowers;
await databaseSet(databaseRef(database, 'users/' + latestProfile.id), latestProfile);
await loadProfile(latestProfile.id);
}
const hasFollowed = () => {
if (!profile || !user) {
return false;
}
if (!profile.followers || !profile.followers.length) {
return false;
}
return profile.followers.includes(user.id);
};
if (!user) {
return <></>;
}
const followed = hasFollowed();
return (
<ScrollView style={styles.scrollViewContainer}>
<Header profile={profile} hasFollowed={followed} onFollowToggled={onFollowToggled} isFollowHidden={user && profile && user.id === profile.id} />
<Actions onCategorySelected={onCategorySelected} />
<Posts authorId={user.id} postCategory={postCategory} isGrid />
</ScrollView>
);
};
const styles = StyleSheet.create({
scrollViewContainer: {
backgroundColor: '#fff'
}
});
export default Profile;