Created
August 3, 2020 05:57
-
-
Save andreferi3/a81c35f85d1e94d4b45b13bb889cfa97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react' | |
| import { View, Text, TouchableOpacity } from 'react-native' | |
| import PropTypes from 'prop-types'; | |
| const Timer = () => { | |
| const [durations, setDurations] = useState({ | |
| minutes: '00', | |
| seconds: '00', | |
| }); | |
| let [increment, setIncrement] = useState(1); | |
| const [timesUp, setTimesUp] = useState(false); | |
| let duration = moment.duration(300 * 1000, 'milliseconds'); | |
| const interval = 1000; | |
| useEffect(() => { | |
| const x = setInterval(() => { | |
| if (duration <= 0) { | |
| setTimesUp(true); | |
| clearInterval(x); | |
| onResendPress; | |
| } else { | |
| duration = moment.duration(duration - interval, 'milliseconds'); | |
| setDurations({ | |
| minutes: duration.minutes(), | |
| seconds: duration.seconds(), | |
| }); | |
| } | |
| }, interval); | |
| }, []); | |
| const onResend = () => { | |
| increment = increment + 1; | |
| setIncrement(increment); | |
| setTimesUp(false); | |
| duration = moment.duration(300 * increment * 1000, 'milliseconds'); | |
| const x = setInterval(() => { | |
| if (duration <= 0) { | |
| setTimesUp(true); | |
| clearInterval(x); | |
| onResendPress; | |
| } else { | |
| duration = moment.duration(duration - interval, 'milliseconds'); | |
| setDurations({ | |
| minutes: duration.minutes(), | |
| seconds: duration.seconds(), | |
| }); | |
| } | |
| }, interval); | |
| }; | |
| return ( | |
| <View style={style.yellowBox}> | |
| <View style={[GlobalStyles.flexRowCenter, style.yellowBoxContent]}> | |
| <Text>Belum menerima kode ? </Text> | |
| <TouchableOpacity disabled={!timesUp} onPress={onResend}> | |
| <Text>Kirim ulang </Text> | |
| </TouchableOpacity> | |
| <Text> | |
| {!timesUp && `(${durations.minutes}:${durations.seconds})`} | |
| </Text> | |
| </View> | |
| </View> | |
| ) | |
| } | |
| Timer.propTypes = { | |
| onResendPress: PropTypes.func, | |
| }; | |
| Timer.defaultProps = { | |
| onResendPress: () => { }, | |
| }; | |
| export default Timer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment