Newer
Older
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { CalendarList } from 'react-native-calendars'
import { setDate } from '../slices/date'
import { navigate } from '../slices/navigation'
import { getBleedingDaysSortedByDate } from '../db'
import cycleModule from '../lib/cycle'
import nothingChanged from '../db/db-unchanged'
class CalendarView extends Component {
static propTypes = {
setDate: PropTypes.func.isRequired,
constructor(props) {
super(props)
this.bleedingDays = getBleedingDaysSortedByDate()
const predictedMenses = cycleModule().getPredictedMenses()
bleedingDaysInCalFormat: toCalFormat(this.bleedingDays),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses),
this.bleedingDays.addListener(this.setStateWithCalFormattedDays)
setStateWithCalFormattedDays = (_, changes) => {
if (nothingChanged(changes)) return
const predictedMenses = cycleModule().getPredictedMenses()
this.setState({
bleedingDaysInCalFormat: toCalFormat(this.bleedingDays),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses),
this.bleedingDays.removeListener(this.setStateWithCalFormattedDays)
this.props.setDate(result.dateString)
this.props.navigate('CycleDay')
const {
todayInCalFormat,
bleedingDaysInCalFormat,
} = this.state
const markedDates = Object.assign(
{},
todayInCalFormat,
bleedingDaysInCalFormat,
predictedBleedingDaysInCalFormat
)
<View style={styles.container}>
<CalendarList
// If firstDay=1 week starts from Monday. Note that dayNames and dayNamesShort should still start from Sunday.
firstDay={1}
onDayPress={this.passDateToDayView.bind(this)}
markedDates={markedDates}
// Max amount of months allowed to scroll to the past.
pastScrollRange={120}
const mapDispatchToProps = (dispatch) => {
setDate: (date) => dispatch(setDate(date)),
navigate: (page) => dispatch(navigate(page)),
export default connect(null, mapDispatchToProps)(CalendarView)