Skip to content
Snippets Groups Projects
cycle-day-overview.js 9.9 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 { getCycleDay } from '../../db'
import cycleModule from '../../lib/cycle'
import styles from '../../styles'
import * as labels from '../../i18n/en/cycle-day'
import { headerTitles as symptomTitles } from '../../i18n/en/labels'
Julia Friesel's avatar
Julia Friesel committed
import AppText from '../app-text'
import DripIcon from '../../assets/drip-icons'
Julia Friesel's avatar
Julia Friesel committed
const bleedingLabels = labels.bleeding.labels
Julia Friesel's avatar
Julia Friesel committed
const intensityLabels = labels.intensity
const sexLabels = labels.sex.categories
const contraceptiveLabels = labels.contraceptives.categories
Julia Friesel's avatar
Julia Friesel committed
const painLabels = labels.pain.categories
const moodLabels = labels.mood.categories
export default class CycleDayOverView extends Component {
  constructor(props) {
    super(props)
    this.state = {
      date: this.props.date,
      cycleDay: getCycleDay(this.props.date)
  goToCycleDay = (target) => {
    const localDate = LocalDate.parse(this.state.date)
    const targetDate = target === 'before' ?
      localDate.minusDays(1).toString() :
      localDate.plusDays(1).toString()
    this.setState({
      date: targetDate,
      cycleDay: getCycleDay(targetDate)
    })
  }

  navigate(symptom) {
    this.props.navigate(symptom, this.state)
  getLabel(symptomName) {
    const cycleDay = this.state.cycleDay
    if (!cycleDay || !cycleDay[symptomName]) return

    const l = {
      bleeding: bleeding => {
        if (isNumber(bleeding.value)) {
Julia Friesel's avatar
Julia Friesel committed
          let bleedingLabel = bleedingLabels[bleeding.value]
          if (bleeding.exclude) bleedingLabel = "( " + bleedingLabel + " )"
          return bleedingLabel
        }
      },
      temperature: temperature => {
        if (isNumber(temperature.value)) {
          let temperatureLabel = `${temperature.value} °C`
          if (temperature.time) {
            temperatureLabel = `${temperatureLabel} - ${temperature.time}`
          }
          if (temperature.exclude) {
            temperatureLabel = "( " + temperatureLabel + " )"
          }
          return temperatureLabel
        }
      },
      mucus: mucus => {
        const filledCategories = ['feeling', 'texture'].filter(c => isNumber(mucus[c]))
        let label = filledCategories.map(category => {
          return labels.mucus.subcategories[category] + ': ' + labels.mucus[category].categories[mucus[category]]
        if (isNumber(mucus.value)) label += `\n => ${labels.mucusNFP[mucus.value]}`
        if (mucus.exclude) label = `(${label})`
        return label
      },
      cervix: cervix => {
        const filledCategories = ['opening', 'firmness', 'position'].filter(c => isNumber(cervix[c]))
        let label = filledCategories.map(category => {
          return labels.cervix.subcategories[category] + ': ' + labels.cervix[category].categories[cervix[category]]
        }).join(', ')

        if (cervix.exclude) label = `(${label})`

        return label
      },
      note: note => {
        return note.value
      },
      desire: desire => {
        if (isNumber(desire.value)) {
          const desireLabel = `${intensityLabels[desire.value]}`
          return desireLabel
        }
      },
      sex: sex => {
        let sexLabel = []
        if (sex && Object.values({...sex}).some(val => val)){
          Object.keys(sex).forEach(key => {
            if(sex[key] && key !== 'other' && key !== 'note') {
              sexLabel.push(
                sexLabels[key] ||
                contraceptiveLabels[key]
              )
            }
            if(key === 'other' && sex.other) {
              let label = contraceptiveLabels[key]
              if(sex.note) {
                label = `${label} (${sex.note})`
              }
              sexLabel.push(label)
            }
          })
          sexLabel = sexLabel.join(', ')
          return sexLabel
        }
      },
      pain: pain => {
        let painLabel = []
        if (pain && Object.values({...pain}).some(val => val)){
          Object.keys(pain).forEach(key => {
            if(pain[key] && key !== 'other' && key !== 'note') {
              painLabel.push(painLabels[key])
            }
            if(key === 'other' && pain.other) {
              let label = painLabels[key]
              if(pain.note) {
                label = `${label} (${pain.note})`
              }
              painLabel.push(label)
            }
          })
          painLabel = painLabel.join(', ')
          return painLabel
        }
        if (mood && Object.values({...mood}).some(val => val)){
          Object.keys(mood).forEach(key => {
            if(mood[key] && key !== 'other' && key !== 'note') {
              moodLabel.push(moodLabels[key])
            }
            if(key === 'other' && mood.other) {
              let label = moodLabels[key]
              if(mood.note) {
                label = `${label} (${mood.note})`
              }
              moodLabel.push(label)
            }
          })
          moodLabel = moodLabel.join(', ')
          return moodLabel
        }
      }
    }

    const symptomValue = cycleDay[symptomName]
    return l[symptomName](symptomValue)
    const getCycleDayNumber = cycleModule().getCycleDayNumber
    const cycleDayNumber = getCycleDayNumber(this.state.date)
Julia Friesel's avatar
Julia Friesel committed
    const dateInFuture = LocalDate
      .now()
      .isBefore(LocalDate.parse(this.state.date))

    return (
      <View style={{ flex: 1 }}>
        <Header
          isCycleDayOverView={true}
          cycleDayNumber={cycleDayNumber}
          date={this.state.date}
          goToCycleDay={this.goToCycleDay}
        <ScrollView>
          <View style={styles.symptomBoxesView}>
            <SymptomBox
              title={symptomTitles.bleeding}
              onPress={() => this.navigate('BleedingEditView')}
              data={this.getLabel('bleeding')}
              iconName='drip-icon-bleeding'
            >
            </SymptomBox>
            <SymptomBox
              title={symptomTitles.temperature}
              onPress={() => this.navigate('TemperatureEditView')}
              data={this.getLabel('temperature')}
              iconName='drip-icon-temperature'
            >
            </SymptomBox>
            <SymptomBox
              title={symptomTitles.mucus}
              onPress={() => this.navigate('MucusEditView')}
              data={this.getLabel('mucus')}
              iconName='drip-icon-mucus'
            <SymptomBox
              title={symptomTitles.cervix}
              onPress={() => this.navigate('CervixEditView')}
              data={this.getLabel('cervix')}
              iconName='drip-icon-cervix'
            >
            </SymptomBox>
            <SymptomBox
              title={symptomTitles.desire}
              onPress={() => this.navigate('DesireEditView')}
              data={this.getLabel('desire')}
              iconName='drip-icon-desire'
            >
            </SymptomBox>
            <SymptomBox
              title={symptomTitles.sex}
              onPress={() => this.navigate('SexEditView')}
              data={this.getLabel('sex')}
              iconName='drip-icon-sex'
            >
            </SymptomBox>
              title={symptomTitles.pain}
              onPress={() => this.navigate('PainEditView')}
              data={this.getLabel('pain')}
              iconName='drip-icon-pain'
            >
            </SymptomBox>
              title={symptomTitles.mood}
              onPress={() => this.navigate('MoodEditView')}
              data={this.getLabel('mood')}
              disabled={dateInFuture}
Sofiya Tepikin's avatar
Sofiya Tepikin committed
              iconName='drip-icon-mood'
              title={symptomTitles.note}
              onPress={() => this.navigate('NoteEditView')}
              data={this.getLabel('note')}
              iconName='drip-icon-note'
            >
            </SymptomBox>
            {/*  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 />
class SymptomBox extends Component {
  render() {
Sofiya Tepikin's avatar
Sofiya Tepikin committed
    const hasData = this.props.data
    const boxActive = hasData ? styles.symptomBoxActive : {}
    const textActive = hasData ? styles.symptomTextActive : {}
    const disabledStyle = this.props.disabled ? styles.symptomInFuture : {}
    return (
Julia Friesel's avatar
Julia Friesel committed
      <TouchableOpacity
        onPress={this.props.onPress}
        disabled={this.props.disabled}
      >
Julia Friesel's avatar
Julia Friesel committed
        <View style={[styles.symptomBox, boxActive, disabledStyle]}>
Sofiya Tepikin's avatar
Sofiya Tepikin committed
          <DripIcon name={this.props.iconName} size={50} color={hasData ? 'white' : 'black'}/>
          <AppText
            style={[textActive, disabledStyle, {fontSize: 15}]}
            numberOfLines={1}
          >
            {this.props.title.toLowerCase()}
Julia Friesel's avatar
Julia Friesel committed
          </AppText>
        </View>
Julia Friesel's avatar
Julia Friesel committed
        <View style={[styles.symptomDataBox, disabledStyle]}>
          <AppText
            style={styles.symptomDataText}
            numberOfLines={3}
          >{this.props.data}</AppText>
        </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
  }

function isNumber(val) {
  return typeof val === 'number'