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';
|
2022-06-27 18:46:59 +00:00
|
|
|
import { FormTextInput } from '../../containers/TextInput';
|
2020-12-01 17:30:39 +00:00
|
|
|
import sharedStyles from '../Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-03-02 14:18:01 +00:00
|
|
|
import { IApplicationState } from '../../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes } from '../../theme';
|
2020-12-01 17:30:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-02 14:18:01 +00:00
|
|
|
export interface IJoinCodeProps {
|
|
|
|
rid: string;
|
|
|
|
t: string;
|
|
|
|
onJoin: Function;
|
|
|
|
isMasterDetail: boolean;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-03-02 14:18:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 22:58:23 +00:00
|
|
|
export interface IJoinCode {
|
|
|
|
show: () => void;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const JoinCode = React.memo(
|
2022-05-02 22:58:23 +00:00
|
|
|
forwardRef<IJoinCode, IJoinCodeProps>(({ rid, t, onJoin, isMasterDetail, theme }, ref) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [error, setError] = useState(false);
|
|
|
|
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
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
const handleJoinRoom = async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.joinRoom(rid, code, t as any);
|
2021-09-13 20:41:05 +00:00
|
|
|
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 (
|
2022-03-22 13:44:07 +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>
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2021-09-13 20:41:05 +00:00
|
|
|
value={code}
|
2022-03-02 14:18:01 +00:00
|
|
|
// TODO: find a way to type this ref
|
|
|
|
inputRef={(e: any) => InteractionManager.runAfterInteractions(() => e?.getNativeRef()?.focus())}
|
2021-09-13 20:41:05 +00:00
|
|
|
returnKeyType='send'
|
|
|
|
autoCapitalize='none'
|
|
|
|
onChangeText={setCode}
|
2022-04-28 20:37:25 +00:00
|
|
|
onSubmitEditing={handleJoinRoom}
|
2021-09-13 20:41:05 +00:00
|
|
|
placeholder={I18n.t('Join_Code')}
|
|
|
|
secureTextEntry
|
2022-03-02 14:18:01 +00:00
|
|
|
error={error ? { error: 'error-code-invalid', reason: I18n.t('Code_or_password_invalid') } : undefined}
|
2021-09-13 20:41:05 +00:00
|
|
|
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}
|
|
|
|
testID='join-code-cancel'
|
|
|
|
onPress={hide}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Join')}
|
|
|
|
type='primary'
|
|
|
|
style={styles.button}
|
|
|
|
testID='join-code-submit'
|
2022-04-28 20:37:25 +00:00
|
|
|
onPress={handleJoinRoom}
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
|
|
|
</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
|
|
|
|
2022-03-02 14:18:01 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-12-01 17:30:39 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
|
|
|
});
|
2022-05-02 22:58:23 +00:00
|
|
|
|
2020-12-01 17:30:39 +00:00
|
|
|
export default connect(mapStateToProps, null, null, { forwardRef: true })(JoinCode);
|