aboutsummaryrefslogtreecommitdiff
path: root/src/components/profile/TaggAvatar.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/profile/TaggAvatar.tsx')
-rw-r--r--src/components/profile/TaggAvatar.tsx66
1 files changed, 46 insertions, 20 deletions
diff --git a/src/components/profile/TaggAvatar.tsx b/src/components/profile/TaggAvatar.tsx
index 304b9e3a..8ccae2ef 100644
--- a/src/components/profile/TaggAvatar.tsx
+++ b/src/components/profile/TaggAvatar.tsx
@@ -7,6 +7,7 @@ import {useDispatch, useSelector} from 'react-redux';
import {loadUserData, resetHeaderAndProfileImage} from '../../store/actions';
import PurplePlus from '../../assets/icons/purple-plus.svg';
import {patchProfile, validateImageLink} from '../../utils';
+import {useIsFocused} from '@react-navigation/native';
const PROFILE_DIM = 100;
@@ -14,28 +15,35 @@ interface TaggAvatarProps {
style?: object;
userXId: string | undefined;
screenType: ScreenType;
+ editable: boolean;
}
const TaggAvatar: React.FC<TaggAvatarProps> = ({
style,
screenType,
userXId,
+ editable = false,
}) => {
- const {avatar} = useSelector((state: RootState) =>
+ const {avatar, user} = useSelector((state: RootState) =>
userXId ? state.userX[screenType][userXId] : state.user,
);
const dispatch = useDispatch();
const [needsUpdate, setNeedsUpdate] = useState(false);
- const [loading, setLoading] = useState(false);
+ const [updating, setUpdating] = useState(false);
+ const [loading, setLoading] = useState(true);
const [validImage, setValidImage] = useState<boolean>(true);
- const {user} = useSelector((state: RootState) =>
- userXId ? state.userX[screenType][userXId] : state.user,
- );
+ const isFocused = useIsFocused();
useEffect(() => {
checkAvatar(avatar);
+ setLoading(false);
}, []);
useEffect(() => {
+ checkAvatar(avatar);
+ }, [avatar, isFocused]);
+
+ useEffect(() => {
+ checkAvatar(avatar);
if (needsUpdate) {
const userId = user.userId;
const username = user.username;
@@ -47,8 +55,11 @@ const TaggAvatar: React.FC<TaggAvatarProps> = ({
const handleNewImage = async () => {
setLoading(true);
const result = await patchProfile('profile', user.userId);
+ setLoading(true);
if (result) {
+ setUpdating(true);
setNeedsUpdate(true);
+ setLoading(false);
} else {
setLoading(false);
}
@@ -61,21 +72,28 @@ const TaggAvatar: React.FC<TaggAvatarProps> = ({
}
};
- if (!validImage && userXId === undefined && !loading) {
- return (
- <>
- <Avatar style={[styles.image, style]} uri={avatar} />
- <TouchableOpacity
- accessible={true}
- accessibilityLabel="ADD PROFILE PICTURE"
- onPress={() => handleNewImage()}>
- <PurplePlus style={styles.plus} />
- </TouchableOpacity>
- </>
- );
- } else {
- return <Avatar style={[styles.image, style]} uri={avatar} />;
- }
+ return (
+ <>
+ <Avatar
+ style={[styles.image, style]}
+ uri={avatar}
+ loading={loading}
+ loadingStyle={styles.loadingLarge}
+ />
+ {editable &&
+ !validImage &&
+ userXId === undefined &&
+ !loading &&
+ !updating && (
+ <TouchableOpacity
+ accessible={true}
+ accessibilityLabel="ADD PROFILE PICTURE"
+ onPress={() => handleNewImage()}>
+ <PurplePlus style={styles.plus} />
+ </TouchableOpacity>
+ )}
+ </>
+ );
};
const styles = StyleSheet.create({
@@ -83,12 +101,20 @@ const styles = StyleSheet.create({
height: PROFILE_DIM,
width: PROFILE_DIM,
borderRadius: PROFILE_DIM / 2,
+ overflow: 'hidden',
},
plus: {
position: 'absolute',
bottom: 35,
right: 0,
},
+ loadingLarge: {
+ height: PROFILE_DIM * 0.8,
+ width: PROFILE_DIM * 0.8,
+ alignSelf: 'center',
+ justifyContent: 'center',
+ aspectRatio: 2,
+ },
});
export default TaggAvatar;