Skip to content

Instantly share code, notes, and snippets.

@emanueleDiVizio
Created June 1, 2020 17:22
Show Gist options
  • Save emanueleDiVizio/35ee13a96eddf69f0d36d01686c1ed03 to your computer and use it in GitHub Desktop.
Save emanueleDiVizio/35ee13a96eddf69f0d36d01686c1ed03 to your computer and use it in GitHub Desktop.
const lastShiftSelector = state => state.shifts.slice(-1)[0]
const ShiftButton = () => {
const dispatch = useDispatch()
const currentTime = useCurrentTime()
const lastShift = useSelector(lastShiftSelector)
const [isOnShift, setIsOnShift] = useState(false)
useEffect(() => {
if (lastShift) {
setIsOnShift(currentTime.isBefore(lastShift.endTime))
}
}, [currentTime])
const onUserPressButton = () => {
isOnShift
? dispatch(shiftSlice.actions.endShift())
: dispatch(shiftSlice.actions.startShift())
}
const computeStyles = onShift => ({
backgroundColor: onShift ? config.colors.accent : config.colors.main,
})
return (
<TouchableOpacity
testID="shift-button"
style={[computeStyles(isOnShift), styles.container]}
onPress={onUserPressButton}
>
<Text style={styles.text}>{isOnShift ? 'End Shift' : 'Start shift'}</Text>
</TouchableOpacity>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment