Merge branch 'save-as-localdate' into 'master'

Save date as localdate instead of JS date

See merge request bloodyhealth/drip!10
This commit is contained in:
Julia Friesel
2018-06-11 10:00:49 +00:00
2 changed files with 7 additions and 16 deletions
+2 -5
View File
@@ -17,9 +17,7 @@ export default class DatePickView extends Component {
} }
passDateToDayView(result) { passDateToDayView(result) {
// TODO this also has date as a string, perhaps useful for LocalDateFormat const cycleDay = getOrCreateCycleDay(result.dateString)
const date = new Date(result.year, result.month - 1, result.day)
const cycleDay = getOrCreateCycleDay(date)
const navigate = this.props.navigation.navigate const navigate = this.props.navigation.navigate
navigate('dayView', { cycleDay }) navigate('dayView', { cycleDay })
} }
@@ -40,8 +38,7 @@ export default class DatePickView extends Component {
function getBleedingDaysInCalFormat(bleedingDaysSortedByDate) { function getBleedingDaysInCalFormat(bleedingDaysSortedByDate) {
const shadesOfRed = ['#ffbaba', '#ff7b7b', '#ff5252', '#ff0000'] const shadesOfRed = ['#ffbaba', '#ff7b7b', '#ff5252', '#ff0000']
return bleedingDaysSortedByDate.reduce((acc, day) => { return bleedingDaysSortedByDate.reduce((acc, day) => {
const dateString = day.date.toISOString().slice(0, 10) acc[day.date] = { startingDay: true, endingDay: true, color: shadesOfRed[day.bleeding.value] }
acc[dateString] = { startingDay: true, endingDay: true, color: shadesOfRed[day.bleeding.value] }
return acc return acc
}, {}) }, {})
} }
+5 -11
View File
@@ -1,9 +1,7 @@
import realm from 'realm' import realm from 'realm'
import { v4 as uuid } from 'uuid'
let db let db
let cycleDaysSortedbyTempValueView = [] let cycleDaysSortedbyTempValueView = []
let cycleDaysSortedbyDate = []
let bleedingDaysSortedByDate = [] let bleedingDaysSortedByDate = []
const TemperatureSchema = { const TemperatureSchema = {
@@ -24,10 +22,9 @@ const BleedingSchema = {
const CycleDaySchema = { const CycleDaySchema = {
name: 'CycleDay', name: 'CycleDay',
primaryKey: 'key', primaryKey: 'date',
properties: { properties: {
key: 'string', date: 'string',
date: 'date',
temperature: { temperature: {
type: 'Temperature', type: 'Temperature',
optional: true optional: true
@@ -52,14 +49,12 @@ async function openDatabase() {
// just for testing purposes, the highest temperature will be topmost // just for testing purposes, the highest temperature will be topmost
// because I was too layz to make a scroll view // because I was too layz to make a scroll view
cycleDaysSortedbyTempValueView = db.objects('CycleDay').filtered('temperature != null').sorted('temperature.value', true) cycleDaysSortedbyTempValueView = db.objects('CycleDay').filtered('temperature != null').sorted('temperature.value', true)
cycleDaysSortedbyDate = db.objects('CycleDay').sorted('date', true)
bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
} }
function saveTemperature(date, temperature) { function saveTemperature(date, temperature) {
db.write(() => { db.write(() => {
const doc = { const doc = {
key: uuid(),
date, date,
temperature temperature
} }
@@ -73,13 +68,12 @@ function saveBleeding(cycleDay, bleeding) {
}) })
} }
function getOrCreateCycleDay(date) { function getOrCreateCycleDay(localDate) {
let result = Array.from(cycleDaysSortedbyDate.filtered('date = $0', date))[0] let result = db.objectForPrimaryKey('CycleDay', localDate)
if (!result) { if (!result) {
db.write(() => { db.write(() => {
result = db.create('CycleDay', { result = db.create('CycleDay', {
key: uuid(), date: localDate
date
}) })
}) })
} }