From 4cd81dbecf71265c421ee2ca8f5421070370cb90 Mon Sep 17 00:00:00 2001 From: Nicolas Azari Date: Tue, 17 Mar 2020 20:00:22 +0100 Subject: [PATCH] :heart: changes :heart_eyes: --- components/FindButton.js | 18 +++--------------- components/LifeinaBoxStats.js | 3 +-- model/temperature-model.js | 8 +++++++- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/components/FindButton.js b/components/FindButton.js index 594ac79..0eed40e 100644 --- a/components/FindButton.js +++ b/components/FindButton.js @@ -1,10 +1,6 @@ import React from 'react' import { useStoreState, useStoreActions } from 'easy-peasy' -import { - lastBatteryBuffer, - lastTemperatureBuffer, - celciusToFahrenheit -} from '../lib/' +import { lastBatteryBuffer, lastTemperatureBuffer } from '../lib/' let device = null let myCharacteristicNotify = null @@ -26,9 +22,7 @@ const FindButton = () => { const resetDevice = useStoreActions(actions => actions.device.reset) // Temperature Model - const addTemperature = useStoreActions( - actions => actions.temperature.addValue - ) + const { prepareValue } = useStoreActions(actions => actions.temperature) const resetTemperature = useStoreActions(actions => actions.temperature.reset) // Battery Model const addBatteryValue = useStoreActions(actions => actions.battery.addValue) @@ -54,13 +48,7 @@ const FindButton = () => { const hex2dec = parseInt(a[2], 16) const celcius = hex2dec / 10 - console.log(unit) - console.log(celcius) - console.log(celciusToFahrenheit(celcius)) - - unit === 'C' - ? addTemperature(celcius) - : addTemperature(celciusToFahrenheit(celcius)) + prepareValue(celcius) } if (hexString.substr(0, 4) === 'aa8e') { diff --git a/components/LifeinaBoxStats.js b/components/LifeinaBoxStats.js index ccccbfa..461cf86 100644 --- a/components/LifeinaBoxStats.js +++ b/components/LifeinaBoxStats.js @@ -1,6 +1,5 @@ import React from 'react' import { useStoreState } from 'easy-peasy' -import { celciusToFahrenheit } from '../lib/' const LifeinaBoxStats = () => { const temperatureValue = useStoreState(state => state.temperature.value) @@ -23,7 +22,7 @@ const LifeinaBoxStats = () => { ) : temperatureValue && unit === 'F' ? ( - {`${celciusToFahrenheit(temperatureValue)} °F`} + {`${temperatureValue} °F`} ) : null} {batteryValue ? ( diff --git a/model/temperature-model.js b/model/temperature-model.js index f2950e5..808e514 100644 --- a/model/temperature-model.js +++ b/model/temperature-model.js @@ -1,5 +1,6 @@ -import { action, computed } from 'easy-peasy' +import { action, computed, thunk } from 'easy-peasy' import { nth } from 'lodash' +import { celciusToFahrenheit } from '../lib/' const temperatureModel = { values: [], @@ -24,6 +25,11 @@ const temperatureModel = { { x: -9, y: nth(state.values, -10) || 0 }, { x: -10, y: nth(state.values, -11) || 0 } ] + }), + prepareValue: thunk((actions, payload, { getState, getStoreState }) => { + const unit = getStoreState().settings.unit + const value = unit === 'C' ? payload : celciusToFahrenheit(payload) + actions.addValue(value) }) }