verdnatura-chat/app/views/ChangeAvatarView/index.tsx

101 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useLayoutEffect, useState } from 'react';
2022-12-07 03:11:25 +00:00
import { ScrollView, View } from 'react-native';
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';
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('');
const [avatarSuggestions, setAvatarSuggestions] = useState<IAvatar[]>([]);
2022-12-07 03:11:25 +00:00
const { colors } = useTheme();
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') });
}, [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;