Newer
Older
emelko
committed
import { ChronoUnit, LocalDate } from 'js-joda'
import { ScrollView, View } from 'react-native'
import { connect } from 'react-redux'
import { setDate } from '../slices/date'
emelko
committed
import DripHomeIcon from '../assets/drip-home-icons'
import {
bleedingPrediction as predictLabels,
home as labels
} from '../i18n/en/labels'
import links from '../i18n/en/links'
import cycleModule from '../lib/cycle'
import { getFertilityStatusForDay } from '../lib/sympto-adapter'
emelko
committed
import styles, { cycleDayColor, periodColor, secondaryColor } from '../styles'
import { formatDateForShortText } from './helpers/format-date'
import { getCycleDay } from '../db'
const IconText = ({ children, wrapperStyles }) => {
return (
<View style={[styles.homeIconTextWrapper, wrapperStyles]}>
<AppText style={styles.iconText}>
</View>
)
}
const HomeElement = ({ children, onPress, buttonColor, buttonLabel }) => {
return (
<View style={styles.homeIconAndText}>
{children[0]}
{children[1]}
</View>
<Button
style={styles.homeButton}
onPress={ onPress }
backgroundColor={ buttonColor }>
{ buttonLabel }
</Button>
</View>
class Home extends Component {
const { getCycleDayNumber, getPredictedMenses } = cycleModule()
this.getCycleDayNumber = getCycleDayNumber
this.getBleedingPrediction = getPredictedMenses
this.todayDateString = LocalDate.now().toString()
const prediction = this.getBleedingPrediction()
const fertilityStatus = getFertilityStatusForDay(this.todayDateString)
cycleDayNumber: this.getCycleDayNumber(this.todayDateString),
predictionText: determinePredictionText(prediction),
bleedingPredictionRange: getBleedingPredictionRange(prediction),
...fertilityStatus
setTodayDate = () => {
this.props.setDate(this.todayDateString)
}
navigateToCycleDayView = () => {
this.setTodayDate()
this.props.navigate('CycleDay')
}
navigateToBleedingEditView = () => {
this.setTodayDate()
this.props.navigate(
'BleedingEditView',
getCycleDay(this.todayDateString)
)
const { cycleDayNumber, phase, status } = this.state
const { navigate } = this.props
const cycleDayMoreText = cycleDayNumber ?
labels.cycleDayKnown(cycleDayNumber) :
const { statusText } = this.state
<View style={styles.homeView}>
<HomeElement
onPress={this.navigateToCycleDayView}
buttonColor={ cycleDayColor }
buttonLabel={ labels.editToday }
<DripHomeIcon name="circle" size={80} color={cycleDayColor}/>
<IconText wrapperStyles={styles.wrapperIcon}>
{cycleDayNumber || labels.unknown}
</IconText>
<AppText style={styles.homeDescriptionText}>
{cycleDayMoreText}
</AppText>
onPress={this.navigateToBleedingEditView}
buttonColor={ periodColor }
buttonLabel={ labels.trackPeriod }
<DripHomeIcon name="drop" size={100} color={periodColor} />
<IconText wrapperStyles={{top: '45%', ...styles.wrapperIcon}}>
{this.state.bleedingPredictionRange}
</IconText>
<AppText style={styles.homeDescriptionText}>
{this.state.predictionText}
</AppText>
<HomeElement
onPress={ () => navigate('Chart') }
buttonColor={ secondaryColor }
buttonLabel={ labels.checkFertility }
<View style={styles.homeCircle}/>
<IconText wrapperStyles={styles.wrapperIcon}>
{ phase ? phase.toString() : labels.unknown }
</IconText>
{ phase &&
<AppText style={styles.homeDescriptionText}>
{`${labels.phase(phase)} (${status})`}
</AppText>
<AppText style={styles.homeDescriptionText}>
{ `${statusText} Visit ${links.wiki.url}.` }
</AppText>
const mapDispatchToProps = (dispatch) => {
return({
setDate: (date) => dispatch(setDate(date)),
})
}
export default connect(
null,
mapDispatchToProps,
)(Home)
const todayDate = LocalDate.now()
const predictedBleedingStart = LocalDate.parse(prediction[0][0])
/* the range of predicted bleeding days can be either 3 or 5 */
const predictedBleedingEnd =
LocalDate.parse(prediction[0][ prediction[0].length - 1 ])
const daysToEnd = todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)
return { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd }
}
function determinePredictionText(bleedingPrediction) {
if (!bleedingPrediction.length) return predictLabels.noPrediction
const {
todayDate,
predictedBleedingStart,
predictedBleedingEnd,
daysToEnd
} = getTimes(bleedingPrediction)
todayDate.until(predictedBleedingStart, ChronoUnit.DAYS),
todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)
formatDateForShortText(predictedBleedingStart),
formatDateForShortText(predictedBleedingEnd)
}
if (daysToEnd === 0) {
} else if (daysToEnd === 1) {
return predictLabels.predictionStartedXDaysLeft(daysToEnd)
function getBleedingPredictionRange(prediction) {
if (!prediction.length) return labels.unknown
const {
todayDate,
predictedBleedingStart,
predictedBleedingEnd,
daysToEnd
} = getTimes(prediction)
if (todayDate.isBefore(predictedBleedingStart)) {
return `${todayDate.until(predictedBleedingStart, ChronoUnit.DAYS)}-${todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)}`
return (daysToEnd === 0 ? '0' : `0 - ${daysToEnd}`)