Skip to content
Snippets Groups Projects
Commit f8007fac authored by Julia Friesel's avatar Julia Friesel
Browse files

Use db schema to make column names

parent 831e9935
No related branches found
No related tags found
No related merge requests found
......@@ -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
}, [])
}
\ No newline at end of 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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment