2022-08-26 13:21:25 +00:00
|
|
|
import { CompositeNavigationProp, useNavigation } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import React from 'react';
|
|
|
|
import { View } from 'react-native';
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
|
|
|
import { setLoading } from '../../../actions/selectedUsers';
|
|
|
|
import * as List from '../../../containers/List';
|
|
|
|
import { TSubscriptionModel } from '../../../definitions';
|
|
|
|
import i18n from '../../../i18n';
|
|
|
|
import { usePermissions } from '../../../lib/hooks';
|
|
|
|
import log, { events, logEvent } from '../../../lib/methods/helpers/log';
|
|
|
|
import { Services } from '../../../lib/services';
|
|
|
|
import { MasterDetailInsideStackParamList } from '../../../stacks/MasterDetailStack/types';
|
|
|
|
import { ChatsStackParamList } from '../../../stacks/types';
|
|
|
|
|
|
|
|
type TNavigation = CompositeNavigationProp<
|
|
|
|
StackNavigationProp<ChatsStackParamList, 'RoomActionsView'>,
|
|
|
|
StackNavigationProp<MasterDetailInsideStackParamList>
|
|
|
|
>;
|
|
|
|
|
|
|
|
interface IActionsSection {
|
|
|
|
rid: TSubscriptionModel['rid'];
|
|
|
|
t: TSubscriptionModel['t'];
|
|
|
|
joined: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ActionsSection({ rid, t, joined }: IActionsSection): React.ReactElement {
|
|
|
|
const { navigate, pop } = useNavigation<TNavigation>();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const [addUserToJoinedRoomPermission, addUserToAnyCRoomPermission, addUserToAnyPRoomPermission, createInviteLinksPermission] =
|
|
|
|
usePermissions(['add-user-to-joined-room', 'add-user-to-any-c-room', 'add-user-to-any-p-room', 'create-invite-links'], rid);
|
|
|
|
|
|
|
|
const canAddUser =
|
|
|
|
(joined && addUserToJoinedRoomPermission) ||
|
|
|
|
(t === 'c' && addUserToAnyCRoomPermission) ||
|
|
|
|
(t === 'p' && addUserToAnyPRoomPermission) ||
|
|
|
|
false;
|
|
|
|
|
|
|
|
const canInviteUser = createInviteLinksPermission;
|
|
|
|
|
|
|
|
const handleOnPress = ({
|
|
|
|
route,
|
|
|
|
params
|
|
|
|
}: {
|
|
|
|
route: keyof ChatsStackParamList;
|
|
|
|
params: ChatsStackParamList[keyof ChatsStackParamList];
|
|
|
|
}) => {
|
2023-05-18 21:09:33 +00:00
|
|
|
// @ts-ignore
|
2022-08-26 13:21:25 +00:00
|
|
|
navigate(route, params);
|
|
|
|
// @ts-ignore
|
|
|
|
logEvent(events[`RM_GO_${route.replace('View', '').toUpperCase()}`]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const addUser = async () => {
|
|
|
|
try {
|
|
|
|
dispatch(setLoading(true));
|
|
|
|
await Services.addUsersToRoom(rid);
|
|
|
|
pop();
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
} finally {
|
|
|
|
dispatch(setLoading(false));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-09-23 20:24:37 +00:00
|
|
|
<View style={{ paddingTop: canAddUser || canInviteUser ? 16 : 0, paddingBottom: canAddUser || canInviteUser ? 16 : 0 }}>
|
2022-08-26 13:21:25 +00:00
|
|
|
{['c', 'p'].includes(t) && canAddUser ? (
|
|
|
|
<>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Add_users'
|
|
|
|
onPress={() =>
|
|
|
|
handleOnPress({
|
|
|
|
route: 'SelectedUsersView',
|
|
|
|
params: {
|
|
|
|
title: i18n.t('Add_users'),
|
2023-01-25 18:28:03 +00:00
|
|
|
nextAction: addUser,
|
|
|
|
showSkipText: false
|
2022-08-26 13:21:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
testID='room-actions-add-user'
|
|
|
|
left={() => <List.Icon name='add' />}
|
|
|
|
showActionIndicator
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{['c', 'p'].includes(t) && canInviteUser ? (
|
|
|
|
<>
|
|
|
|
<List.Item
|
|
|
|
title='Invite_users'
|
|
|
|
onPress={() =>
|
|
|
|
handleOnPress({
|
|
|
|
route: 'InviteUsersView',
|
|
|
|
params: { rid }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
testID='room-actions-invite-user'
|
|
|
|
left={() => <List.Icon name='user-add' />}
|
|
|
|
showActionIndicator
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|