enhanced points & checkbox convert to hooks

This commit is contained in:
ue 2019-10-19 00:07:34 +03:00
parent efefe67ebd
commit 086403527a
8 changed files with 151 additions and 221 deletions

View File

@ -1,30 +1,19 @@
import React, { Component } from 'react'; import React, { useState } from 'react';
import { View, TouchableOpacity } from 'react-native'; import { View, TouchableOpacity } from 'react-native';
import styles from './checkboxStyles'; import styles from './checkboxStyles';
class CheckBoxView extends Component { const CheckBoxView = ({ clicked, value, isChecked, style, locked }) => {
constructor(props) { const [isCheck, setIsCheck] = useState(false);
super(props);
this.state = { isCheck: false }; const _checkClicked = () => {
setIsCheck(!isCheck);
if (clicked) {
clicked(value, !isCheck);
} }
_checkClicked = async () => {
const { clicked, value } = this.props;
await this.setState(prevState => ({
isCheck: !prevState.isCheck,
}));
const { isCheck } = this.state;
if (clicked) clicked(value, isCheck);
}; };
render() {
const { style, isChecked, locked } = this.props;
const { isCheck } = this.state;
if (locked) { if (locked) {
return ( return (
<View style={styles.bigSquare}> <View style={styles.bigSquare}>
@ -32,14 +21,14 @@ class CheckBoxView extends Component {
</View> </View>
); );
} }
return ( return (
<TouchableOpacity onPress={this._checkClicked} style={style}> <TouchableOpacity onPress={_checkClicked} style={style}>
<View style={styles.bigSquare}> <View style={styles.bigSquare}>
<View style={[styles.smallSquare, isCheck && styles.checked]} /> <View style={[styles.smallSquare, isCheck && styles.checked]} />
</View> </View>
</TouchableOpacity> </TouchableOpacity>
); );
} };
}
export default CheckBoxView; export default CheckBoxView;

View File

@ -1,3 +0,0 @@
import ErrorBoundary from './view/errorBoundaryView';
export { ErrorBoundary };

View File

@ -1,32 +0,0 @@
import React, { Component, Fragment } from 'react';
import Text from 'react-native';
export default class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error, info) {
if (__DEV__) {
return;
}
this.setState({ hasError: true, info, error });
}
render() {
const { hasError, info, error } = this.state;
const { children } = this.props;
if (hasError) {
return (
<Fragment>
<Text>Something went wrong.</Text>
<Text>{error}</Text>
<Text>{info}</Text>
</Fragment>
);
}
return children;
}
}

View File

@ -3,6 +3,7 @@ import { Provider, connect } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react'; import { PersistGate } from 'redux-persist/integration/react';
import { IntlProvider } from 'react-intl'; import { IntlProvider } from 'react-intl';
import { useScreens } from 'react-native-screens'; import { useScreens } from 'react-native-screens';
import { setTopLevelNavigator } from './navigation/service';
import { flattenMessages } from './utils/flattenMessages'; import { flattenMessages } from './utils/flattenMessages';
import messages from './config/locales'; import messages from './config/locales';
@ -29,7 +30,11 @@ const App = connect(mapStateToProps)(_renderApp);
export default () => { export default () => {
return ( return (
<Provider store={store}> <Provider store={store}>
<App /> <App
ref={navigatorRef => {
setTopLevelNavigator(navigatorRef);
}}
/>
</Provider> </Provider>
); );
}; };

16
src/navigation/service.js Normal file
View File

@ -0,0 +1,16 @@
import { NavigationActions } from 'react-navigation';
let _navigator;
export const setTopLevelNavigator = navigatorRef => {
_navigator = navigatorRef;
};
export const navigate = (routeName, params) => {
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
}),
);
};

View File

@ -1,4 +1,6 @@
import React, { PureComponent } from 'react'; import React, { useState } from 'react';
import { useIntl } from 'react-intl';
import { Text, TouchableOpacity, View } from 'react-native'; import { Text, TouchableOpacity, View } from 'react-native';
import { NumericKeyboard, PinAnimatedInput } from '../../../components'; import { NumericKeyboard, PinAnimatedInput } from '../../../components';
@ -6,53 +8,42 @@ import { UserAvatar } from '../../../components';
import styles from './pinCodeStyles'; import styles from './pinCodeStyles';
class PinCodeScreen extends PureComponent { const PinCodeScreen = ({
constructor(props) { informationText,
super(props); showForgotButton,
this.state = { username,
pin: '', handleForgotButton,
loading: false, setPinCode,
}; }) => {
} const [pin, setPin] = useState('');
const [loading, setLoading] = useState(false);
const intl = useIntl();
// Component Life Cycles const _handleKeyboardOnPress = async value => {
// Component Functions
_handleKeyboardOnPress = async value => {
const { setPinCode } = this.props;
const { pin, loading } = this.state;
if (loading) { if (loading) {
return; return;
} }
if (value === 'clear') { if (value === 'clear') {
this.setState({ pin: '' }); setPin('');
return; return;
} }
const newPin = `${pin}${value}`; const newPin = `${pin}${value}`;
if (pin.length < 3) { if (pin.length < 3) {
this.setState({ pin: newPin }); setPin(newPin);
} else if (pin.length === 3) { } else if (pin.length === 3) {
await this.setState({ pin: newPin, loading: true }); await setPin(newPin);
await setLoading(true);
setPinCode(`${pin}${value}`) await setPinCode(`${pin}${value}`);
.then(() => {
// TODO: fix unmounted component error setPin('');
this.setState({ pin: '', loading: false }); setLoading(false);
})
.catch(() => {
this.setState({ pin: '', loading: false });
});
} else if (pin.length > 3) { } else if (pin.length > 3) {
this.setState({ pin: `${value}` }); setPin(`${value}`);
} }
}; };
render() {
const { informationText, showForgotButton, username, intl, handleForgotButton } = this.props;
const { pin, loading } = this.state;
return ( return (
<View style={styles.container}> <View style={styles.container}>
<View style={styles.logoView}> <View style={styles.logoView}>
@ -68,7 +59,7 @@ class PinCodeScreen extends PureComponent {
<PinAnimatedInput pin={pin} loading={loading} /> <PinAnimatedInput pin={pin} loading={loading} />
</View> </View>
<View style={styles.numericKeyboardView}> <View style={styles.numericKeyboardView}>
<NumericKeyboard onPress={this._handleKeyboardOnPress} /> <NumericKeyboard onPress={_handleKeyboardOnPress} />
</View> </View>
{showForgotButton ? ( {showForgotButton ? (
<TouchableOpacity onPress={() => handleForgotButton()} style={styles.forgotButtonView}> <TouchableOpacity onPress={() => handleForgotButton()} style={styles.forgotButtonView}>
@ -83,7 +74,6 @@ class PinCodeScreen extends PureComponent {
)} )}
</View> </View>
); );
} };
}
export default PinCodeScreen; export default PinCodeScreen;

View File

@ -1,4 +1,4 @@
import React, { PureComponent } from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
// Component // Component
@ -7,33 +7,14 @@ import PointsScreen from '../screen/pointsScreen';
// Constants // Constants
import ROUTES from '../../../constants/routeNames'; import ROUTES from '../../../constants/routeNames';
/* const PointsContainer = ({ isLoggedIn, navigation }) => {
* Props Name Description Value const _handleOnPressLogin = () => {
*@props --> props name here description here Value Type Here
*
*/
class PointsContainer extends PureComponent {
constructor(props) {
super(props);
this.state = {};
}
// Component Life Cycle Functions
// Component Functions
_handleOnPressLogin = () => {
const { navigation } = this.props;
navigation.navigate(ROUTES.SCREENS.LOGIN); navigation.navigate(ROUTES.SCREENS.LOGIN);
}; };
render() { return <PointsScreen isLoggedIn={isLoggedIn} handleLoginPress={_handleOnPressLogin} />;
const { isLoggedIn } = this.props; };
return <PointsScreen isLoggedIn={isLoggedIn} handleLoginPress={this._handleOnPressLogin} />;
}
}
const matStateToProps = state => ({ const matStateToProps = state => ({
isLoggedIn: state.application.isLoggedIn, isLoggedIn: state.application.isLoggedIn,
}); });

View File

@ -1,6 +1,6 @@
import React, { PureComponent, Fragment } from 'react'; import React, { Fragment } from 'react';
import { injectIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { View, SafeAreaView } from 'react-native'; import { SafeAreaView } from 'react-native';
// Containers // Containers
import { PointsContainer } from '../../../containers'; import { PointsContainer } from '../../../containers';
@ -11,23 +11,8 @@ import { Header, Points, NoPost } from '../../../components';
// Styles // Styles
import styles from './pointsStyles'; import styles from './pointsStyles';
class PointsScreen extends PureComponent { const PointsScreen = ({ isLoggedIn, handleLoginPress }) => {
/* Props const intl = useIntl();
* ------------------------------------------------
* @prop { type } name - Description....
*/
constructor(props) {
super(props);
this.state = {};
}
// Component Life Cycles
// Component Functions
render() {
const { intl, isLoggedIn, handleLoginPress } = this.props;
return ( return (
<Fragment> <Fragment>
@ -72,7 +57,6 @@ class PointsScreen extends PureComponent {
</SafeAreaView> </SafeAreaView>
</Fragment> </Fragment>
); );
} };
}
export default injectIntl(PointsScreen); export default PointsScreen;