Move export logic to separate module
This commit is contained in:
+17
-48
@@ -3,12 +3,11 @@ import {
|
|||||||
View,
|
View,
|
||||||
Button,
|
Button,
|
||||||
Text,
|
Text,
|
||||||
ScrollView
|
ScrollView,
|
||||||
|
Alert
|
||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
import Share from 'react-native-share'
|
import Share from 'react-native-share'
|
||||||
import { Base64 } from 'js-base64'
|
import getDataAsCsvDataUri from '../lib/export-to-csv'
|
||||||
import objectPath from 'object-path'
|
|
||||||
import { getColumnNamesForCsv, cycleDaysSortedByDate } from '../db'
|
|
||||||
import styles from '../styles/index'
|
import styles from '../styles/index'
|
||||||
|
|
||||||
export default class Settings extends Component {
|
export default class Settings extends Component {
|
||||||
@@ -28,9 +27,17 @@ export default class Settings extends Component {
|
|||||||
<View style={styles.homeButton}>
|
<View style={styles.homeButton}>
|
||||||
<Button
|
<Button
|
||||||
onPress={async () => {
|
onPress={async () => {
|
||||||
// TODO show warning that there is nothing to export
|
let data
|
||||||
if (!cycleDaysSortedByDate.length) return
|
try {
|
||||||
const data = makeDataURI(cycleDaysSortedByDate)
|
data = getDataAsCsvDataUri()
|
||||||
|
if (!data) {
|
||||||
|
return Alert.alert('There is no data to export')
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
return Alert.alert('Could not convert data to CSV')
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await Share.open({
|
await Share.open({
|
||||||
title: 'My Drip data export',
|
title: 'My Drip data export',
|
||||||
@@ -40,11 +47,11 @@ export default class Settings extends Component {
|
|||||||
showAppsToView: true
|
showAppsToView: true
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// TODO handle error
|
console.error(err)
|
||||||
console.log(err)
|
return Alert.alert('There was a problem sharing the data export file')
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
title="Edit symptoms for today">
|
title="Export data">
|
||||||
</Button>
|
</Button>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
@@ -52,41 +59,3 @@ export default class Settings extends Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeDataURI(cycleDays) {
|
|
||||||
const csv = transformToCsv(cycleDays)
|
|
||||||
const encoded = Base64.encodeURI(csv)
|
|
||||||
return `data:text/csv;base64,${encoded}`
|
|
||||||
}
|
|
||||||
|
|
||||||
function transformToCsv(cycleDays) {
|
|
||||||
const columnNames = getColumnNamesForCsv()
|
|
||||||
const rows = cycleDays
|
|
||||||
.map(day => {
|
|
||||||
return columnNames.map(column => {
|
|
||||||
const val = objectPath.get(day, column, '')
|
|
||||||
return typeof val === 'string' ? csvify(val) : val
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map(row => row.join(','))
|
|
||||||
|
|
||||||
rows.unshift(columnNames.join(','))
|
|
||||||
return rows.join('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
function csvify (val) {
|
|
||||||
// escape double quotes
|
|
||||||
val = val.replace(/"/g, '""')
|
|
||||||
|
|
||||||
val = val.toLowerCase()
|
|
||||||
const hasSpecialChars = (
|
|
||||||
val.includes('\n') ||
|
|
||||||
val.includes('\t') ||
|
|
||||||
val.includes(',') ||
|
|
||||||
val.includes(';') ||
|
|
||||||
val.includes('.') ||
|
|
||||||
val.includes('\'')
|
|
||||||
)
|
|
||||||
|
|
||||||
return hasSpecialChars ? `"${val}"` : val
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import objectPath from 'object-path'
|
||||||
|
import { Base64 } from 'js-base64'
|
||||||
|
|
||||||
|
import { getColumnNamesForCsv, cycleDaysSortedByDate } from '../db'
|
||||||
|
|
||||||
|
export default function makeDataURI() {
|
||||||
|
if (!cycleDaysSortedByDate.length) return null
|
||||||
|
|
||||||
|
const csv = transformToCsv(cycleDaysSortedByDate)
|
||||||
|
const encoded = Base64.encodeURI(csv)
|
||||||
|
return `data:text/csv;base64,${encoded}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function transformToCsv(cycleDays) {
|
||||||
|
const columnNames = getColumnNamesForCsv()
|
||||||
|
const rows = cycleDays
|
||||||
|
.map(day => {
|
||||||
|
return columnNames.map(column => {
|
||||||
|
const val = objectPath.get(day, column)
|
||||||
|
return typeof val === 'string' ? csvify(val) : val
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.map(row => row.join(','))
|
||||||
|
|
||||||
|
rows.unshift(columnNames.join(','))
|
||||||
|
return rows.join('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
function csvify (val) {
|
||||||
|
// we wrap fields with special characters in quotes,
|
||||||
|
// thus have to escape actual quotes
|
||||||
|
val = val.replace(/"/g, '""')
|
||||||
|
|
||||||
|
val = val.toLowerCase()
|
||||||
|
const hasSpecialChars = (
|
||||||
|
val.includes('\n') ||
|
||||||
|
val.includes('\t') ||
|
||||||
|
val.includes(',') ||
|
||||||
|
val.includes(';') ||
|
||||||
|
val.includes('.') ||
|
||||||
|
val.includes('\'')
|
||||||
|
)
|
||||||
|
|
||||||
|
return hasSpecialChars ? `"${val}"` : val
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user