Newer
Older
import React, { Component } from 'react'
import { View } from 'react-native'
import { Calendar } from 'react-native-calendars'
import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db'
export default class CalendarView extends Component {
constructor(props) {
super(props)
this.state = {
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate)
}
this.setStateWithCalFormattedDays = (function (CalendarComponent) {
return function() {
CalendarComponent.setState({
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate)
bleedingDaysSortedByDate.addListener(this.setStateWithCalFormattedDays)
bleedingDaysSortedByDate.removeListener(this.setStateWithCalFormattedDays)
const cycleDay = getOrCreateCycleDay(result.dateString)
const navigate = this.props.navigation.navigate
render() {
return (
<View style={styles.container}>
<Calendar
onDayPress={ this.passDateToDayView.bind(this) }
markedDates = { this.state.bleedingDaysInCalFormat }
</View>
)
}
}
function toCalFormat(bleedingDaysSortedByDate) {
const shadesOfRed = ['#ffbaba', '#ff7b7b', '#ff5252', '#ff0000']
return bleedingDaysSortedByDate.reduce((acc, day) => {
acc[day.date] = {
startingDay: true,
endingDay: true,
color: shadesOfRed[day.bleeding.value]
}
return acc
}, {})
}