fix the delete from rooms and finished RoomInfoView, missing RoomInfoEditView

This commit is contained in:
Reinaldo Neto 2022-12-13 12:54:47 -03:00
parent 13f5075f7c
commit e48b174118
6 changed files with 37 additions and 9 deletions

View File

@ -31,6 +31,7 @@ const AvatarContainer = ({
}: IAvatar & { isUserProfile?: boolean; handleEdit?: () => void }): React.ReactElement => { }: IAvatar & { isUserProfile?: boolean; handleEdit?: () => void }): React.ReactElement => {
const subscription = useRef<Subscription>(); const subscription = useRef<Subscription>();
const [avatarETag, setAvatarETag] = useState<string | undefined>(''); const [avatarETag, setAvatarETag] = useState<string | undefined>('');
const previousAvatarEtag = useRef<string | undefined>('');
const { colors } = useTheme(); const { colors } = useTheme();
const isDirect = () => type === 'd'; const isDirect = () => type === 'd';
@ -82,7 +83,7 @@ const AvatarContainer = ({
}; };
useEffect(() => { useEffect(() => {
if (!avatarETag) { if (!avatarETag || avatarETag !== previousAvatarEtag.current) {
init(); init();
} }
return () => { return () => {
@ -92,6 +93,10 @@ const AvatarContainer = ({
}; };
}, [text, type, size, avatarETag, externalProviderUrl]); }, [text, type, size, avatarETag, externalProviderUrl]);
useEffect(() => {
previousAvatarEtag.current = avatarETag;
}, [avatarETag]);
return ( return (
<> <>
<Avatar <Avatar

View File

@ -17,7 +17,7 @@ export interface IAvatarButton {
} }
export interface IAvatar { export interface IAvatar {
data: {} | string | null; data: string | null;
url?: string; url?: string;
contentType?: string; contentType?: string;
service?: any; service?: any;

View File

@ -553,7 +553,7 @@ export const saveRoomSettings = (
rid: string, rid: string,
params: { params: {
roomName?: string; roomName?: string;
roomAvatar?: string; roomAvatar?: string | null;
roomDescription?: string; roomDescription?: string;
roomTopic?: string; roomTopic?: string;
roomAnnouncement?: string; roomAnnouncement?: string;

View File

@ -183,6 +183,8 @@ export type ChatsStackParamList = {
ChangeAvatarView: { ChangeAvatarView: {
fromUser?: boolean; fromUser?: boolean;
titleHeader?: string; titleHeader?: string;
room?: ISubscription;
t?: SubscriptionType;
}; };
}; };
@ -201,6 +203,8 @@ export type ProfileStackParamList = {
ChangeAvatarView: { ChangeAvatarView: {
fromUser?: boolean; fromUser?: boolean;
titleHeader?: string; titleHeader?: string;
room?: ISubscription;
t?: SubscriptionType;
}; };
}; };

View File

@ -39,7 +39,7 @@ const ChangeAvatarView = () => {
const avatarUrl = useRef<string | undefined>(''); const avatarUrl = useRef<string | undefined>('');
const navigation = useNavigation<StackNavigationProp<ChatsStackParamList, 'ChangeAvatarView'>>(); const navigation = useNavigation<StackNavigationProp<ChatsStackParamList, 'ChangeAvatarView'>>();
const { fromUser, titleHeader } = useRoute<RouteProp<ChatsStackParamList, 'ChangeAvatarView'>>().params; const { fromUser, titleHeader, room, t } = useRoute<RouteProp<ChatsStackParamList, 'ChangeAvatarView'>>().params;
useLayoutEffect(() => { useLayoutEffect(() => {
navigation.setOptions({ navigation.setOptions({
@ -72,6 +72,21 @@ const ChangeAvatarView = () => {
}; };
const submit = async () => { const submit = async () => {
if (!fromUser && room?.rid) {
try {
setSaving(true);
await Services.saveRoomSettings(room.rid, { roomAvatar: avatar?.data });
setSaving(false);
avatarUrl.current = '';
return navigation.goBack();
} catch (e) {
log(e);
setSaving(false);
return handleError(e, 'setAvatarFromService', 'changing_avatar');
}
}
// Change Users Avatar
if (avatar?.url) { if (avatar?.url) {
try { try {
setSaving(true); setSaving(true);
@ -107,9 +122,8 @@ const ChangeAvatarView = () => {
}; };
const resetRoomAvatar = () => { const resetRoomAvatar = () => {
setAvatar(undefined); setAvatar({ data: null });
avatarUrl.current = 'resetRoomAvatar';
// await Services.saveRoomSettings(room.rid, params);
}; };
const pickImage = async () => { const pickImage = async () => {
@ -130,6 +144,8 @@ const ChangeAvatarView = () => {
} }
}; };
const ridProps = avatarUrl.current !== 'resetRoomAvatar' ? { rid: room?.rid } : {};
return ( return (
<KeyboardView <KeyboardView
style={{ backgroundColor: colors.auxiliaryBackground }} style={{ backgroundColor: colors.auxiliaryBackground }}
@ -145,11 +161,13 @@ const ChangeAvatarView = () => {
> >
<View style={styles.avatarContainer} testID='change-avatar-view-avatar'> <View style={styles.avatarContainer} testID='change-avatar-view-avatar'>
<Avatar <Avatar
text={textAvatar || user.username} text={room?.name || textAvatar || user.username}
avatar={avatar?.url} avatar={avatar?.url}
isStatic={avatar?.url} isStatic={avatar?.url}
size={100} size={100}
isUserProfile={fromUser} isUserProfile={fromUser}
type={t}
{...ridProps}
/> />
</View> </View>
{fromUser ? <AvatarUrl submit={value => setAvatar({ url: value, data: value, service: 'url' })} /> : null} {fromUser ? <AvatarUrl submit={value => setAvatar({ url: value, data: value, service: 'url' })} /> : null}

View File

@ -402,7 +402,8 @@ class RoomInfoView extends React.Component<IRoomInfoViewProps, IRoomInfoViewStat
handleEditAvatar = () => { handleEditAvatar = () => {
const {navigation} = this.props const {navigation} = this.props
navigation.navigate("ChangeAvatarView",{titleHeader:I18n.t('Room_Info')}) const { room } = this.state;
navigation.navigate('ChangeAvatarView', { titleHeader: I18n.t('Room_Info'), room, t: this.t });
} }
renderAvatar = (room: ISubscription, roomUser: IUserParsed) => { renderAvatar = (room: ISubscription, roomUser: IUserParsed) => {