Skip to content
Snippets Groups Projects
cycle-day-overview.js 6.36 KiB
Newer Older
import React, { Component } from 'react'
import {
  ScrollView,
  View,
Julia Friesel's avatar
Julia Friesel committed
  TouchableOpacity,
  Dimensions
} from 'react-native'
import { LocalDate } from 'js-joda'
import Header from '../header'
import { getOrCreateCycleDay } from '../../db'
import cycleModule from '../../lib/cycle'
import Icon from 'react-native-vector-icons/FontAwesome'
import styles, { iconStyles } from '../../styles'
import {
  bleeding as bleedingLabels,
  mucusFeeling as feelingLabels,
  mucusTexture as textureLabels,
  mucusNFP as computeSensiplanMucusLabels,
emelko's avatar
emelko committed
  cervixOpening as openingLabels,
  cervixFirmness as firmnessLabels,
  cervixPosition as positionLabels,
  intensity as intensityLabels
emelko's avatar
emelko committed
} from './labels/labels'
export default class CycleDayOverView extends Component {
  constructor(props) {
    super(props)
    this.state = {
      cycleDay: props.cycleDay
    }
  }

  goToCycleDay(target) {
    const localDate = LocalDate.parse(this.state.cycleDay.date)
    const targetDate = target === 'before' ?
      localDate.minusDays(1).toString() :
      localDate.plusDays(1).toString()
    this.setState({cycleDay: getOrCreateCycleDay(targetDate)})
  }

  navigate(symptom) {
    this.props.navigate(symptom, {
      cycleDay: this.state.cycleDay,
  render() {
    const cycleDay = this.state.cycleDay
    const getCycleDayNumber = cycleModule().getCycleDayNumber
    const cycleDayNumber = getCycleDayNumber(cycleDay.date)
    return (
      <ScrollView>
        <Header
          isCycleDayOverView={true}
          cycleDayNumber={cycleDayNumber}
          date={cycleDay.date}
        />
        <View style={styles.symptomBoxesView}>
          <SymptomBox
            title='Bleeding'
            onPress={() => this.navigate('BleedingEditView')}
            data={getLabel('bleeding', cycleDay.bleeding)}
          />
          <SymptomBox
            title='Temperature'
            onPress={() => this.navigate('TemperatureEditView')}
            data={getLabel('temperature', cycleDay.temperature)}
          />
          <SymptomBox
            title='Mucus'
            onPress={() => this.navigate('MucusEditView')}
            data={getLabel('mucus', cycleDay.mucus)}
          />
          <SymptomBox
            title='Cervix'
            onPress={() => this.navigate('CervixEditView')}
            data={getLabel('cervix', cycleDay.cervix)}
          />
          <SymptomBox
            title='Note'
            onPress={() => this.navigate('NoteEditView')}
            data={getLabel('note', cycleDay.note)}
          />
          <SymptomBox
            title='Desire'
            onPress={() => this.navigate('DesireEditView')}
            data={getLabel('desire', cycleDay.desire)}
          />
          <SymptomBox
            title='Sex'
            onPress={() => this.navigate('SexEditView')}
            data={getLabel('sex', cycleDay.sex)}
          />
          {/*  this is just to make the last row adhere to the grid
Julia Friesel's avatar
Julia Friesel committed
        (and) because there are no pseudo properties in RN */}
          <FillerBoxes />
        </View >
      </ScrollView >
Julia Friesel's avatar
Julia Friesel committed

function getLabel(symptomName, symptom) {
  const labels = {
    bleeding: bleeding => {
      if (typeof bleeding.value === 'number') {
        let bleedingLabel = `${bleedingLabels[bleeding.value]}`
        if (bleeding.exclude) bleedingLabel = "( " + bleedingLabel + " )"
        return bleedingLabel
      }
    },
    temperature: temperature => {
      if (typeof temperature.value === 'number') {
        let temperatureLabel = `${temperature.value} °C - ${temperature.time}`
        if (temperature.exclude) {
          temperatureLabel = "( " + temperatureLabel + " )"
        }
        return temperatureLabel
      }
    },
    mucus: mucus => {
      const categories = ['feeling', 'texture', 'value']
      if (categories.every(c => typeof mucus[c] === 'number')) {
        let mucusLabel = [feelingLabels[mucus.feeling], textureLabels[mucus.texture]].join(', ')
        mucusLabel += `\n${computeSensiplanMucusLabels[mucus.value]}`
        if (mucus.exclude) mucusLabel = `(${mucusLabel})`
Julia Friesel's avatar
Julia Friesel committed
        return mucusLabel
      }
    },
    cervix: cervix => {
      let cervixLabel = []
Julia Friesel's avatar
Julia Friesel committed
      if (cervix.opening > -1 && cervix.firmness > -1) {
        cervixLabel.push(openingLabels[cervix.opening], firmnessLabels[cervix.firmness])
Bl00dyMarie's avatar
Bl00dyMarie committed
        if (cervix.position > -1) {
          cervixLabel.push(positionLabels[cervix.position])
Bl00dyMarie's avatar
Bl00dyMarie committed
        }
        cervixLabel = cervixLabel.join(', ')
        if (cervix.exclude) cervixLabel = `(${cervixLabel})`
Julia Friesel's avatar
Julia Friesel committed
        return cervixLabel
      }
    },
    note: note => {
      return note.value
    },
    desire: desire => {
      if (typeof desire.value === 'number') {
        const desireLabel = `${intensityLabels[desire.value]}`
        return desireLabel
      }
Bl00dyMarie's avatar
Bl00dyMarie committed
    },
    sex: sex => {
      const sexLabel = []
Bl00dyMarie's avatar
Bl00dyMarie committed
      if ( sex.solo || sex.partner ) {
        sexLabel.push('activity')
Bl00dyMarie's avatar
Bl00dyMarie committed
      }
      if (sex.condom || sex.pill || sex.iud ||
        sex.patch || sex.ring || sex.implant || sex.other) {
        sexLabel.push('contraceptive')
Bl00dyMarie's avatar
Bl00dyMarie committed
      }
      return sexLabel.join(', ')
Julia Friesel's avatar
Julia Friesel committed
  if (!symptom) return
  const label = labels[symptomName](symptom)
  if (label.length < 50) return label
  return label.slice(0, 47) + '...'

class SymptomBox extends Component {
  render() {
    const d = this.props.data
    const boxActive = d ? styles.symptomBoxActive : {}
    const iconActive = d ? iconStyles.symptomBoxActive : {}
    const textStyle = d ? styles.symptomTextActive : {}

    const symptomBoxStyle = Object.assign({}, styles.symptomBox, boxActive)
    const iconStyle = Object.assign({}, iconStyles.symptomBox, iconActive)

    return (
      <TouchableOpacity onPress={this.props.onPress}>
        <View style={symptomBoxStyle}>
          <Icon
            name='thermometer'
            {...iconStyle}
          <Text style={textStyle}>{this.props.title}</Text>
        </View>
        <View style={styles.symptomDataBox}>
          <Text style={styles.symptomDataText}>{this.props.data}</Text>
        </View>
      </TouchableOpacity>
    )
  }
Julia Friesel's avatar
Julia Friesel committed
}

class FillerBoxes extends Component {
  render() {
    const n = Dimensions.get('window').width / styles.symptomBox.width
Julia Friesel's avatar
Julia Friesel committed
    const fillerBoxes = []
    for (let i = 0; i < Math.ceil(n); i++) {
      fillerBoxes.push(
        <View
          width={styles.symptomBox.width}
Julia Friesel's avatar
Julia Friesel committed
          height={0}
          key={i.toString()}
        />
      )
    }
    return fillerBoxes
Julia Friesel's avatar
Julia Friesel committed
  }