Skip to content
Snippets Groups Projects
cycle-day-overview.js 5.87 KiB
Newer Older
import React, { Component } from 'react'
import {
  View,
  Text,
  Image,
Julia Friesel's avatar
Julia Friesel committed
  TouchableOpacity,
  Dimensions
} from 'react-native'
import styles 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'
import cycleDayModule from '../../lib/cycle'
emelko's avatar
emelko committed
import { bleedingDaysSortedByDate } from '../../db'
const getCycleDayNumber = cycleDayModule().getCycleDayNumber

export default class DayView extends Component {
  constructor(props) {
    super(props)
    this.cycleDay = props.cycleDay
    this.showView = props.showView
    this.state = {
      cycleDayNumber: getCycleDayNumber(this.cycleDay.date),
    }

Julia Friesel's avatar
Julia Friesel committed
    this.setStateWithCycleDayNumber = (function (DayViewComponent) {
      return function () {
        DayViewComponent.setState({
          cycleDayNumber: getCycleDayNumber(DayViewComponent.cycleDay.date)
        })
      }
    })(this)

Julia Friesel's avatar
Julia Friesel committed
    bleedingDaysSortedByDate.addListener(this.setStateWithCycleDayNumber)
  }

  componentWillUnmount() {
Julia Friesel's avatar
Julia Friesel committed
    bleedingDaysSortedByDate.removeListener(this.setStateWithCycleDayNumber)
Julia Friesel's avatar
Julia Friesel committed
    const cycleDay = this.cycleDay
    return (
Julia Friesel's avatar
Julia Friesel committed
      <View style={styles.symptomBoxesView}>
        <SymptomBox
          title='Bleeding'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('BleedingEditView')}
          data={getLabel('bleeding', cycleDay.bleeding)}
        />
        <SymptomBox
          title='Temperature'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('TemperatureEditView')}
          data={getLabel('temperature', cycleDay.temperature)}
        />
        <SymptomBox
          title='Mucus'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('MucusEditView')}
          data={getLabel('mucus', cycleDay.mucus)}
        />
        <SymptomBox
          title='Cervix'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('CervixEditView')}
          data={getLabel('cervix', cycleDay.cervix)}
        />
        <SymptomBox
          title='Note'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('NoteEditView')}
          data={getLabel('note', cycleDay.note)}
        />
        <SymptomBox
          title='Desire'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('DesireEditView')}
          data={getLabel('desire', cycleDay.desire)}
        />
        <SymptomBox
          title='Sex'
          icon={require('./assets/placeholder.png')}
          onPress={() => this.showView('SexEditView')}
          data={getLabel('sex', cycleDay.sex)}
        />
        {/*  this is just to make the last row adhere to the grid
        (and) because there are no pseudo properties in RN */}
        <FillerBoxes/>
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 => {
      if (
        typeof mucus.feeling === 'number' &&
        typeof mucus.texture === 'number' &&
        typeof mucus.value === 'number'
      ) {
Bl00dyMarie's avatar
Bl00dyMarie committed
        let mucusLabel =
          `${feelingLabels[mucus.feeling]} +
          ${textureLabels[mucus.texture]}
          ( ${computeSensiplanMucusLabels[mucus.value]} )`
Julia Friesel's avatar
Julia Friesel committed
        if (mucus.exclude) mucusLabel = "( " + mucusLabel + " )"
        return mucusLabel
      }
    },
    cervix: cervix => {
      if (cervix.opening > -1 && cervix.firmness > -1) {
Bl00dyMarie's avatar
Bl00dyMarie committed
        let cervixLabel =
          `${openingLabels[cervix.opening]} +
          ${firmnessLabels[cervix.firmness]}`
        if (cervix.position > -1) {
          cervixLabel += `+ ${positionLabels[cervix.position]}`
        }
Julia Friesel's avatar
Julia Friesel committed
        if (cervix.exclude) cervixLabel = "( " + cervixLabel + " )"
        return cervixLabel
      }
    },
    note: note => {
      return note.value.slice(0, 12) + '...'
    },
    desire: desire => {
      if (typeof desire.value === 'number') {
        const desireLabel = `${intensityLabels[desire.value]}`
        return desireLabel
      }
Bl00dyMarie's avatar
Bl00dyMarie committed
    },
    sex: sex => {
      let sexLabel = ''
      if ( sex.solo || sex.partner ) {
        sexLabel += 'Activity '
      }
      if (sex.condom || sex.pill || sex.iud ||
        sex.patch || sex.ring || sex.implant || sex.other) {
        sexLabel += 'Contraceptive'
      }
Julia Friesel's avatar
Julia Friesel committed
      return sexLabel ? sexLabel : undefined
Julia Friesel's avatar
Julia Friesel committed
  if (!symptom) return
  return labels[symptomName](symptom)

class SymptomBox extends Component {
  render() {
    return (
      <TouchableOpacity onPress={this.props.onPress}>
        <View style={styles.symptomBox}>
          <Image
            source={require('./assets/placeholder.png')}
            style={styles.symptomBoxImage}
          />
          <Text>{this.props.title}</Text>
          <Text>{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.minHeight
    return Array(Math.ceil(n)).fill(
      <View
        width={styles.symptomBox.minHeight}
        height={0}
        key={Math.random().toString()}
      />
    )
  }