Rocket.Chat.ReactNative/app/containers/Passcode/Base/index.js

127 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-04-23 20:44:00 +00:00
import React, {
useState, forwardRef, useImperativeHandle, useRef
} from 'react';
2020-04-23 20:11:17 +00:00
import { View, Text } from 'react-native';
2020-04-23 18:28:40 +00:00
import { Col, Row, Grid } from 'react-native-easy-grid';
import _ from 'lodash';
2020-04-23 19:37:00 +00:00
import PropTypes from 'prop-types';
2020-04-23 20:44:00 +00:00
import * as Animatable from 'react-native-animatable';
import * as Haptics from 'expo-haptics';
2020-04-23 18:28:40 +00:00
import styles from './styles';
import Button from './Button';
import Dots from './Dots';
2020-04-23 20:11:17 +00:00
import { TYPE } from '../constants';
import { themes } from '../../../constants/colors';
2020-04-23 20:44:00 +00:00
import { PASSCODE_LENGTH } from '../../../constants/localAuthentication';
2020-04-23 18:28:40 +00:00
const Base = forwardRef(({
2020-04-23 20:59:35 +00:00
theme, type, onEndProcess, previousPasscode, title, subtitle, onError
2020-04-23 18:28:40 +00:00
}, ref) => {
2020-04-23 21:22:08 +00:00
const rootRef = useRef();
2020-04-23 20:44:00 +00:00
const dotsRef = useRef();
2020-04-23 18:28:40 +00:00
const [passcode, setPasscode] = useState('');
const wrongPasscode = () => {
setPasscode('');
2020-04-23 20:44:00 +00:00
dotsRef?.current?.shake(500);
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
2020-04-23 18:28:40 +00:00
};
2020-04-23 21:22:08 +00:00
const animate = (animation, duration = 500) => {
rootRef?.current?.[animation](duration);
};
2020-04-23 18:28:40 +00:00
const onPressNumber = text => setPasscode((p) => {
const currentPasscode = p + text;
if (currentPasscode?.length === PASSCODE_LENGTH) {
switch (type) {
case TYPE.CHOOSE:
onEndProcess(currentPasscode);
break;
case TYPE.CONFIRM:
if (currentPasscode !== previousPasscode) {
2020-04-23 20:59:35 +00:00
onError();
2020-04-23 18:28:40 +00:00
} else {
onEndProcess(currentPasscode);
}
break;
case TYPE.ENTER:
onEndProcess(currentPasscode);
break;
default:
break;
}
return '';
}
return currentPasscode;
});
const onPressDelete = () => setPasscode((p) => {
if (p?.length > 0) {
const newPasscode = p.slice(0, -1);
return newPasscode;
}
return '';
});
useImperativeHandle(ref, () => ({
2020-04-23 21:22:08 +00:00
wrongPasscode, animate
2020-04-23 18:28:40 +00:00
}));
return (
2020-04-23 21:22:08 +00:00
<Animatable.View ref={rootRef} style={styles.container}>
2020-04-23 18:28:40 +00:00
<View style={styles.container}>
<View style={styles.viewTitle}>
2020-04-23 20:11:17 +00:00
<Text style={[styles.textTitle, { color: themes[theme].titleText }]}>{title}</Text>
{subtitle ? <Text style={[styles.textSubtitle, { color: themes[theme].bodyText }]}>{subtitle}</Text> : null}
2020-04-23 18:28:40 +00:00
</View>
2020-04-23 20:44:00 +00:00
<Animatable.View ref={dotsRef} style={styles.flexCirclePasscode}>
2020-04-23 18:28:40 +00:00
<Dots passcode={passcode} theme={theme} length={PASSCODE_LENGTH} />
2020-04-23 20:44:00 +00:00
</Animatable.View>
2020-04-23 18:28:40 +00:00
<Grid style={styles.grid}>
<Row style={styles.row}>
{_.range(1, 4).map(i => (
<Col key={i} style={styles.colButtonCircle}>
<Button text={i} theme={theme} onPress={onPressNumber} />
</Col>
))}
</Row>
<Row style={styles.row}>
{_.range(4, 7).map(i => (
<Col key={i} style={styles.colButtonCircle}>
<Button text={i} theme={theme} onPress={onPressNumber} />
</Col>
))}
</Row>
<Row style={styles.row}>
{_.range(7, 10).map(i => (
<Col key={i} style={styles.colButtonCircle}>
<Button text={i} theme={theme} onPress={onPressNumber} />
</Col>
))}
</Row>
<Row style={styles.row}>
<Col style={styles.colButtonCircle} />
<Col style={styles.colButtonCircle}>
<Button text='0' theme={theme} onPress={onPressNumber} />
</Col>
<Col style={styles.colButtonCircle}>
<Button text='X' theme={theme} onPress={onPressDelete} />
</Col>
</Row>
</Grid>
</View>
2020-04-23 20:59:35 +00:00
</Animatable.View>
2020-04-23 18:28:40 +00:00
);
});
2020-04-23 19:37:00 +00:00
Base.propTypes = {
theme: PropTypes.string,
type: PropTypes.string,
previousPasscode: PropTypes.string,
onEndProcess: PropTypes.string
};
2020-04-23 18:28:40 +00:00
export default Base;