2021-09-13 20:41:05 +00:00
|
|
|
import React, { forwardRef, useImperativeHandle, useState } from 'react';
|
|
|
|
import { InteractionManager, StyleSheet, Text, View } from 'react-native';
|
2020-12-01 17:30:39 +00:00
|
|
|
import Modal from 'react-native-modal';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import Button from '../../containers/Button';
|
|
|
|
import TextInput from '../../containers/TextInput';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
padding: 16,
|
|
|
|
width: '100%',
|
|
|
|
borderRadius: 4
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
fontSize: 16,
|
|
|
|
paddingBottom: 8,
|
|
|
|
...sharedStyles.textBold,
|
|
|
|
...sharedStyles.textAlignCenter
|
|
|
|
},
|
|
|
|
button: {
|
|
|
|
minWidth: 96,
|
|
|
|
marginBottom: 0
|
|
|
|
},
|
|
|
|
buttonContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between'
|
|
|
|
},
|
|
|
|
tablet: {
|
|
|
|
height: undefined
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-02 03:20:03 +00:00
|
|
|
interface IRoomJoinCodeProps {
|
|
|
|
rid: string;
|
|
|
|
t: string;
|
|
|
|
onJoin: Function;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const JoinCode = React.memo(
|
2021-12-02 03:20:03 +00:00
|
|
|
forwardRef(({ rid, t, onJoin, isMasterDetail, theme }: IRoomJoinCodeProps, ref) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2021-12-02 03:20:03 +00:00
|
|
|
const [error, setError] = useState<any>(false);
|
2021-09-13 20:41:05 +00:00
|
|
|
const [code, setCode] = useState('');
|
2020-12-01 17:30:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const show = () => setVisible(true);
|
2020-12-01 17:30:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const hide = () => setVisible(false);
|
2020-12-01 17:30:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const joinRoom = async () => {
|
|
|
|
try {
|
|
|
|
await RocketChat.joinRoom(rid, code, t);
|
|
|
|
onJoin();
|
|
|
|
hide();
|
|
|
|
} catch (e) {
|
|
|
|
setError(true);
|
|
|
|
}
|
|
|
|
};
|
2020-12-01 17:30:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
useImperativeHandle(ref, () => ({ show }));
|
2020-12-01 17:30:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return (
|
2021-12-02 03:20:03 +00:00
|
|
|
<Modal avoidKeyboard useNativeDriver isVisible={visible} hideModalContentWhileAnimating>
|
2021-09-13 20:41:05 +00:00
|
|
|
<View style={styles.container} testID='join-code'>
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.content,
|
|
|
|
isMasterDetail && [sharedStyles.modalFormSheet, styles.tablet],
|
|
|
|
{ backgroundColor: themes[theme].backgroundColor }
|
|
|
|
]}>
|
|
|
|
<Text style={[styles.title, { color: themes[theme].titleText }]}>{I18n.t('Insert_Join_Code')}</Text>
|
|
|
|
<TextInput
|
|
|
|
value={code}
|
2020-12-01 17:30:39 +00:00
|
|
|
theme={theme}
|
2021-12-02 03:20:03 +00:00
|
|
|
inputRef={(e: any) => InteractionManager.runAfterInteractions(() => e?.getNativeRef()?.focus())}
|
2021-09-13 20:41:05 +00:00
|
|
|
returnKeyType='send'
|
|
|
|
autoCapitalize='none'
|
|
|
|
onChangeText={setCode}
|
|
|
|
onSubmitEditing={joinRoom}
|
|
|
|
placeholder={I18n.t('Join_Code')}
|
|
|
|
secureTextEntry
|
|
|
|
error={error && { error: 'error-code-invalid', reason: I18n.t('Code_or_password_invalid') }}
|
|
|
|
testID='join-code-input'
|
2020-12-01 17:30:39 +00:00
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
<View style={styles.buttonContainer}>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Cancel')}
|
|
|
|
type='secondary'
|
|
|
|
style={styles.button}
|
|
|
|
backgroundColor={themes[theme].chatComponentBackground}
|
|
|
|
theme={theme}
|
|
|
|
testID='join-code-cancel'
|
|
|
|
onPress={hide}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Join')}
|
|
|
|
type='primary'
|
|
|
|
style={styles.button}
|
|
|
|
theme={theme}
|
|
|
|
testID='join-code-submit'
|
|
|
|
onPress={joinRoom}
|
|
|
|
/>
|
|
|
|
</View>
|
2020-12-01 17:30:39 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
2021-09-13 20:41:05 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2020-12-01 17:30:39 +00:00
|
|
|
|
2021-12-02 03:20:03 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-12-01 17:30:39 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
|
|
|
});
|
|
|
|
export default connect(mapStateToProps, null, null, { forwardRef: true })(JoinCode);
|