2022-12-07 05:01:00 +00:00
|
|
|
import React, { useEffect, useLayoutEffect, useState } from 'react';
|
2022-12-07 03:11:25 +00:00
|
|
|
import { ScrollView, View } from 'react-native';
|
2022-12-07 05:01:00 +00:00
|
|
|
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2022-12-07 03:11:25 +00:00
|
|
|
|
|
|
|
import KeyboardView from '../../containers/KeyboardView';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import * as List from '../../containers/List';
|
|
|
|
import styles from './styles';
|
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import AvatarUrl from './AvatarUrl';
|
|
|
|
import Button from '../../containers/Button';
|
|
|
|
import I18n from '../../i18n';
|
2022-12-07 05:01:00 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
|
|
|
import { IAvatar } from '../../definitions';
|
|
|
|
import { Services } from '../../lib/services';
|
|
|
|
import AvatarSuggestion from './AvatarSuggestion';
|
2022-12-07 03:11:25 +00:00
|
|
|
|
|
|
|
const ChangeAvatarView = () => {
|
|
|
|
const [avatarUrl, setAvatarUrl] = useState('');
|
2022-12-07 05:01:00 +00:00
|
|
|
const [avatarSuggestions, setAvatarSuggestions] = useState<IAvatar[]>([]);
|
2022-12-07 03:11:25 +00:00
|
|
|
const { colors } = useTheme();
|
|
|
|
|
2022-12-07 05:01:00 +00:00
|
|
|
const navigation = useNavigation<StackNavigationProp<ChatsStackParamList, 'ChangeAvatarView'>>();
|
|
|
|
const { fromUser, titleHeader } = useRoute<RouteProp<ChatsStackParamList, 'ChangeAvatarView'>>().params;
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2022-12-07 21:02:16 +00:00
|
|
|
titleHeader ? navigation.setOptions({ title: titleHeader }) : navigation.setOptions({ title: I18n.t('Avatar') });
|
2022-12-07 05:01:00 +00:00
|
|
|
}, [titleHeader, navigation]);
|
|
|
|
|
|
|
|
const getAvatarSuggestion = async () => {
|
|
|
|
const result = await Services.getAvatarSuggestion();
|
|
|
|
const suggestions = Object.keys(result).map(service => {
|
|
|
|
const { url, blob, contentType } = result[service];
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
data: blob,
|
|
|
|
service,
|
|
|
|
contentType
|
|
|
|
};
|
|
|
|
});
|
|
|
|
setAvatarSuggestions(suggestions);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (fromUser) {
|
|
|
|
getAvatarSuggestion();
|
|
|
|
}
|
|
|
|
}, [fromUser]);
|
|
|
|
|
2022-12-07 03:11:25 +00:00
|
|
|
const user = useAppSelector(state => getUserSelector(state));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<KeyboardView
|
|
|
|
style={{ backgroundColor: colors.auxiliaryBackground }}
|
|
|
|
contentContainerStyle={sharedStyles.container}
|
|
|
|
keyboardVerticalOffset={128}
|
|
|
|
>
|
|
|
|
<StatusBar />
|
|
|
|
<SafeAreaView testID='change-avatar-view'>
|
|
|
|
<ScrollView
|
|
|
|
contentContainerStyle={sharedStyles.containerScrollView}
|
|
|
|
testID='change-avatar-view-list'
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
>
|
|
|
|
<View style={styles.avatarContainer} testID='change-avatar-view-avatar'>
|
|
|
|
<Avatar text={user.username} avatar={avatarUrl} isStatic={avatarUrl} size={100} />
|
|
|
|
</View>
|
2022-12-07 21:02:16 +00:00
|
|
|
<AvatarUrl submit={setAvatarUrl} />
|
2022-12-07 03:11:25 +00:00
|
|
|
<List.Separator style={styles.separator} />
|
2022-12-07 21:02:16 +00:00
|
|
|
{fromUser && avatarSuggestions.length ? <AvatarSuggestion avatarSuggestions={avatarSuggestions} /> : null}
|
2022-12-07 03:11:25 +00:00
|
|
|
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Upload_image')}
|
|
|
|
type='secondary'
|
|
|
|
backgroundColor={colors.chatComponentBackground}
|
|
|
|
onPress={() => {}}
|
|
|
|
testID='change-avatar-view-logout-other-locations'
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Delete_image')}
|
|
|
|
type='primary'
|
|
|
|
backgroundColor={colors.dangerColor}
|
|
|
|
onPress={() => {}}
|
|
|
|
testID='change-avatar-view-delete-my-account'
|
|
|
|
/>
|
2022-12-07 21:02:16 +00:00
|
|
|
<Button title={I18n.t('Save')} type='primary' onPress={() => {}} testID='change-avatar-view-submit' />
|
2022-12-07 03:11:25 +00:00
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChangeAvatarView;
|