From 8a6f943e5f7c93caf941cb0cc6b97f4d3b6ab2be Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Sun, 26 Aug 2018 19:34:52 +0200
Subject: [PATCH] Separate warning out of absolute range

---
 components/cycle-day/action-buttons.js        | 48 --------------
 components/cycle-day/labels/labels.js         |  1 +
 .../symptoms/action-button-footer.js          | 11 +++-
 components/cycle-day/symptoms/temperature.js  | 64 +++++++++++++------
 components/labels.js                          |  1 +
 5 files changed, 55 insertions(+), 70 deletions(-)
 delete mode 100644 components/cycle-day/action-buttons.js

diff --git a/components/cycle-day/action-buttons.js b/components/cycle-day/action-buttons.js
deleted file mode 100644
index 9ddb036e..00000000
--- a/components/cycle-day/action-buttons.js
+++ /dev/null
@@ -1,48 +0,0 @@
-import React from 'react'
-import {
-  View,
-  Button,
-} 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)
-      },
-      {
-        title: 'Delete',
-        action: () => {
-          saveSymptom(symptom, cycleDay)
-          showView(dayView)
-        }
-      }, {
-        title: 'Save',
-        action: async () => {
-          await saveAction()
-          showView(dayView)
-        },
-        disabledCondition: saveDisabled
-      }
-    ]
-
-    return buttons.map(({ title, action, disabledCondition }, i) => {
-      const style = { flex: 1, marginHorizontal: 10 }
-      if (i === 0) style.marginLeft = 0
-      if (i === buttons.length - 1) style.marginRight = 0
-      return (
-        <View style={style} key={i}>
-          <Button
-            onPress={action}
-            disabled={disabledCondition}
-            title={title}>
-          </Button>
-        </View >
-      )
-    })
-  }
-}
\ No newline at end of file
diff --git a/components/cycle-day/labels/labels.js b/components/cycle-day/labels/labels.js
index 910e6c96..6650e335 100644
--- a/components/cycle-day/labels/labels.js
+++ b/components/cycle-day/labels/labels.js
@@ -29,5 +29,6 @@ export const fertilityStatus = {
 
 export const temperature = {
   outOfRangeWarning: 'This temperature value is out of the current range for the temperature chart. You can change the range in the settings.',
+  outOfAbsoluteRangeWarning: 'This temperature value is too high or low to be shown on the temperature chart.',
   saveAnyway: 'Save anyway'
 }
diff --git a/components/cycle-day/symptoms/action-button-footer.js b/components/cycle-day/symptoms/action-button-footer.js
index a1534964..dac39a6a 100644
--- a/components/cycle-day/symptoms/action-button-footer.js
+++ b/components/cycle-day/symptoms/action-button-footer.js
@@ -8,7 +8,14 @@ import styles, {iconStyles} from '../../../styles'
 
 export default class ActionButtonFooter extends Component {
   render() {
-    const { symptom, cycleDay, saveAction, saveDisabled, navigate} = this.props
+    const {
+      symptom,
+      cycleDay,
+      saveAction,
+      saveDisabled,
+      navigate,
+      autoShowDayView = true}
+      = this.props
     const navigateToOverView = () => navigate('CycleDay', {cycleDay})
     const buttons = [
       {
@@ -28,7 +35,7 @@ export default class ActionButtonFooter extends Component {
         title: 'Save',
         action: () => {
           saveAction()
-          navigateToOverView()
+          if (autoShowDayView) navigateToOverView()
         },
         disabledCondition: saveDisabled,
         icon: 'content-save-outline'
diff --git a/components/cycle-day/symptoms/temperature.js b/components/cycle-day/symptoms/temperature.js
index fda52006..11ee7dae 100644
--- a/components/cycle-day/symptoms/temperature.js
+++ b/components/cycle-day/symptoms/temperature.js
@@ -17,6 +17,7 @@ import { temperature as tempLabels } from '../labels/labels'
 import { scaleObservable } from '../../../local-storage'
 import { shared } from '../../labels'
 import ActionButtonFooter from './action-button-footer'
+import config from '../../../config'
 
 const minutes = ChronoUnit.MINUTES
 
@@ -49,6 +50,47 @@ export default class Temp extends Component {
     }
   }
 
+  saveTemperature = () => {
+    const dataToSave = {
+      value: Number(this.state.temperature),
+      exclude: this.state.exclude,
+      time: this.state.time
+    }
+    saveSymptom('temperature', this.cycleDay, dataToSave)
+    this.props.navigate('CycleDay', {cycleDay: this.cycleDay})
+  }
+
+  checkRangeAndSave = () => {
+    const value = Number(this.state.temperature)
+
+    const absolute = {
+      min: config.temperatureScale.min,
+      max: config.temperatureScale.max
+    }
+    const scale = scaleObservable.value
+    let warningMsg
+    if (value < absolute.min || value > absolute.max) {
+      warningMsg = tempLabels.outOfAbsoluteRangeWarning
+    } else if (value < scale.min || value > scale.max) {
+      warningMsg = tempLabels.outOfRangeWarning
+    }
+
+    if (warningMsg) {
+      Alert.alert(
+        shared.warning,
+        warningMsg,
+        [
+          { text: shared.cancel },
+          { text: shared.save, onPress: this.saveTemperature}
+        ]
+      )
+    } else {
+      this.saveTemperature()
+    }
+
+  }
+
+
   render() {
     return (
       <View style={{ flex: 1 }}>
@@ -98,20 +140,14 @@ export default class Temp extends Component {
         <ActionButtonFooter
           symptom='temperature'
           cycleDay={this.cycleDay}
-          saveAction={() => {
-            const dataToSave = {
-              value: Number(this.state.temperature),
-              exclude: this.state.exclude,
-              time: this.state.time
-            }
-            saveSymptom('temperature', this.cycleDay, dataToSave)
-          }}
+          saveAction={() => this.checkRangeAndSave()}
           saveDisabled={
             this.state.temperature === '' ||
             isNaN(Number(this.state.temperature)) ||
             isInvalidTime(this.state.time)
           }
           navigate={this.props.navigate}
+          autoShowDayView={false}
         />
       </View>
     )
@@ -119,18 +155,6 @@ export default class Temp extends Component {
 }
 
 class TempInput extends Component {
-  checkRange = () => {
-    const value = Number(this.props.value)
-    if (isNaN(value)) return
-    const scale = scaleObservable.value
-    if (value < scale.min || value > scale.max) {
-      Alert.alert(
-        shared.warning,
-        tempLabels.outOfRangeWarning,
-      )
-    }
-  }
-
   render() {
     const style = [styles.temperatureTextInput]
     if (this.props.isSuggestion) {
diff --git a/components/labels.js b/components/labels.js
index 28cd3939..b249e378 100644
--- a/components/labels.js
+++ b/components/labels.js
@@ -1,5 +1,6 @@
 export const shared = {
   cancel: 'Cancel',
+  save: 'Save',
   errorTitle: 'Error',
   successTitle: 'Success',
   warning: 'Warning'
-- 
GitLab