Use db schema to make column names
This commit is contained in:
+10
-29
@@ -7,8 +7,9 @@ import {
|
|||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
import Share from 'react-native-share'
|
import Share from 'react-native-share'
|
||||||
import { Base64 } from 'js-base64'
|
import { Base64 } from 'js-base64'
|
||||||
import styles from '../styles/index'
|
|
||||||
import objectPath from 'object-path'
|
import objectPath from 'object-path'
|
||||||
|
import { getColumnNamesForCsv, cycleDaysSortedByDate } from '../db'
|
||||||
|
import styles from '../styles/index'
|
||||||
|
|
||||||
export default class Settings extends Component {
|
export default class Settings extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -27,8 +28,9 @@ export default class Settings extends Component {
|
|||||||
<View style={styles.homeButton}>
|
<View style={styles.homeButton}>
|
||||||
<Button
|
<Button
|
||||||
onPress={async () => {
|
onPress={async () => {
|
||||||
const data = makeDataURI()
|
// TODO show warning that there is nothing to export
|
||||||
console.log(data)
|
if (!cycleDaysSortedByDate.length) return
|
||||||
|
const data = makeDataURI(cycleDaysSortedByDate)
|
||||||
try {
|
try {
|
||||||
await Share.open({
|
await Share.open({
|
||||||
title: 'My Drip data export',
|
title: 'My Drip data export',
|
||||||
@@ -51,24 +53,15 @@ export default class Settings extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeDataURI() {
|
function makeDataURI(cycleDays) {
|
||||||
//TODO handle empty DB
|
const csv = transformToCsv(cycleDays)
|
||||||
const data = [{
|
|
||||||
date: '2018-06-23',
|
|
||||||
temperature: {
|
|
||||||
value: 36.8,
|
|
||||||
exclude: false
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
const csv = transformToCsv(data)
|
|
||||||
const encoded = Base64.encodeURI(csv)
|
const encoded = Base64.encodeURI(csv)
|
||||||
return `data:text/csv;base64,${encoded}`
|
return `data:text/csv;base64,${encoded}`
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformToCsv(json) {
|
function transformToCsv(cycleDays) {
|
||||||
const day = json[0]
|
const columnNames = getColumnNamesForCsv()
|
||||||
const columnNames = getPrefixedKeys(day)
|
const rows = cycleDays
|
||||||
const rows = json
|
|
||||||
.map(day => {
|
.map(day => {
|
||||||
return columnNames.map(column => {
|
return columnNames.map(column => {
|
||||||
return objectPath.get(day, column, '')
|
return objectPath.get(day, column, '')
|
||||||
@@ -79,15 +72,3 @@ function transformToCsv(json) {
|
|||||||
rows.unshift(columnNames.join(','))
|
rows.unshift(columnNames.join(','))
|
||||||
return rows.join('\n')
|
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
@@ -98,6 +98,7 @@ const db = new Realm(realmConfig)
|
|||||||
|
|
||||||
const bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
|
const bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
|
||||||
const temperatureDaysSortedByDate = db.objects('CycleDay').filtered('temperature != 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) {
|
function saveSymptom(symptom, cycleDay, val) {
|
||||||
db.write(() => {
|
db.write(() => {
|
||||||
@@ -105,8 +106,6 @@ function saveSymptom(symptom, cycleDay, val) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const cycleDaysSortedByDate = db.objects('CycleDay').sorted('date', true)
|
|
||||||
|
|
||||||
function getOrCreateCycleDay(localDate) {
|
function getOrCreateCycleDay(localDate) {
|
||||||
let result = db.objectForPrimaryKey('CycleDay', localDate)
|
let result = db.objectForPrimaryKey('CycleDay', localDate)
|
||||||
if (!result) {
|
if (!result) {
|
||||||
@@ -164,6 +163,24 @@ function getPreviousTemperature(cycleDay) {
|
|||||||
return winner.temperature.value
|
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 {
|
export {
|
||||||
saveSymptom,
|
saveSymptom,
|
||||||
getOrCreateCycleDay,
|
getOrCreateCycleDay,
|
||||||
@@ -173,5 +190,6 @@ export {
|
|||||||
fillWithDummyData,
|
fillWithDummyData,
|
||||||
deleteAll,
|
deleteAll,
|
||||||
getPreviousTemperature,
|
getPreviousTemperature,
|
||||||
getCycleDay
|
getCycleDay,
|
||||||
|
getColumnNamesForCsv
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user