Files
EMI-ExpoAPP/components/ProfileCard.js
T
2022-11-21 17:25:44 -05:00

89 lines
3.0 KiB
JavaScript

import React, { useState, useEffect } from 'react';
import { Text, StyleSheet } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import { Avatar, Button, Card, Title, Paragraph } from 'react-native-paper';
import API from './../API.js';
import { useNavigation } from '@react-navigation/native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import FollowButton from './basics/FollowButton.js';
const DefaultPhoto = "https://social.emmint.com/uploads/e6f9be6d665dc43417701bf16a90122c.png";
const storeName = async (key, value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('Name_' + key, jsonValue)
} catch (e) {
}
}
const getName = async (key) => {
try {
const value = await AsyncStorage.getItem('Name_' + key)
if (value !== null) {
return JSON.parse(value);
}
} catch (e) {
return []
}
}
let ProfileCard = ({ profileid, hideIcon, profileObj }) => {
let [profile, setProfile] = useState(profileObj || {});
const navigation = useNavigation();
useEffect(() => {
let subscribed = true;
const getData = async () => {
if (profileObj._id) return 0;
let cacheProfile = await getName(profileid);
if (cacheProfile && cacheProfile.profile && subscribed) setProfile(cacheProfile);
let p = await API.getUserProfile(profileid).catch(() => { return {} });
if (subscribed) setProfile(p);
storeName(profileid, p)
};
getData();
return () => {
subscribed = false;
}
}, [profileid]);
let icon = profile._id ? (!profile.isGroup ? "person-outline" : "group") : '';
icon = icon === "person-outline" && profile.subscription && profile.subscription > (new Date() - 0) ? "assignment-ind" : icon;
icon = icon === "group" && profile.isCourse ? "subscriptions" : icon;
let photoUrl = profile.profile.photo ? 'https://social.emmint.com/' + profile.profile.photo : DefaultPhoto;
const onPress = () => {
return navigation.navigate('Profile', { profileid: profile._id })
}
return (
<Card style={styles.content}>
<Card.Content>
<Avatar.Image size={150} source={{ uri: photoUrl }} />
<Title onPress={onPress} numberOfLines={1}>
<Text>
{profile.profile && profile.profile.firstName} {profile.profile && profile.profile.lastName}
</Text>
</Title>
<Paragraph numberOfLines={2}>{profileObj.profile.description}</Paragraph>
<FollowButton profile={profile} />
</Card.Content>
</Card>
);
}
export default React.memo(ProfileCard);
const styles = StyleSheet.create({
content: {
margin: 4,
width: "50%",
},
centerItems: {
justifyContent: 'center',
alignItems: 'center',
}
});