Chore/prettify for joda
This commit is contained in:
+22
-19
@@ -12,14 +12,16 @@ import Button from './common/button'
|
|||||||
|
|
||||||
import cycleModule from '../lib/cycle'
|
import cycleModule from '../lib/cycle'
|
||||||
import { getFertilityStatusForDay } from '../lib/sympto-adapter'
|
import { getFertilityStatusForDay } from '../lib/sympto-adapter'
|
||||||
import { determinePredictionText, formatWithOrdinalSuffix } from './helpers/home'
|
import {
|
||||||
|
determinePredictionText,
|
||||||
|
formatWithOrdinalSuffix,
|
||||||
|
} from './helpers/home'
|
||||||
|
|
||||||
import { Colors, Fonts, Sizes, Spacing } from '../styles'
|
import { Colors, Fonts, Sizes, Spacing } from '../styles'
|
||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
const Home = ({ navigate, setDate }) => {
|
const Home = ({ navigate, setDate }) => {
|
||||||
|
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
function navigateToCycleDayView() {
|
function navigateToCycleDayView() {
|
||||||
@@ -34,22 +36,26 @@ const Home = ({ navigate, setDate }) => {
|
|||||||
getFertilityStatusForDay(todayDateString)
|
getFertilityStatusForDay(todayDateString)
|
||||||
const prediction = determinePredictionText(getPredictedMenses(), t)
|
const prediction = determinePredictionText(getPredictedMenses(), t)
|
||||||
|
|
||||||
const cycleDayText = cycleDayNumber ? formatWithOrdinalSuffix(cycleDayNumber) : ''
|
const cycleDayText = cycleDayNumber
|
||||||
|
? formatWithOrdinalSuffix(cycleDayNumber)
|
||||||
|
: ''
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ScrollView
|
<ScrollView
|
||||||
style={styles.container}
|
style={styles.container}
|
||||||
contentContainerStyle={styles.contentContainer}
|
contentContainerStyle={styles.contentContainer}
|
||||||
>
|
>
|
||||||
<AppText style={styles.title}>{moment().format("MMM Do YYYY")}</AppText>
|
<AppText style={styles.title}>{moment().format('MMM Do YYYY')}</AppText>
|
||||||
|
|
||||||
{cycleDayNumber &&
|
{cycleDayNumber && (
|
||||||
<View style={styles.line}>
|
<View style={styles.line}>
|
||||||
<AppText style={styles.whiteSubtitle}>{cycleDayText}</AppText>
|
<AppText style={styles.whiteSubtitle}>{cycleDayText}</AppText>
|
||||||
<AppText style={styles.turquoiseText}>{t('labels.home.cycleDay')}</AppText>
|
<AppText style={styles.turquoiseText}>
|
||||||
|
{t('labels.home.cycleDay')}
|
||||||
|
</AppText>
|
||||||
</View>
|
</View>
|
||||||
}
|
)}
|
||||||
{phase &&
|
{phase && (
|
||||||
<View style={styles.line}>
|
<View style={styles.line}>
|
||||||
<AppText style={styles.whiteSubtitle}>
|
<AppText style={styles.whiteSubtitle}>
|
||||||
{formatWithOrdinalSuffix(phase)}
|
{formatWithOrdinalSuffix(phase)}
|
||||||
@@ -60,7 +66,7 @@ const Home = ({ navigate, setDate }) => {
|
|||||||
<AppText style={styles.turquoiseText}>{status}</AppText>
|
<AppText style={styles.turquoiseText}>{status}</AppText>
|
||||||
<Asterisk />
|
<Asterisk />
|
||||||
</View>
|
</View>
|
||||||
}
|
)}
|
||||||
<View style={styles.line}>
|
<View style={styles.line}>
|
||||||
<AppText style={styles.turquoiseText}>{prediction}</AppText>
|
<AppText style={styles.turquoiseText}>{prediction}</AppText>
|
||||||
</View>
|
</View>
|
||||||
@@ -128,28 +134,25 @@ const styles = StyleSheet.create({
|
|||||||
greyText: {
|
greyText: {
|
||||||
color: Colors.greyLight,
|
color: Colors.greyLight,
|
||||||
paddingLeft: Spacing.base,
|
paddingLeft: Spacing.base,
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const mapStateToProps = (state) => {
|
const mapStateToProps = (state) => {
|
||||||
return ({
|
return {
|
||||||
date: getDate(state),
|
date: getDate(state),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch) => {
|
const mapDispatchToProps = (dispatch) => {
|
||||||
return ({
|
return {
|
||||||
navigate: (page) => dispatch(navigate(page)),
|
navigate: (page) => dispatch(navigate(page)),
|
||||||
setDate: (date) => dispatch(setDate(date)),
|
setDate: (date) => dispatch(setDate(date)),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Home.propTypes = {
|
Home.propTypes = {
|
||||||
navigate: PropTypes.func,
|
navigate: PropTypes.func,
|
||||||
setDate: PropTypes.func
|
setDate: PropTypes.func,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect(
|
export default connect(mapStateToProps, mapDispatchToProps)(Home)
|
||||||
mapStateToProps,
|
|
||||||
mapDispatchToProps,
|
|
||||||
)(Home)
|
|
||||||
|
|||||||
+48
-50
@@ -45,7 +45,6 @@ export function getTickList(columnHeight) {
|
|||||||
const tickHeight = columnHeight / numberOfTicks
|
const tickHeight = columnHeight / numberOfTicks
|
||||||
|
|
||||||
return getTickPositions(columnHeight).map((tickPosition, i) => {
|
return getTickPositions(columnHeight).map((tickPosition, i) => {
|
||||||
|
|
||||||
const tick = scaleMax - i * unit
|
const tick = scaleMax - i * unit
|
||||||
const isBold = Number.isInteger(tick) ? true : false
|
const isBold = Number.isInteger(tick) ? true : false
|
||||||
const label = tick.toFixed(1)
|
const label = tick.toFixed(1)
|
||||||
@@ -56,14 +55,14 @@ export function getTickList(columnHeight) {
|
|||||||
|
|
||||||
if (unit === 0.1) {
|
if (unit === 0.1) {
|
||||||
// show label with step 0.2
|
// show label with step 0.2
|
||||||
shouldShowLabel = !(label * 10 % 2)
|
shouldShowLabel = !((label * 10) % 2)
|
||||||
} else {
|
} else {
|
||||||
// show label with step 0.5
|
// show label with step 0.5
|
||||||
shouldShowLabel = !(label * 10 % 5)
|
shouldShowLabel = !((label * 10) % 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't show label, if first or last tick
|
// don't show label, if first or last tick
|
||||||
if ( i === 0 || i === (numberOfTicks - 1) ) {
|
if (i === 0 || i === numberOfTicks - 1) {
|
||||||
shouldShowLabel = false
|
shouldShowLabel = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +71,7 @@ export function getTickList(columnHeight) {
|
|||||||
label,
|
label,
|
||||||
isBold,
|
isBold,
|
||||||
shouldShowLabel,
|
shouldShowLabel,
|
||||||
tickHeight
|
tickHeight,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -84,17 +83,17 @@ export function isSymptomDataComplete(symptom, dateString) {
|
|||||||
const symptomData = cycleDayData[symptom]
|
const symptomData = cycleDayData[symptom]
|
||||||
|
|
||||||
const dataCompletenessCheck = {
|
const dataCompletenessCheck = {
|
||||||
'cervix': () => {
|
cervix: () => {
|
||||||
const { opening, firmness } = symptomData
|
const { opening, firmness } = symptomData
|
||||||
return (opening !== null) && (firmness !== null)
|
return opening !== null && firmness !== null
|
||||||
},
|
},
|
||||||
'mucus': () => {
|
mucus: () => {
|
||||||
const { feeling, texture } = symptomData
|
const { feeling, texture } = symptomData
|
||||||
return (feeling !== null) && (texture !== null)
|
return feeling !== null && texture !== null
|
||||||
},
|
},
|
||||||
'default': () => {
|
default: () => {
|
||||||
return true
|
return true
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
return (dataCompletenessCheck[symptom] || dataCompletenessCheck['default'])()
|
return (dataCompletenessCheck[symptom] || dataCompletenessCheck['default'])()
|
||||||
}
|
}
|
||||||
@@ -104,7 +103,7 @@ function getInfoForNeighborColumns(dateString, columnHeight) {
|
|||||||
rightY: null,
|
rightY: null,
|
||||||
rightTemperatureExclude: null,
|
rightTemperatureExclude: null,
|
||||||
leftY: null,
|
leftY: null,
|
||||||
leftTemperatureExclude: null
|
leftTemperatureExclude: null,
|
||||||
}
|
}
|
||||||
const target = LocalDate.parse(dateString)
|
const target = LocalDate.parse(dateString)
|
||||||
const dayBefore = target.minusDays(1).toString()
|
const dayBefore = target.minusDays(1).toString()
|
||||||
@@ -127,53 +126,57 @@ function getInfoForNeighborColumns(dateString, columnHeight) {
|
|||||||
export function getTemperatureProps(symptomData, columnHeight, dateString) {
|
export function getTemperatureProps(symptomData, columnHeight, dateString) {
|
||||||
const extractedData = {}
|
const extractedData = {}
|
||||||
const { value, exclude } = symptomData
|
const { value, exclude } = symptomData
|
||||||
const neighborTemperatureGraphPoints =
|
const neighborTemperatureGraphPoints = getInfoForNeighborColumns(
|
||||||
getInfoForNeighborColumns(dateString, columnHeight)
|
dateString,
|
||||||
|
columnHeight
|
||||||
|
)
|
||||||
|
|
||||||
for (const key in neighborTemperatureGraphPoints) {
|
for (const key in neighborTemperatureGraphPoints) {
|
||||||
extractedData[key] = neighborTemperatureGraphPoints[key]
|
extractedData[key] = neighborTemperatureGraphPoints[key]
|
||||||
}
|
}
|
||||||
return Object.assign({
|
return Object.assign(
|
||||||
value,
|
{
|
||||||
y: normalizeToScale(value, columnHeight),
|
value,
|
||||||
temperatureExclude: exclude,
|
y: normalizeToScale(value, columnHeight),
|
||||||
}, extractedData)
|
temperatureExclude: exclude,
|
||||||
|
},
|
||||||
|
extractedData
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const symptomColorMethods = {
|
export const symptomColorMethods = {
|
||||||
'mucus': (symptomData) => {
|
mucus: (symptomData) => {
|
||||||
const { feeling, texture } = symptomData
|
const { feeling, texture } = symptomData
|
||||||
const colorIndex = feeling + texture
|
const colorIndex = feeling + texture
|
||||||
return colorIndex
|
return colorIndex
|
||||||
},
|
},
|
||||||
'cervix': (symptomData) => {
|
cervix: (symptomData) => {
|
||||||
const { opening, firmness } = symptomData
|
const { opening, firmness } = symptomData
|
||||||
const isDataComplete = opening !== null && firmness !== null
|
const isDataComplete = opening !== null && firmness !== null
|
||||||
const isClosedAndHard =
|
const isClosedAndHard = isDataComplete && opening === 0 && firmness === 0
|
||||||
isDataComplete &&
|
|
||||||
(opening === 0 && firmness === 0)
|
|
||||||
const colorIndex = isClosedAndHard ? 0 : 2
|
const colorIndex = isClosedAndHard ? 0 : 2
|
||||||
return colorIndex
|
return colorIndex
|
||||||
},
|
},
|
||||||
'sex': (symptomData) => {
|
sex: (symptomData) => {
|
||||||
const { solo, partner } = symptomData
|
const { solo, partner } = symptomData
|
||||||
const colorIndex = (solo !== null && partner !== null) ?
|
const colorIndex =
|
||||||
(solo + 2 * partner - 1) : 0
|
solo !== null && partner !== null ? solo + 2 * partner - 1 : 0
|
||||||
return colorIndex
|
return colorIndex
|
||||||
},
|
},
|
||||||
'bleeding': (symptomData) => {
|
bleeding: (symptomData) => {
|
||||||
const { value } = symptomData
|
const { value } = symptomData
|
||||||
const colorIndex = value
|
const colorIndex = value
|
||||||
return colorIndex
|
return colorIndex
|
||||||
},
|
},
|
||||||
'desire': (symptomData) => {
|
desire: (symptomData) => {
|
||||||
const { value } = symptomData
|
const { value } = symptomData
|
||||||
const colorIndex = value
|
const colorIndex = value
|
||||||
return colorIndex
|
return colorIndex
|
||||||
},
|
},
|
||||||
'default': () => { //pain, mood, note
|
default: () => {
|
||||||
|
//pain, mood, note
|
||||||
return 0
|
return 0
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chart helpers
|
// Chart helpers
|
||||||
@@ -188,7 +191,7 @@ export function makeColumnInfo() {
|
|||||||
amountOfCycleDays += 5
|
amountOfCycleDays += 5
|
||||||
}
|
}
|
||||||
const localDates = getTodayAndPreviousDays(amountOfCycleDays)
|
const localDates = getTodayAndPreviousDays(amountOfCycleDays)
|
||||||
return localDates.map(localDate => localDate.toString())
|
return localDates.map((localDate) => localDate.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTodayAndPreviousDays(n) {
|
function getTodayAndPreviousDays(n) {
|
||||||
@@ -210,17 +213,17 @@ function getTodayAndPreviousDays(n) {
|
|||||||
|
|
||||||
export function nfpLines() {
|
export function nfpLines() {
|
||||||
const cycle = {
|
const cycle = {
|
||||||
status: null
|
status: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCurrentCycle(dateString) {
|
function updateCurrentCycle(dateString) {
|
||||||
// for the NFP lines, we don't care about potentially extending the
|
// for the NFP lines, we don't care about potentially extending the
|
||||||
// preOvu phase, so we don't include all earlier cycles, as that is
|
// preOvu phase, so we don't include all earlier cycles, as that is
|
||||||
// an expensive db operation at the moment
|
// an expensive db operation at the moment
|
||||||
cycle.status = getCycleStatusForDay(
|
cycle.status = getCycleStatusForDay(dateString, {
|
||||||
dateString, { excludeEarlierCycles: true }
|
excludeEarlierCycles: true,
|
||||||
)
|
})
|
||||||
if(!cycle.status) {
|
if (!cycle.status) {
|
||||||
cycle.noMoreCycles = true
|
cycle.noMoreCycles = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -232,39 +235,34 @@ export function nfpLines() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function dateIsInPeriOrPostPhase(dateString) {
|
function dateIsInPeriOrPostPhase(dateString) {
|
||||||
return (
|
return dateString >= cycle.status.phases.periOvulatory.start.date
|
||||||
dateString >= cycle.status.phases.periOvulatory.start.date
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function precededByAnotherTempValue(dateString) {
|
function precededByAnotherTempValue(dateString) {
|
||||||
return (
|
return (
|
||||||
// we are only interested in days that have a preceding
|
// we are only interested in days that have a preceding
|
||||||
// temp
|
// temp
|
||||||
Object.keys(cycle.status.phases).some(phaseName => {
|
Object.keys(cycle.status.phases).some((phaseName) => {
|
||||||
return cycle.status.phases[phaseName].cycleDays.some(day => {
|
return cycle.status.phases[phaseName].cycleDays.some((day) => {
|
||||||
return day.temperature && day.date < dateString
|
return day.temperature && day.date < dateString
|
||||||
})
|
})
|
||||||
})
|
}) &&
|
||||||
// and also a following temp, so we don't draw the line
|
// and also a following temp, so we don't draw the line
|
||||||
// longer than necessary
|
// longer than necessary
|
||||||
&&
|
cycle.status.phases.postOvulatory.cycleDays.some((day) => {
|
||||||
cycle.status.phases.postOvulatory.cycleDays.some(day => {
|
|
||||||
return day.temperature && day.date > dateString
|
return day.temperature && day.date > dateString
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isInTempMeasuringPhase(temperature, dateString) {
|
function isInTempMeasuringPhase(temperature, dateString) {
|
||||||
return (
|
return temperature || precededByAnotherTempValue(dateString)
|
||||||
temperature || precededByAnotherTempValue(dateString)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return function(dateString, temperature, columnHeight) {
|
return function (dateString, temperature, columnHeight) {
|
||||||
const ret = {
|
const ret = {
|
||||||
drawLtlAt: null,
|
drawLtlAt: null,
|
||||||
drawFhmLine: false
|
drawFhmLine: false,
|
||||||
}
|
}
|
||||||
if (!cycle.status && !cycle.noMoreCycles) updateCurrentCycle(dateString)
|
if (!cycle.status && !cycle.noMoreCycles) updateCurrentCycle(dateString)
|
||||||
if (cycle.noMoreCycles) return ret
|
if (cycle.noMoreCycles) return ret
|
||||||
|
|||||||
+14
-17
@@ -11,20 +11,18 @@ function getTimes(prediction) {
|
|||||||
const todayDate = LocalDate.now()
|
const todayDate = LocalDate.now()
|
||||||
const predictedBleedingStart = LocalDate.parse(prediction[0][0])
|
const predictedBleedingStart = LocalDate.parse(prediction[0][0])
|
||||||
/* the range of predicted bleeding days can be either 3 or 5 */
|
/* the range of predicted bleeding days can be either 3 or 5 */
|
||||||
const predictedBleedingEnd =
|
const predictedBleedingEnd = LocalDate.parse(
|
||||||
LocalDate.parse(prediction[0][prediction[0].length - 1])
|
prediction[0][prediction[0].length - 1]
|
||||||
|
)
|
||||||
const daysToEnd = todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)
|
const daysToEnd = todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)
|
||||||
return { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd }
|
return { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function determinePredictionText(bleedingPrediction, t) {
|
export function determinePredictionText(bleedingPrediction, t) {
|
||||||
if (!bleedingPrediction.length) return t('labels.bleedingPrediction.noPrediction')
|
if (!bleedingPrediction.length)
|
||||||
const {
|
return t('labels.bleedingPrediction.noPrediction')
|
||||||
todayDate,
|
const { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd } =
|
||||||
predictedBleedingStart,
|
getTimes(bleedingPrediction)
|
||||||
predictedBleedingEnd,
|
|
||||||
daysToEnd
|
|
||||||
} = getTimes(bleedingPrediction)
|
|
||||||
if (todayDate.isBefore(predictedBleedingStart)) {
|
if (todayDate.isBefore(predictedBleedingStart)) {
|
||||||
return predictLabels.predictionInFuture(
|
return predictLabels.predictionInFuture(
|
||||||
todayDate.until(predictedBleedingStart, ChronoUnit.DAYS),
|
todayDate.until(predictedBleedingStart, ChronoUnit.DAYS),
|
||||||
@@ -48,19 +46,18 @@ export function determinePredictionText(bleedingPrediction, t) {
|
|||||||
|
|
||||||
export function getBleedingPredictionRange(prediction) {
|
export function getBleedingPredictionRange(prediction) {
|
||||||
if (!prediction.length) return labels.unknown
|
if (!prediction.length) return labels.unknown
|
||||||
const {
|
const { todayDate, predictedBleedingStart, predictedBleedingEnd, daysToEnd } =
|
||||||
todayDate,
|
getTimes(prediction)
|
||||||
predictedBleedingStart,
|
|
||||||
predictedBleedingEnd,
|
|
||||||
daysToEnd
|
|
||||||
} = getTimes(prediction)
|
|
||||||
if (todayDate.isBefore(predictedBleedingStart)) {
|
if (todayDate.isBefore(predictedBleedingStart)) {
|
||||||
return `${todayDate.until(predictedBleedingStart, ChronoUnit.DAYS)}-${todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)}`
|
return `${todayDate.until(
|
||||||
|
predictedBleedingStart,
|
||||||
|
ChronoUnit.DAYS
|
||||||
|
)}-${todayDate.until(predictedBleedingEnd, ChronoUnit.DAYS)}`
|
||||||
}
|
}
|
||||||
if (todayDate.isAfter(predictedBleedingEnd)) {
|
if (todayDate.isAfter(predictedBleedingEnd)) {
|
||||||
return labels.unknown
|
return labels.unknown
|
||||||
}
|
}
|
||||||
return (daysToEnd === 0 ? '0' : `0 - ${daysToEnd}`)
|
return daysToEnd === 0 ? '0' : `0 - ${daysToEnd}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getOrdinalSuffix(num) {
|
export function getOrdinalSuffix(num) {
|
||||||
|
|||||||
+28
-29
@@ -30,7 +30,9 @@ export default function config(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getLastMensesStartForDay(targetDateString) {
|
function getLastMensesStartForDay(targetDateString) {
|
||||||
return cycleStartsSortedByDate.find(start => start.date <= targetDateString)
|
return cycleStartsSortedByDate.find(
|
||||||
|
(start) => start.date <= targetDateString
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCycleDayNumber(targetDateString) {
|
function getCycleDayNumber(targetDateString) {
|
||||||
@@ -55,25 +57,25 @@ export default function config(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCyclesBefore(targetCycleStartDay) {
|
function getCyclesBefore(targetCycleStartDay) {
|
||||||
const startFromHere = cycleStartsSortedByDate.findIndex(start => {
|
const startFromHere = cycleStartsSortedByDate.findIndex((start) => {
|
||||||
return start.date < targetCycleStartDay.date
|
return start.date < targetCycleStartDay.date
|
||||||
})
|
})
|
||||||
if (startFromHere < 0) return null
|
if (startFromHere < 0) return null
|
||||||
return cycleStartsSortedByDate
|
return (
|
||||||
.slice(startFromHere)
|
cycleStartsSortedByDate
|
||||||
.map(start => getCycleByStartDay(start))
|
.slice(startFromHere)
|
||||||
// filter the ones exceeding maxCycleLength, those are null
|
.map((start) => getCycleByStartDay(start))
|
||||||
.filter(cycle => cycle)
|
// filter the ones exceeding maxCycleLength, those are null
|
||||||
|
.filter((cycle) => cycle)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function findIndexOfDay(day, daysSortedByDate) {
|
function findIndexOfDay(day, daysSortedByDate) {
|
||||||
return daysSortedByDate.findIndex(d => d.date === day.date)
|
return daysSortedByDate.findIndex((d) => d.date === day.date)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNextCycleStartDay(startDay, cycleStartsSortedByDate) {
|
function getNextCycleStartDay(startDay, cycleStartsSortedByDate) {
|
||||||
const cycleStartIndex = findIndexOfDay(
|
const cycleStartIndex = findIndexOfDay(startDay, cycleStartsSortedByDate)
|
||||||
startDay,
|
|
||||||
cycleStartsSortedByDate)
|
|
||||||
return cycleStartsSortedByDate[cycleStartIndex - 1]
|
return cycleStartsSortedByDate[cycleStartIndex - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,8 +84,7 @@ export default function config(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCycleLength(startDate, endDate) {
|
function getCycleLength(startDate, endDate) {
|
||||||
return LocalDate.parse(startDate)
|
return LocalDate.parse(startDate).until(LocalDate.parse(endDate), DAYS)
|
||||||
.until(LocalDate.parse(endDate), DAYS)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidCycle(startDate, endDate) {
|
function isValidCycle(startDate, endDate) {
|
||||||
@@ -109,19 +110,16 @@ export default function config(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isValidCycle(startDay.date, cycleEndDate)) {
|
if (isValidCycle(startDay.date, cycleEndDate)) {
|
||||||
const cycleStartIndex = findIndexOfDay(
|
const cycleStartIndex = findIndexOfDay(startDay, cycleDaysSortedByDate)
|
||||||
startDay,
|
return cycleDaysSortedByDate.slice(cycleEndIndex, cycleStartIndex + 1)
|
||||||
cycleDaysSortedByDate
|
|
||||||
)
|
|
||||||
return cycleDaysSortedByDate
|
|
||||||
.slice(cycleEndIndex, cycleStartIndex + 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCycleForDay(dayOrDate, todayDate) {
|
function getCycleForDay(dayOrDate, todayDate) {
|
||||||
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
|
const dateString =
|
||||||
|
typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
|
||||||
const cycleStart = getLastMensesStartForDay(dateString)
|
const cycleStart = getLastMensesStartForDay(dateString)
|
||||||
if (!cycleStart) return null
|
if (!cycleStart) return null
|
||||||
return getCycleByStartDay(cycleStart, todayDate)
|
return getCycleByStartDay(cycleStart, todayDate)
|
||||||
@@ -138,9 +136,9 @@ export default function config(opts) {
|
|||||||
const localDate = LocalDate.parse(cycleDay.date)
|
const localDate = LocalDate.parse(cycleDay.date)
|
||||||
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
|
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
|
||||||
const bleedingDays = bleedingDaysSortedByDate
|
const bleedingDays = bleedingDaysSortedByDate
|
||||||
const index = bleedingDays.findIndex(day => day.date === cycleDay.date)
|
const index = bleedingDays.findIndex((day) => day.date === cycleDay.date)
|
||||||
const candidates = bleedingDays.slice(index + 1)
|
const candidates = bleedingDays.slice(index + 1)
|
||||||
return !candidates.some(day => {
|
return !candidates.some((day) => {
|
||||||
return day.date >= threshold && !day.bleeding.exclude
|
return day.date >= threshold && !day.bleeding.exclude
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -151,9 +149,9 @@ export default function config(opts) {
|
|||||||
// changes
|
// changes
|
||||||
function getMensesDaysRightAfter(cycleDay) {
|
function getMensesDaysRightAfter(cycleDay) {
|
||||||
const bleedingDays = bleedingDaysSortedByDate
|
const bleedingDays = bleedingDaysSortedByDate
|
||||||
.filter(d => !d.bleeding.exclude)
|
.filter((d) => !d.bleeding.exclude)
|
||||||
.reverse()
|
.reverse()
|
||||||
const firstFollowingBleedingDayIndex = bleedingDays.findIndex(day => {
|
const firstFollowingBleedingDayIndex = bleedingDays.findIndex((day) => {
|
||||||
return day.date > cycleDay.date
|
return day.date > cycleDay.date
|
||||||
})
|
})
|
||||||
return recurse(cycleDay, firstFollowingBleedingDayIndex, [])
|
return recurse(cycleDay, firstFollowingBleedingDayIndex, [])
|
||||||
@@ -179,13 +177,13 @@ export default function config(opts) {
|
|||||||
|
|
||||||
function getAllCycleLengths() {
|
function getAllCycleLengths() {
|
||||||
return cycleStartsSortedByDate
|
return cycleStartsSortedByDate
|
||||||
.map(day => LocalDate.parse(day.date))
|
.map((day) => LocalDate.parse(day.date))
|
||||||
.map((cycleStart, i, startsAsLocalDates) => {
|
.map((cycleStart, i, startsAsLocalDates) => {
|
||||||
if (i === cycleStartsSortedByDate.length - 1) return null
|
if (i === cycleStartsSortedByDate.length - 1) return null
|
||||||
const prevCycleStart = startsAsLocalDates[i + 1]
|
const prevCycleStart = startsAsLocalDates[i + 1]
|
||||||
return prevCycleStart.until(cycleStart, DAYS)
|
return prevCycleStart.until(cycleStart, DAYS)
|
||||||
})
|
})
|
||||||
.filter(length => length && length <= maxCycleLength)
|
.filter((length) => length && length <= maxCycleLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPredictedMenses() {
|
function getPredictedMenses() {
|
||||||
@@ -198,12 +196,14 @@ export default function config(opts) {
|
|||||||
let periodStartVariation
|
let periodStartVariation
|
||||||
if (cycleInfo.stdDeviation === null) {
|
if (cycleInfo.stdDeviation === null) {
|
||||||
periodStartVariation = 2
|
periodStartVariation = 2
|
||||||
} else if (cycleInfo.stdDeviation < 1.5) { // threshold is chosen a little arbitrarily
|
} else if (cycleInfo.stdDeviation < 1.5) {
|
||||||
|
// threshold is chosen a little arbitrarily
|
||||||
periodStartVariation = 1
|
periodStartVariation = 1
|
||||||
} else {
|
} else {
|
||||||
periodStartVariation = 2
|
periodStartVariation = 2
|
||||||
}
|
}
|
||||||
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
|
if (periodDistance - 5 < periodStartVariation) {
|
||||||
|
// otherwise predictions overlap
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
const allMensesStarts = cycleStartsSortedByDate
|
const allMensesStarts = cycleStartsSortedByDate
|
||||||
@@ -222,7 +222,6 @@ export default function config(opts) {
|
|||||||
return predictedMenses
|
return predictedMenses
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getCycleDayNumber,
|
getCycleDayNumber,
|
||||||
getCycleForDay,
|
getCycleForDay,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
getSchema,
|
getSchema,
|
||||||
tryToImportWithDelete,
|
tryToImportWithDelete,
|
||||||
tryToImportWithoutDelete,
|
tryToImportWithoutDelete,
|
||||||
updateCycleStartsForAllCycleDays
|
updateCycleStartsForAllCycleDays,
|
||||||
} from '../../db'
|
} from '../../db'
|
||||||
import getColumnNamesForCsv from './get-csv-column-names'
|
import getColumnNamesForCsv from './get-csv-column-names'
|
||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
@@ -12,7 +12,7 @@ import labels from '../../i18n/en/settings'
|
|||||||
|
|
||||||
export default async function importCsv(csv, deleteFirst) {
|
export default async function importCsv(csv, deleteFirst) {
|
||||||
const parseFuncs = {
|
const parseFuncs = {
|
||||||
bool: val => {
|
bool: (val) => {
|
||||||
if (val.toLowerCase() === 'true') return true
|
if (val.toLowerCase() === 'true') return true
|
||||||
if (val.toLowerCase() === 'false') return false
|
if (val.toLowerCase() === 'false') return false
|
||||||
return val
|
return val
|
||||||
@@ -20,7 +20,7 @@ export default async function importCsv(csv, deleteFirst) {
|
|||||||
int: parseNumberIfPossible,
|
int: parseNumberIfPossible,
|
||||||
float: parseNumberIfPossible,
|
float: parseNumberIfPossible,
|
||||||
double: parseNumberIfPossible,
|
double: parseNumberIfPossible,
|
||||||
string: val => val
|
string: (val) => val,
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseNumberIfPossible(val) {
|
function parseNumberIfPossible(val) {
|
||||||
@@ -36,12 +36,12 @@ export default async function importCsv(csv, deleteFirst) {
|
|||||||
colParser: getColumnNamesForCsv().reduce((acc, colName) => {
|
colParser: getColumnNamesForCsv().reduce((acc, colName) => {
|
||||||
const path = colName.split('.')
|
const path = colName.split('.')
|
||||||
const dbType = getDbType(schema.CycleDay, path)
|
const dbType = getDbType(schema.CycleDay, path)
|
||||||
acc[colName] = item => {
|
acc[colName] = (item) => {
|
||||||
if (item === '') return null
|
if (item === '') return null
|
||||||
return parseFuncs[dbType](item)
|
return parseFuncs[dbType](item)
|
||||||
}
|
}
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {}),
|
||||||
}
|
}
|
||||||
|
|
||||||
const cycleDays = await csvParser(config)
|
const cycleDays = await csvParser(config)
|
||||||
@@ -62,9 +62,11 @@ export default async function importCsv(csv, deleteFirst) {
|
|||||||
|
|
||||||
function validateHeaders(headers) {
|
function validateHeaders(headers) {
|
||||||
const expectedHeaders = getColumnNamesForCsv()
|
const expectedHeaders = getColumnNamesForCsv()
|
||||||
if (!headers.every(header => {
|
if (
|
||||||
return expectedHeaders.indexOf(header) > -1
|
!headers.every((header) => {
|
||||||
})) {
|
return expectedHeaders.indexOf(header) > -1
|
||||||
|
})
|
||||||
|
) {
|
||||||
const msg = `Expected CSV column titles to be ${expectedHeaders.join()}`
|
const msg = `Expected CSV column titles to be ${expectedHeaders.join()}`
|
||||||
throw new Error(msg)
|
throw new Error(msg)
|
||||||
}
|
}
|
||||||
@@ -76,7 +78,7 @@ function putNullForEmptySymptoms(data) {
|
|||||||
function replaceWithNullIfAllPropertiesAreNull(obj) {
|
function replaceWithNullIfAllPropertiesAreNull(obj) {
|
||||||
Object.keys(obj).forEach((key) => {
|
Object.keys(obj).forEach((key) => {
|
||||||
if (!isObject(obj[key])) return
|
if (!isObject(obj[key])) return
|
||||||
if (Object.values(obj[key]).every(val => val === null)) {
|
if (Object.values(obj[key]).every((val) => val === null)) {
|
||||||
obj[key] = null
|
obj[key] = null
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -97,7 +99,10 @@ function throwIfFutureData(cycleDays) {
|
|||||||
for (const i in cycleDays) {
|
for (const i in cycleDays) {
|
||||||
const day = cycleDays[i]
|
const day = cycleDays[i]
|
||||||
// notes are allowed for future dates but everything else isn't
|
// notes are allowed for future dates but everything else isn't
|
||||||
if (day.date > today && Object.keys(day).some(symptom => symptom != 'date' && symptom != 'note')) {
|
if (
|
||||||
|
day.date > today &&
|
||||||
|
Object.keys(day).some((symptom) => symptom != 'date' && symptom != 'note')
|
||||||
|
) {
|
||||||
throw new Error(labels.import.errors.futureEdit)
|
throw new Error(labels.import.errors.futureEdit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -5,8 +5,8 @@ const dateSlice = createSlice({
|
|||||||
slice: 'date',
|
slice: 'date',
|
||||||
initialState: LocalDate.now().toString(),
|
initialState: LocalDate.now().toString(),
|
||||||
reducers: {
|
reducers: {
|
||||||
setDate: (state, action) => action.payload
|
setDate: (state, action) => action.payload,
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// Extract the action creators object and the reducer
|
// Extract the action creators object and the reducer
|
||||||
|
|||||||
Reference in New Issue
Block a user