From 310683c5f80727703130867cd86bbaad6445f1b7 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Thu, 2 Aug 2018 09:47:21 +0200 Subject: [PATCH] Programmatically add props to cycle day views --- components/cycle-day/action-buttons.js | 8 ++-- components/cycle-day/cycle-day-overview.js | 10 ++--- components/cycle-day/index.js | 44 ++++++++++++---------- components/cycle-day/symptoms/index.js | 15 ++++++++ 4 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 components/cycle-day/symptoms/index.js diff --git a/components/cycle-day/action-buttons.js b/components/cycle-day/action-buttons.js index eb33201..78e2479 100644 --- a/components/cycle-day/action-buttons.js +++ b/components/cycle-day/action-buttons.js @@ -5,24 +5,26 @@ import { } from 'react-native' import { saveSymptom } from '../../db' +const dayView = 'DayView' + export default function (showView) { return function ({ symptom, cycleDay, saveAction, saveDisabled}) { const buttons = [ { title: 'Cancel', - action: () => showView('dayView') + action: () => showView(dayView) }, { title: 'Delete', action: () => { saveSymptom(symptom, cycleDay) - showView('dayView') + showView(dayView) } }, { title: 'Save', action: () => { saveAction() - showView('dayView') + showView(dayView) }, disabledCondition: saveDisabled } diff --git a/components/cycle-day/cycle-day-overview.js b/components/cycle-day/cycle-day-overview.js index 0b476f5..fc4ebbf 100644 --- a/components/cycle-day/cycle-day-overview.js +++ b/components/cycle-day/cycle-day-overview.js @@ -52,7 +52,7 @@ export default class DayView extends Component { Bleeding @@ -61,7 +61,7 @@ export default class DayView extends Component { Temperature @@ -70,7 +70,7 @@ export default class DayView extends Component { Mucus @@ -79,7 +79,7 @@ export default class DayView extends Component { Cervix @@ -88,7 +88,7 @@ export default class DayView extends Component { Note diff --git a/components/cycle-day/index.js b/components/cycle-day/index.js index 7017583..94bb51e 100644 --- a/components/cycle-day/index.js +++ b/components/cycle-day/index.js @@ -6,16 +6,11 @@ import { } from 'react-native' import cycleModule from '../../lib/cycle' import { getFertilityStatusStringForDay } from '../../lib/sympto-adapter' -import DayView from './cycle-day-overview' -import BleedingEditView from './symptoms/bleeding' -import TemperatureEditView from './symptoms/temperature' -import MucusEditView from './symptoms/mucus' -import CervixEditView from './symptoms/cervix' -import NoteEditView from './symptoms/note' -import DesireEditView from './symptoms/desire' import { formatDateForViewHeader } from './labels/format' import styles from '../../styles' import actionButtonModule from './action-buttons' +import symptomComponents from './symptoms' +import DayView from './cycle-day-overview' const getCycleDayNumber = cycleModule().getCycleDayNumber @@ -25,14 +20,32 @@ export default class Day extends Component { this.cycleDay = props.navigation.state.params.cycleDay this.state = { - visibleComponent: 'dayView', + visibleComponent: 'DayView', } - this.showView = view => { + const showView = view => { this.setState({visibleComponent: view}) } - this.makeActionButtons = actionButtonModule(this.showView) + const makeActionButtons = actionButtonModule(showView) + + const symptomComponentNames = Object.keys(symptomComponents) + this.cycleDayViews = symptomComponentNames.reduce((acc, curr) => { + acc[curr] = React.createElement( + symptomComponents[curr], + { + cycleDay: this.cycleDay, + makeActionButtons + } + ) + return acc + }, {}) + + // DayView needs showView instead of makeActionButtons + this.cycleDayViews.DayView = React.createElement(DayView, { + cycleDay: this.cycleDay, + showView + }) } render() { @@ -56,16 +69,7 @@ export default class Day extends Component { - { - { dayView: , - bleedingEditView: , - temperatureEditView: , - mucusEditView: , - cervixEditView: , - noteEditView: , - desireEditView: - }[this.state.visibleComponent] - } + { this.cycleDayViews[this.state.visibleComponent] } ) diff --git a/components/cycle-day/symptoms/index.js b/components/cycle-day/symptoms/index.js new file mode 100644 index 0000000..029bb89 --- /dev/null +++ b/components/cycle-day/symptoms/index.js @@ -0,0 +1,15 @@ +import BleedingEditView from './bleeding' +import TemperatureEditView from './temperature' +import MucusEditView from './mucus' +import CervixEditView from './cervix' +import NoteEditView from './note' +import DesireEditView from './symptoms/desire' + +export default { + BleedingEditView, + TemperatureEditView, + MucusEditView, + CervixEditView, + NoteEditView, + DesireEditView +} \ No newline at end of file