Add toCSV function
This commit is contained in:
+56
-29
@@ -3,12 +3,12 @@ import {
|
||||
View,
|
||||
Button,
|
||||
Text,
|
||||
ScrollView,
|
||||
Picker
|
||||
ScrollView
|
||||
} from 'react-native'
|
||||
import Share from 'react-native-share'
|
||||
import { Base64 } from 'js-base64'
|
||||
import styles from '../styles/index'
|
||||
import objectPath from 'object-path'
|
||||
|
||||
export default class Settings extends Component {
|
||||
constructor(props) {
|
||||
@@ -26,41 +26,68 @@ export default class Settings extends Component {
|
||||
<View style={styles.homeButtons}>
|
||||
<View style={styles.homeButton}>
|
||||
<Button
|
||||
onPress={() => this.setState({pickerVisible: true})}
|
||||
onPress={async () => {
|
||||
const data = makeDataURI()
|
||||
console.log(data)
|
||||
try {
|
||||
await Share.open({
|
||||
title: 'My Drip data export',
|
||||
url: data,
|
||||
subject: 'My Drip data export',
|
||||
type: 'text/csv',
|
||||
showAppsToView: true
|
||||
})
|
||||
} catch (err) {
|
||||
// TODO handle error
|
||||
console.log(err)
|
||||
}
|
||||
}}
|
||||
title="Edit symptoms for today">
|
||||
</Button>
|
||||
</View>
|
||||
</View>
|
||||
{this.state.pickerVisible &&
|
||||
<Picker
|
||||
selectedValue='json'
|
||||
style={{ height: 50, width: 200 }}
|
||||
onValueChange={ async format => {
|
||||
const data = makeDataURI(format)
|
||||
console.log(data)
|
||||
try {
|
||||
await Share.open({
|
||||
title: 'My Drip data export',
|
||||
url: data,
|
||||
subject: 'My Drip data export',
|
||||
type: 'text/csv',
|
||||
showAppsToView: true
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}}>
|
||||
<Picker.Item label="JSON" value="json" />
|
||||
<Picker.Item label="CSV" value="csv" />
|
||||
</Picker>
|
||||
}
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function makeDataURI(format) {
|
||||
const data = {hello: "world"}
|
||||
const encoded = Base64.encodeURI(JSON.stringify(data))
|
||||
function makeDataURI() {
|
||||
//TODO handle empty DB
|
||||
const data = [{
|
||||
date: '2018-06-23',
|
||||
temperature: {
|
||||
value: 36.8,
|
||||
exclude: false
|
||||
}
|
||||
}]
|
||||
const csv = transformToCsv(data)
|
||||
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
|
||||
.map(day => {
|
||||
return columnNames.map(column => {
|
||||
return objectPath.get(day, column, '')
|
||||
})
|
||||
})
|
||||
.map(row => row.join(','))
|
||||
|
||||
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
|
||||
}, [])
|
||||
}
|
||||
Generated
+5
@@ -5744,6 +5744,11 @@
|
||||
"integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==",
|
||||
"dev": true
|
||||
},
|
||||
"object-path": {
|
||||
"version": "0.11.4",
|
||||
"resolved": "https://registry.npmjs.org/object-path/-/object-path-0.11.4.tgz",
|
||||
"integrity": "sha1-NwrnUvvzfePqcKhhwju6iRVpGUk="
|
||||
},
|
||||
"object-visit": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/object-visit/-/object-visit-1.0.1.tgz",
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
"js-base64": "^2.4.8",
|
||||
"js-joda": "^1.8.2",
|
||||
"moment": "^2.22.1",
|
||||
"object-path": "^0.11.4",
|
||||
"react": "16.4.1",
|
||||
"react-native": "^0.56.0",
|
||||
"react-native-calendars": "^1.19.3",
|
||||
|
||||
Reference in New Issue
Block a user