Use db schema to make column names

This commit is contained in:
Julia Friesel
2018-08-04 09:57:20 +02:00
parent 831e99351d
commit f8007fac14
2 changed files with 31 additions and 32 deletions
+10 -29
View File
@@ -7,8 +7,9 @@ import {
} from 'react-native'
import Share from 'react-native-share'
import { Base64 } from 'js-base64'
import styles from '../styles/index'
import objectPath from 'object-path'
import { getColumnNamesForCsv, cycleDaysSortedByDate } from '../db'
import styles from '../styles/index'
export default class Settings extends Component {
constructor(props) {
@@ -27,8 +28,9 @@ export default class Settings extends Component {
<View style={styles.homeButton}>
<Button
onPress={async () => {
const data = makeDataURI()
console.log(data)
// TODO show warning that there is nothing to export
if (!cycleDaysSortedByDate.length) return
const data = makeDataURI(cycleDaysSortedByDate)
try {
await Share.open({
title: 'My Drip data export',
@@ -51,24 +53,15 @@ export default class Settings extends Component {
}
}
function makeDataURI() {
//TODO handle empty DB
const data = [{
date: '2018-06-23',
temperature: {
value: 36.8,
exclude: false
}
}]
const csv = transformToCsv(data)
function makeDataURI(cycleDays) {
const csv = transformToCsv(cycleDays)
const encoded = Base64.encodeURI(csv)
return `data:text/csv;base64,${encoded}`
}
function transformToCsv(json) {
const day = json[0]
const columnNames = getPrefixedKeys(day)
const rows = json
function transformToCsv(cycleDays) {
const columnNames = getColumnNamesForCsv()
const rows = cycleDays
.map(day => {
return columnNames.map(column => {
return objectPath.get(day, column, '')
@@ -79,15 +72,3 @@ function transformToCsv(json) {
rows.unshift(columnNames.join(','))
return rows.join('\n')
}
function getPrefixedKeys(obj, prefix) {
return Object.keys(obj).reduce((acc, key) => {
const prefixedKey = prefix ? [prefix, key].join('.') : key
if (typeof obj[key] != 'object') {
acc.push(prefixedKey)
return acc
}
acc.push(...getPrefixedKeys(obj[key], prefixedKey))
return acc
}, [])
}
+21 -3
View File
@@ -98,6 +98,7 @@ const db = new Realm(realmConfig)
const bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
const temperatureDaysSortedByDate = db.objects('CycleDay').filtered('temperature != null').sorted('date', true)
const cycleDaysSortedByDate = db.objects('CycleDay').sorted('date', true)
function saveSymptom(symptom, cycleDay, val) {
db.write(() => {
@@ -105,8 +106,6 @@ function saveSymptom(symptom, cycleDay, val) {
})
}
const cycleDaysSortedByDate = db.objects('CycleDay').sorted('date', true)
function getOrCreateCycleDay(localDate) {
let result = db.objectForPrimaryKey('CycleDay', localDate)
if (!result) {
@@ -164,6 +163,24 @@ function getPreviousTemperature(cycleDay) {
return winner.temperature.value
}
function getColumnNamesForCsv() {
return getPrefixedKeys('CycleDay')
function getPrefixedKeys(schemaName, prefix) {
const schema = db.schema.find(x => x.name === schemaName).properties
return Object.keys(schema).reduce((acc, key) => {
const prefixedKey = prefix ? [prefix, key].join('.') : key
const childSchemaName = schema[key].objectType
if (!childSchemaName) {
acc.push(prefixedKey)
return acc
}
acc.push(...getPrefixedKeys(childSchemaName, prefixedKey))
return acc
}, [])
}
}
export {
saveSymptom,
getOrCreateCycleDay,
@@ -173,5 +190,6 @@ export {
fillWithDummyData,
deleteAll,
getPreviousTemperature,
getCycleDay
getCycleDay,
getColumnNamesForCsv
}