From bb0a535d65bbc097bdcb5395fad523b4fb33fa16 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 16 Nov 2018 14:24:16 +0100 Subject: [PATCH] Replace getOrCreateCycleDay --- components/app.js | 3 +- components/calendar.js | 5 +- components/chart/day-column.js | 12 +- components/cycle-day/cycle-day-overview.js | 246 +++++++++--------- components/cycle-day/labels.js | 36 +-- components/cycle-day/select-box-group.js | 10 +- .../symptoms/action-button-footer.js | 9 +- components/cycle-day/symptoms/bleeding.js | 12 +- components/cycle-day/symptoms/cervix.js | 17 +- components/cycle-day/symptoms/desire.js | 10 +- components/cycle-day/symptoms/mucus.js | 16 +- components/cycle-day/symptoms/note.js | 11 +- components/cycle-day/symptoms/pain.js | 49 +--- components/cycle-day/symptoms/sex.js | 69 ++--- components/cycle-day/symptoms/temperature.js | 14 +- components/home.js | 6 +- db/index.js | 33 ++- lib/notifications.js | 6 +- 18 files changed, 255 insertions(+), 309 deletions(-) diff --git a/components/app.js b/components/app.js index 8fcfb62..5f7c766 100644 --- a/components/app.js +++ b/components/app.js @@ -56,8 +56,7 @@ export default class App extends Component { if (this.state.currentPage === 'Home') return false if (isSymptomView(this.state.currentPage)) { this.navigate( - this.originForSymptomView, - { cycleDay: this.state.currentProps.cycleDay } + this.originForSymptomView, { date: this.state.currentProps.date } ) } else if(this.state.currentPage === 'CycleDay') { this.navigate(this.menuOrigin) diff --git a/components/calendar.js b/components/calendar.js index fba53c7..791654a 100644 --- a/components/calendar.js +++ b/components/calendar.js @@ -1,7 +1,7 @@ import React, { Component } from 'react' import { CalendarList } from 'react-native-calendars' import {LocalDate} from 'js-joda' -import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db' +import { getBleedingDaysSortedByDate } from '../db' import cycleModule from '../lib/cycle' import {shadesOfRed} from '../styles/index' import styles from '../styles/index' @@ -36,9 +36,8 @@ export default class CalendarView extends Component { } passDateToDayView = (result) => { - const cycleDay = getOrCreateCycleDay(result.dateString) const navigate = this.props.navigate - navigate('CycleDay', { cycleDay }) + navigate('CycleDay', { date: result.dateString }) } render() { return ( diff --git a/components/chart/day-column.js b/components/chart/day-column.js index e5abc0a..8b0e0eb 100644 --- a/components/chart/day-column.js +++ b/components/chart/day-column.js @@ -7,7 +7,6 @@ import { LocalDate } from 'js-joda' import moment from 'moment' import styles from './styles' import config from '../../config' -import { getOrCreateCycleDay } from '../../db' import cycleModule from '../../lib/cycle' import { getCycleDay } from '../../db' import DotAndLine from './dot-and-line' @@ -58,13 +57,8 @@ export default class DayColumn extends Component { ) } - passDateToDayView(dateString) { - const cycleDay = getOrCreateCycleDay(dateString) - this.props.navigate('CycleDay', { cycleDay }) - } - - shouldComponentUpdate(newProps) { - return Object.keys(newProps).some(key => newProps[key] != this.props[key]) + shouldComponentUpdate() { + return false } render() { @@ -232,7 +226,7 @@ export default class DayColumn extends Component { return ( this.passDateToDayView(dateString)} + onPress={() => this.props.navigate('CycleDay', { date: dateString })} activeOpacity={1} > diff --git a/components/cycle-day/cycle-day-overview.js b/components/cycle-day/cycle-day-overview.js index aded1e0..495dac4 100644 --- a/components/cycle-day/cycle-day-overview.js +++ b/components/cycle-day/cycle-day-overview.js @@ -8,7 +8,7 @@ import { import { LocalDate } from 'js-joda' import Svg, { G } from 'react-native-svg' import Header from '../header' -import { getOrCreateCycleDay } from '../../db' +import { getCycleDay } from '../../db' import cycleModule from '../../lib/cycle' import styles from '../../styles' import * as labels from './labels' @@ -29,44 +29,151 @@ const openingLabels = labels.cervix.opening.categories const firmnessLabels = labels.cervix.firmness.categories const positionLabels = labels.cervix.position.categories const intensityLabels = labels.intensity -const sexLabels = labels.sex +const sexLabels = labels.sex.categories +const contraceptiveLabels = labels.contraceptives.categories const painLabels = labels.pain.categories export default class CycleDayOverView extends Component { constructor(props) { super(props) this.state = { - cycleDay: props.cycleDay + date: this.props.date, + cycleDay: getCycleDay(this.props.date) } } goToCycleDay = (target) => { - const localDate = LocalDate.parse(this.state.cycleDay.date) + const localDate = LocalDate.parse(this.state.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, + this.setState({ + date: targetDate, + cycleDay: getCycleDay(targetDate) }) } - render() { + 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)) { + let bleedingLabel = `${bleedingLabels[bleeding.value]}` + if (bleeding.exclude) bleedingLabel = "( " + bleedingLabel + " )" + return bleedingLabel + } + }, + temperature: temperature => { + if (isNumber(temperature.value)) { + 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 => isNumber(mucus[c]))) { + let mucusLabel = [feelingLabels[mucus.feeling], textureLabels[mucus.texture]].join(', ') + mucusLabel += `\n${labels.mucusNFP[mucus.value]}` + if (mucus.exclude) mucusLabel = `(${mucusLabel})` + return mucusLabel + } + }, + cervix: cervix => { + let cervixLabel = [] + if (isNumber(cervix.opening) && isNumber(cervix.firmness)) { + cervixLabel.push( + openingLabels[cervix.opening], + firmnessLabels[cervix.firmness] + ) + if (isNumber(cervix.position)) { + cervixLabel.push(positionLabels[cervix.position]) + } + cervixLabel = cervixLabel.join(', ') + if (cervix.exclude) cervixLabel = `(${cervixLabel})` + return cervixLabel + } + }, + 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 + } + } + } + + const symptomValue = cycleDay[symptomName] + const label = l[symptomName](symptomValue) + if (!label) return + if (label.length < 45) return label + return label.slice(0, 42) + '...' + } + + render() { const getCycleDayNumber = cycleModule().getCycleDayNumber - const cycleDayNumber = getCycleDayNumber(cycleDay.date) + const cycleDayNumber = getCycleDayNumber(this.state.date) const dateInFuture = LocalDate .now() - .isBefore(LocalDate.parse(this.state.cycleDay.date)) + .isBefore(LocalDate.parse(this.state.date)) + return (
@@ -74,7 +181,7 @@ export default class CycleDayOverView extends Component { this.navigate('BleedingEditView')} - data={getLabel('bleeding', cycleDay.bleeding)} + data={this.getLabel('bleeding')} disabled={dateInFuture} > @@ -82,7 +189,7 @@ export default class CycleDayOverView extends Component { this.navigate('TemperatureEditView')} - data={getLabel('temperature', cycleDay.temperature)} + data={this.getLabel('temperature')} disabled={dateInFuture} > @@ -90,7 +197,7 @@ export default class CycleDayOverView extends Component { this.navigate('MucusEditView')} - data={getLabel('mucus', cycleDay.mucus)} + data={this.getLabel('mucus')} disabled={dateInFuture} > @@ -98,7 +205,7 @@ export default class CycleDayOverView extends Component { this.navigate('CervixEditView')} - data={getLabel('cervix', cycleDay.cervix)} + data={this.getLabel('cervix')} disabled={dateInFuture} > @@ -106,7 +213,7 @@ export default class CycleDayOverView extends Component { this.navigate('DesireEditView')} - data={getLabel('desire', cycleDay.desire)} + data={this.getLabel('desire')} disabled={dateInFuture} > @@ -114,7 +221,7 @@ export default class CycleDayOverView extends Component { this.navigate('SexEditView')} - data={getLabel('sex', cycleDay.sex)} + data={this.getLabel('sex')} disabled={dateInFuture} > @@ -122,7 +229,7 @@ export default class CycleDayOverView extends Component { this.navigate('PainEditView')} - data={getLabel('pain', cycleDay.pain)} + data={this.getLabel('pain')} disabled={dateInFuture} > @@ -130,7 +237,7 @@ export default class CycleDayOverView extends Component { this.navigate('NoteEditView')} - data={getLabel('note', cycleDay.note)} + data={this.getLabel('note')} > @@ -144,102 +251,7 @@ export default class CycleDayOverView extends Component { } } -function getLabel(symptomName, symptom) { - const l = { - bleeding: bleeding => { - if (isNumber(bleeding.value)) { - let bleedingLabel = `${bleedingLabels[bleeding.value]}` - if (bleeding.exclude) bleedingLabel = "( " + bleedingLabel + " )" - return bleedingLabel - } - }, - temperature: temperature => { - if (isNumber(temperature.value)) { - 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 => isNumber(mucus[c]))) { - let mucusLabel = [feelingLabels[mucus.feeling], textureLabels[mucus.texture]].join(', ') - mucusLabel += `\n${labels.mucusNFP[mucus.value]}` - if (mucus.exclude) mucusLabel = `(${mucusLabel})` - return mucusLabel - } - }, - cervix: cervix => { - let cervixLabel = [] - if (isNumber(cervix.opening) && isNumber(cervix.firmness)) { - cervixLabel.push( - openingLabels[cervix.opening], - firmnessLabels[cervix.firmness] - ) - if (isNumber(cervix.position)) { - cervixLabel.push(positionLabels[cervix.position]) - } - cervixLabel = cervixLabel.join(', ') - if (cervix.exclude) cervixLabel = `(${cervixLabel})` - return cervixLabel - } - }, - 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]) - } - if(key === 'other' && sex.other) { - let label = sexLabels[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 (!symptom) return - const label = l[symptomName](symptom) - if (label.length < 45) return label - return label.slice(0, 42) + '...' -} class SymptomBox extends Component { diff --git a/components/cycle-day/labels.js b/components/cycle-day/labels.js index b35cf3c..ff7ed56 100644 --- a/components/cycle-day/labels.js +++ b/components/cycle-day/labels.js @@ -35,19 +35,26 @@ export const desire = { } export const sex = { - solo: 'Solo', - partner: 'Partner', - condom: 'Condom', - pill: 'Pill', - iud: 'IUD', - patch: 'Patch', - ring: 'Ring', - implant: 'Implant', - diaphragm: 'Diaphragm', - none: 'None', - other: 'Other', - activityExplainer: 'Were you sexually active today?', - contraceptiveExplainer: 'Did you use contraceptives?' + categories:{ + solo: 'Solo', + partner: 'Partner', + }, + explainer: 'Were you sexually active today?', +} + +export const contraceptives = { + categories:{ + condom: 'Condom', + pill: 'Pill', + iud: 'IUD', + patch: 'Patch', + ring: 'Ring', + implant: 'Implant', + diaphragm: 'Diaphragm', + none: 'None', + other: 'Other', + }, + explainer: 'Did you use contraceptives?' } export const pain = { @@ -59,8 +66,7 @@ export const pain = { nausea: 'Nausea', tenderBreasts: 'Tender breasts', migraine: 'Migraine', - other: 'Other', - note: 'Note', + other: 'Other' }, explainer: 'How did your body feel today?' } diff --git a/components/cycle-day/select-box-group.js b/components/cycle-day/select-box-group.js index 043c5ff..3ff1f58 100644 --- a/components/cycle-day/select-box-group.js +++ b/components/cycle-day/select-box-group.js @@ -10,20 +10,20 @@ export default class SelectBoxGroup extends Component { render() { return ( - {this.props.data.map(({ label, stateKey }) => { + {Object.keys(this.props.labels).map(key => { const style = [styles.selectBox] const textStyle = [] - if (this.props.optionsState[stateKey]) { + if (this.props.optionsState[key]) { style.push(styles.selectBoxActive) textStyle.push(styles.selectBoxTextActive) } return ( this.props.onSelect(stateKey)} - key={stateKey} + onPress={() => this.props.onSelect(key)} + key={key} > - {label} + {this.props.labels[key]} ) diff --git a/components/cycle-day/symptoms/action-button-footer.js b/components/cycle-day/symptoms/action-button-footer.js index b4af8ba..c2bfe29 100644 --- a/components/cycle-day/symptoms/action-button-footer.js +++ b/components/cycle-day/symptoms/action-button-footer.js @@ -11,13 +11,14 @@ export default class ActionButtonFooter extends Component { render() { const { symptom, - cycleDay, + currentSymptomValue, + date, saveAction, saveDisabled, navigate, autoShowDayView = true} = this.props - const navigateToOverView = () => navigate('CycleDay', {cycleDay}) + const navigateToOverView = () => navigate('CycleDay', {date}) const buttons = [ { title: labels.unset, @@ -31,13 +32,13 @@ export default class ActionButtonFooter extends Component { }, { text: labels.reallyUnsetData, onPress: () => { - saveSymptom(symptom, cycleDay) + saveSymptom(symptom, date) navigateToOverView() } }] ) }, - disabledCondition: !cycleDay[symptom], + disabledCondition: !currentSymptomValue, icon: 'delete-outline' }, { title: labels.save, diff --git a/components/cycle-day/symptoms/bleeding.js b/components/cycle-day/symptoms/bleeding.js index 5259b4d..47b140b 100644 --- a/components/cycle-day/symptoms/bleeding.js +++ b/components/cycle-day/symptoms/bleeding.js @@ -14,11 +14,12 @@ import SymptomSection from './symptom-section' export default class Bleeding extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay + const cycleDay = props.cycleDay + this.bleeding = cycleDay && cycleDay.bleeding this.makeActionButtons = props.makeActionButtons this.state = { - currentValue: this.cycleDay.bleeding && this.cycleDay.bleeding.value, - exclude: this.cycleDay.bleeding ? this.cycleDay.bleeding.exclude : false + currentValue: this.bleeding && this.bleeding.value, + exclude: this.bleeding ? this.bleeding.exclude : false } } @@ -57,9 +58,10 @@ export default class Bleeding extends Component { { - saveSymptom('bleeding', this.props.cycleDay, { + saveSymptom('bleeding', this.props.date, { value: this.state.currentValue, exclude: this.state.exclude }) diff --git a/components/cycle-day/symptoms/cervix.js b/components/cycle-day/symptoms/cervix.js index d8e49e4..815787e 100644 --- a/components/cycle-day/symptoms/cervix.js +++ b/components/cycle-day/symptoms/cervix.js @@ -14,14 +14,10 @@ import SymptomSection from './symptom-section' export default class Cervix extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay + const cycleDay = props.cycleDay + this.cervix = cycleDay && cycleDay.cervix this.makeActionButtons = props.makeActionButtons - this.state = { - exclude: this.cycleDay.cervix ? this.cycleDay.cervix.exclude : false, - opening: this.cycleDay.cervix ? this.cycleDay.cervix.opening : null, - firmness: this.cycleDay.cervix ? this.cycleDay.cervix.firmness : null, - position: this.cycleDay.cervix ? this.cycleDay.cervix.position : null - } + this.state = this.cervix ? this.cervix : {} } render() { @@ -87,13 +83,14 @@ export default class Cervix extends Component { { - saveSymptom('cervix', this.cycleDay, { + saveSymptom('cervix', this.props.date, { opening: this.state.opening, firmness: this.state.firmness, position: this.state.position, - exclude: this.state.exclude + exclude: Boolean(this.state.exclude) }) }} saveDisabled={typeof this.state.opening != 'number' || typeof this.state.firmness != 'number'} diff --git a/components/cycle-day/symptoms/desire.js b/components/cycle-day/symptoms/desire.js index 9cbaa1b..d019d48 100644 --- a/components/cycle-day/symptoms/desire.js +++ b/components/cycle-day/symptoms/desire.js @@ -13,9 +13,10 @@ import SymptomSection from './symptom-section' export default class Desire extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay + const cycleDay = props.cycleDay + this.desire = cycleDay && cycleDay.desire this.makeActionButtons = props.makeActionButtons - const desireValue = this.cycleDay.desire && this.cycleDay.desire.value + const desireValue = this.desire && this.desire.value this.state = { currentValue: desireValue } } @@ -41,9 +42,10 @@ export default class Desire extends Component { { - saveSymptom('desire', this.cycleDay, { value: this.state.currentValue }) + saveSymptom('desire', this.props.date, { value: this.state.currentValue }) }} saveDisabled={typeof this.state.currentValue != 'number'} navigate={this.props.navigate} diff --git a/components/cycle-day/symptoms/mucus.js b/components/cycle-day/symptoms/mucus.js index de74709..548a8b3 100644 --- a/components/cycle-day/symptoms/mucus.js +++ b/components/cycle-day/symptoms/mucus.js @@ -16,13 +16,10 @@ import SymptomSection from './symptom-section' export default class Mucus extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay + const cycleDay = props.cycleDay + this.mucus = cycleDay && cycleDay.mucus this.makeActionButtons = props.makeActionButtons - this.state = { - exclude: this.cycleDay.mucus ? this.cycleDay.mucus.exclude : false, - feeling: this.cycleDay.mucus ? this.cycleDay.mucus.feeling : null, - texture: this.cycleDay.mucus ? this.cycleDay.mucus.texture : null - } + this.state = this.mucus ? this.mucus : {} } render() { @@ -75,15 +72,16 @@ export default class Mucus extends Component { { const feeling = this.state.feeling const texture = this.state.texture - saveSymptom('mucus', this.cycleDay, { + saveSymptom('mucus', this.props.date, { feeling, texture, value: computeSensiplanValue(feeling, texture), - exclude: this.state.exclude + exclude: Boolean(this.state.exclude) }) }} saveDisabled={typeof this.state.feeling != 'number' || typeof this.state.texture != 'number'} diff --git a/components/cycle-day/symptoms/note.js b/components/cycle-day/symptoms/note.js index 836c283..99cc624 100644 --- a/components/cycle-day/symptoms/note.js +++ b/components/cycle-day/symptoms/note.js @@ -14,12 +14,12 @@ import { noteExplainer } from '../labels' export default class Note extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay - const note = this.cycleDay.note + const cycleDay = props.cycleDay + this.note = cycleDay && cycleDay.note this.makeActionButtons = props.makeActionButtons this.state = { - currentValue: note && note.value || '' + currentValue: this.note && this.note.value || '' } } @@ -43,9 +43,10 @@ export default class Note extends Component { { - saveSymptom('note', this.cycleDay, { + saveSymptom('note', this.props.date, { value: this.state.currentValue }) }} diff --git a/components/cycle-day/symptoms/pain.js b/components/cycle-day/symptoms/pain.js index 5957d2a..880909e 100644 --- a/components/cycle-day/symptoms/pain.js +++ b/components/cycle-day/symptoms/pain.js @@ -11,43 +11,17 @@ import SelectBoxGroup from '../select-box-group' import SymptomSection from './symptom-section' import styles from '../../../styles' -const categories = labels.categories -const boxes = [{ - label: categories.cramps, - stateKey: 'cramps' -}, { - label: categories.ovulationPain, - stateKey: 'ovulationPain' -}, { - label: categories.headache, - stateKey: 'headache' -}, { - label: categories.backache, - stateKey: 'backache' -}, { - label: categories.nausea, - stateKey: 'nausea' -}, { - label: categories.tenderBreasts, - stateKey: 'tenderBreasts' -}, { - label: categories.migraine, - stateKey: 'migraine' -}, { - label: categories.other, - stateKey: 'other' -}] - export default class Pain extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay - this.state = {} - if (this.cycleDay.pain !== null ) { - Object.assign(this.state, this.cycleDay.pain) - if (this.cycleDay.pain && this.cycleDay.pain.note) { - this.state.other = true - } + const cycleDay = props.cycleDay + if (cycleDay && cycleDay.pain) { + this.state = Object.assign({}, cycleDay.pain) + } else { + this.state = {} + } + if (this.state.note) { + this.state.other = true } } @@ -67,7 +41,7 @@ export default class Pain extends Component { explainer={labels.explainer} > @@ -86,13 +60,14 @@ export default class Pain extends Component { { const copyOfState = Object.assign({}, this.state) if (!copyOfState.other) { copyOfState.note = null } - saveSymptom('pain', this.cycleDay, copyOfState) + saveSymptom('pain', this.props.date, copyOfState) }} saveDisabled={Object.values(this.state).every(value => !value)} navigate={this.props.navigate} diff --git a/components/cycle-day/symptoms/sex.js b/components/cycle-day/symptoms/sex.js index 74ff94f..ec827d6 100644 --- a/components/cycle-day/symptoms/sex.js +++ b/components/cycle-day/symptoms/sex.js @@ -6,61 +6,23 @@ import { } from 'react-native' import styles from '../../../styles' import { saveSymptom } from '../../../db' -import { sex as labels } from '../labels' +import { sex as sexLabels, contraceptives as cLabels } from '../labels' import ActionButtonFooter from './action-button-footer' import SelectBoxGroup from '../select-box-group' import SymptomSection from './symptom-section' -const sexBoxes = [{ - label: labels.solo, - stateKey: 'solo' -}, { - label: labels.partner, - stateKey: 'partner' -}] - -const contraceptiveBoxes = [{ - label: labels.condom, - stateKey: 'condom' -}, { - label: labels.pill, - stateKey: 'pill' -}, { - label: labels.iud, - stateKey: 'iud' -}, { - label: labels.patch, - stateKey: 'patch' -}, { - label: labels.ring, - stateKey: 'ring' -}, { - label: labels.implant, - stateKey: 'implant' -}, { - label: labels.diaphragm, - stateKey: 'diaphragm' -}, { - label: labels.none, - stateKey: 'none' -}, { - label: labels.other, - stateKey: 'other' -}] - export default class Sex extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay - this.state = {} - if (this.cycleDay.sex !== null) { - Object.assign(this.state, this.cycleDay.sex) - // We make sure other is always true when there is a note, - // e.g. when import is messed up. - if (this.cycleDay.sex && this.cycleDay.sex.note) { - this.state.other = true - } + const cycleDay = props.cycleDay + if (cycleDay && cycleDay.sex) { + this.state = Object.assign({}, cycleDay.sex) + } else { + this.state = {} } + // We make sure other is always true when there is a note, + // e.g. when import is messed up. + if (this.state.note) this.state.other = true } toggleState = (key) => { @@ -77,20 +39,20 @@ export default class Sex extends Component { @@ -110,13 +72,14 @@ export default class Sex extends Component { { const copyOfState = Object.assign({}, this.state) if (!copyOfState.other) { copyOfState.note = null } - saveSymptom('sex', this.cycleDay, copyOfState) + saveSymptom('sex', this.props.date, copyOfState) }} saveDisabled={Object.values(this.state).every(value => !value)} navigate={this.props.navigate} diff --git a/components/cycle-day/symptoms/temperature.js b/components/cycle-day/symptoms/temperature.js index 47a3735..8845134 100644 --- a/components/cycle-day/symptoms/temperature.js +++ b/components/cycle-day/symptoms/temperature.js @@ -25,10 +25,11 @@ const minutes = ChronoUnit.MINUTES export default class Temp extends Component { constructor(props) { super(props) - this.cycleDay = props.cycleDay + const cycleDay = props.cycleDay + this.temperature = cycleDay && cycleDay.temperature this.makeActionButtons = props.makeActionButtons - const temp = this.cycleDay.temperature + const temp = this.temperature this.state = { exclude: temp ? temp.exclude : false, @@ -44,7 +45,7 @@ export default class Temp extends Component { this.state.temperature = `${this.state.temperature}.0` } } else { - const prevTemp = getPreviousTemperature(this.cycleDay) + const prevTemp = getPreviousTemperature(this.props.date) if (prevTemp) { this.state.temperature = prevTemp.toString() this.state.isSuggestion = true @@ -59,8 +60,8 @@ export default class Temp extends Component { time: this.state.time, note: this.state.note } - saveSymptom('temperature', this.cycleDay, dataToSave) - this.props.navigate('CycleDay', {cycleDay: this.cycleDay}) + saveSymptom('temperature', this.props.date, dataToSave) + this.props.navigate('CycleDay', {date: this.props.date}) } checkRangeAndSave = () => { @@ -164,7 +165,8 @@ export default class Temp extends Component { this.checkRangeAndSave()} saveDisabled={ this.state.temperature === '' || diff --git a/components/home.js b/components/home.js index e139758..4ebb5d9 100644 --- a/components/home.js +++ b/components/home.js @@ -8,7 +8,7 @@ import { home as labels, bleedingPrediction as predictLabels, shared } from './l import CycleDayIcon from '../assets/home-circle' import Drop from '../assets/home-drop' import cycleModule from '../lib/cycle' -import { getOrCreateCycleDay, getCycleDaysSortedByDate } from '../db' +import { getCycleDaysSortedByDate } from '../db' import { getFertilityStatusForDay } from '../lib/sympto-adapter' import styles from '../styles' import AppText, { AppTextLight } from './app-text' @@ -51,10 +51,8 @@ export default class Home extends Component { } passTodayTo(componentName) { - const todayDateString = LocalDate.now().toString() - const cycleDay = getOrCreateCycleDay(todayDateString) const navigate = this.props.navigate - navigate(componentName, { cycleDay }) + navigate(componentName, { date: LocalDate.now().toString() }) } render() { diff --git a/db/index.js b/db/index.js index 7722ec9..ca9752e 100644 --- a/db/index.js +++ b/db/index.js @@ -68,8 +68,10 @@ export function getCycleDaysSortedByDate() { export function getCycleStartsSortedByDate() { return db.objects('CycleDay').filtered('isCycleStart = true').sorted('date', true) } +export function saveSymptom(symptom, date, val) { + let cycleDay = getCycleDay(date) + if (!cycleDay) cycleDay = createCycleDay(date) -export function saveSymptom(symptom, cycleDay, val) { db.write(() => { if (bleedingValueDeleted(symptom, val)) { cycleDay.bleeding = val @@ -123,28 +125,25 @@ export function updateCycleStartsForAllCycleDays() { }) } -export function getOrCreateCycleDay(localDate) { - let result = db.objectForPrimaryKey('CycleDay', localDate) - if (!result) { - db.write(() => { - result = db.create('CycleDay', { - date: localDate, - isCycleStart: false - }) +export function createCycleDay(dateString) { + let result + db.write(() => { + result = db.create('CycleDay', { + date: dateString, + isCycleStart: false }) - } + }) return result } -export function getCycleDay(localDate) { - return db.objectForPrimaryKey('CycleDay', localDate) +export function getCycleDay(dateString) { + return db.objectForPrimaryKey('CycleDay', dateString) } -export function getPreviousTemperature(cycleDay) { - cycleDay.wrappedDate = LocalDate.parse(cycleDay.date) - const winner = getTemperatureDaysSortedByDate().find(day => { - const wrappedDate = LocalDate.parse(day.date) - return wrappedDate.isBefore(cycleDay.wrappedDate) +export function getPreviousTemperature(date) { + const targetDate = LocalDate.parse(date) + const winner = getTemperatureDaysSortedByDate().find(candidate => { + return LocalDate.parse(candidate.date).isBefore(targetDate) }) if (!winner) return null return winner.temperature.value diff --git a/lib/notifications.js b/lib/notifications.js index 717aad3..6382481 100644 --- a/lib/notifications.js +++ b/lib/notifications.js @@ -3,7 +3,7 @@ import Notification from 'react-native-push-notification' import { LocalDate } from 'js-joda' import Moment from 'moment' import { settings as labels } from '../components/labels' -import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db' +import { getBleedingDaysSortedByDate } from '../db' import cycleModule from './cycle' import nothingChanged from '../db/db-unchanged' @@ -11,9 +11,7 @@ export default function setupNotifications(navigate) { Notification.configure({ onNotification: (notification) => { if (notification.id === '1') { - const todayDateString = LocalDate.now().toString() - const cycleDay = getOrCreateCycleDay(todayDateString) - navigate('TemperatureEditView', { cycleDay }) + navigate('TemperatureEditView', { date: LocalDate.now().toString() }) } else { navigate('Home') }