Merge branch '114-add-import-button' into 'master'
Resolve "Add Import button" Closes #114 See merge request bloodyhealth/drip!48
This commit is contained in:
+30
-7
@@ -1,10 +1,33 @@
|
||||
export const settings = {
|
||||
errors: {
|
||||
noData: 'There is no data to export',
|
||||
couldNotConvert: 'Could not convert data to CSV',
|
||||
problemSharing: 'There was a problem sharing the data export file'
|
||||
shared: {
|
||||
cancel: 'Cancel',
|
||||
errorTitle: 'Error',
|
||||
successTitle: 'Success'
|
||||
},
|
||||
exportTitle: 'My Drip data export',
|
||||
exportSubject: 'My Drip data export',
|
||||
buttonLabel: 'Export data'
|
||||
export: {
|
||||
errors: {
|
||||
noData: 'There is no data to export',
|
||||
couldNotConvert: 'Could not convert data to CSV',
|
||||
problemSharing: 'There was a problem sharing the data export file'
|
||||
},
|
||||
title: 'My Drip data export',
|
||||
subject: 'My Drip data export',
|
||||
button: 'Export data',
|
||||
},
|
||||
import: {
|
||||
button: 'Import data',
|
||||
title: 'Keep existing data?',
|
||||
message: `There are two options for the import:
|
||||
1. Keep existing cycle days and replace only the ones in the import file.
|
||||
2. Delete all existing cycle days and import cycle days from file.`,
|
||||
replaceOption: 'Import and replace',
|
||||
deleteOption: 'Import and delete existing',
|
||||
errors: {
|
||||
couldNotOpenFile: 'Could not open file',
|
||||
postFix: 'No data was imported or changed'
|
||||
},
|
||||
success: {
|
||||
message: 'Data successfully imported'
|
||||
}
|
||||
}
|
||||
}
|
||||
+94
-27
@@ -7,9 +7,12 @@ import {
|
||||
} from 'react-native'
|
||||
|
||||
import Share from 'react-native-share'
|
||||
import getDataAsCsvDataUri from '../lib/export-to-csv'
|
||||
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
|
||||
import rnfs from 'react-native-fs'
|
||||
import styles from '../styles/index'
|
||||
import { settings as labels } from './labels'
|
||||
import getDataAsCsvDataUri from '../lib/import-export/export-to-csv'
|
||||
import importCsv from '../lib/import-export/import-from-csv'
|
||||
|
||||
export default class Settings extends Component {
|
||||
render() {
|
||||
@@ -18,36 +21,100 @@ export default class Settings extends Component {
|
||||
<View style={styles.homeButtons}>
|
||||
<View style={styles.homeButton}>
|
||||
<Button
|
||||
onPress={async () => {
|
||||
let data
|
||||
try {
|
||||
data = getDataAsCsvDataUri()
|
||||
if (!data) {
|
||||
return Alert.alert(labels.errors.noData)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return Alert.alert(labels.errors.couldNotConvert)
|
||||
}
|
||||
|
||||
try {
|
||||
await Share.open({
|
||||
title: labels.exportTitle,
|
||||
url: data,
|
||||
subject: labels.exportSubject,
|
||||
type: 'text/csv',
|
||||
showAppsToView: true
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return Alert.alert(labels.errors.problemSharing)
|
||||
}
|
||||
}}
|
||||
title={labels.buttonLabel}>
|
||||
onPress={ openShareDialogAndExport }
|
||||
title={labels.export.button}>
|
||||
</Button>
|
||||
</View>
|
||||
<View style={styles.homeButton}>
|
||||
<Button
|
||||
title={labels.import.button}
|
||||
onPress={ openImportDialogAndImport }>
|
||||
</Button>
|
||||
</View>
|
||||
</View>
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async function openShareDialogAndExport() {
|
||||
let data
|
||||
try {
|
||||
data = getDataAsCsvDataUri()
|
||||
if (!data) {
|
||||
return alertError(labels.errors.noData)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return alertError(labels.errors.couldNotConvert)
|
||||
}
|
||||
|
||||
try {
|
||||
await Share.open({
|
||||
title: labels.export.title,
|
||||
url: data,
|
||||
subject: labels.export.subject,
|
||||
type: 'text/csv',
|
||||
showAppsToView: true
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return alertError(labels.export.errors.problemSharing)
|
||||
}
|
||||
}
|
||||
|
||||
function openImportDialogAndImport() {
|
||||
Alert.alert(
|
||||
labels.import.title,
|
||||
labels.import.message,
|
||||
[{
|
||||
text: labels.import.replaceOption,
|
||||
onPress: () => getFileContentAndImport({ deleteExisting: false })
|
||||
}, {
|
||||
text: labels.import.deleteOption,
|
||||
onPress: () => getFileContentAndImport({ deleteExisting: true })
|
||||
}, {
|
||||
text: labels.shared.cancel, style: 'cancel', onPress: () => { }
|
||||
}]
|
||||
)
|
||||
}
|
||||
|
||||
async function getFileContentAndImport({ deleteExisting }) {
|
||||
let fileInfo
|
||||
try {
|
||||
fileInfo = await new Promise((resolve, reject) => {
|
||||
DocumentPicker.show({
|
||||
filetype: [DocumentPickerUtil.allFiles()],
|
||||
}, (err, res) => {
|
||||
if (err) return reject(err)
|
||||
resolve(res)
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
// because cancelling also triggers an error, we do nothing here
|
||||
return
|
||||
}
|
||||
|
||||
let fileContent
|
||||
try {
|
||||
fileContent = await rnfs.readFile(fileInfo.uri, 'utf8')
|
||||
} catch (err) {
|
||||
return importError(labels.import.errors.couldNotOpenFile)
|
||||
}
|
||||
|
||||
try {
|
||||
await importCsv(fileContent, deleteExisting)
|
||||
Alert.alert(labels.import.success.title, labels.import.success.message)
|
||||
} catch(err) {
|
||||
importError(err.message)
|
||||
}
|
||||
}
|
||||
|
||||
function alertError(msg) {
|
||||
Alert.alert(labels.shared.errorTitle, msg)
|
||||
}
|
||||
|
||||
function importError(msg) {
|
||||
const postFixed = `${msg}\n\n${labels.import.errors.postFix}`
|
||||
alertError(postFixed)
|
||||
}
|
||||
Reference in New Issue
Block a user