621 Use translation library for data export

This commit is contained in:
Lisa Hillebrand
2022-10-14 12:11:03 +02:00
parent d77cb17f1d
commit df6bc575d0
5 changed files with 92 additions and 68 deletions
@@ -3,34 +3,26 @@ import React, { useState } from 'react'
import AppLoadingView from '../../common/app-loading'
import AppPage from '../../common/app-page'
import AppText from '../../common/app-text'
import Button from '../../common/button'
import Segment from '../../common/segment'
import openShareDialogAndExport from './export-dialog'
import DeleteData from './delete-data'
import labels from '../../../i18n/en/settings'
import ImportData from './ImportData'
import ExportData from './ExportData'
const DataManagement = () => {
const [isLoading, setIsLoading] = useState(false)
const [isDeletingData, setIsDeletingData] = useState(false)
const startExport = () => {
setIsDeletingData(false)
openShareDialogAndExport()
}
if (isLoading) return <AppLoadingView />
return (
<AppPage>
<Segment title={labels.export.button}>
<AppText>{labels.export.segmentExplainer}</AppText>
<Button isCTA onPress={startExport}>
{labels.export.button}
</Button>
</Segment>
<ExportData
resetIsDeletingData={() => setIsDeletingData(false)}
setIsLoading={setIsLoading}
/>
<ImportData
resetIsDeletingData={() => setIsDeletingData(false)}
setIsLoading={setIsLoading}
@@ -0,0 +1,77 @@
import React from 'react'
import PropTypes from 'prop-types'
import { getCycleDaysSortedByDate, mapRealmObjToJsObj } from '../../../db'
import getDataAsCsvDataUri from '../../../lib/import-export/export-to-csv'
import alertError from '../common/alert-error'
import { EXPORT_FILE_NAME } from './constants'
import RNFS from 'react-native-fs'
import { useTranslation } from 'react-i18next'
import AppText from '../../common/app-text'
import Button from '../../common/button'
import Segment from '../../common/segment'
import Share from 'react-native-share'
export default function ExportData({ setIsLoading, resetIsDeletingData }) {
const { t } = useTranslation(null, {
keyPrefix: 'hamburgerMenu.settings.data.export',
})
async function startExport() {
resetIsDeletingData()
setIsLoading(true)
await exportData()
setIsLoading(false)
}
async function getData() {
const cycleDaysByDate = mapRealmObjToJsObj(getCycleDaysSortedByDate())
try {
return cycleDaysByDate.length
? getDataAsCsvDataUri(cycleDaysByDate)
: null
} catch (err) {
alertError(t('error.convert'))
return null
}
}
async function exportData() {
const data = await getData()
if (!data) {
alertError(t('error.data'))
return
}
try {
const path = `${RNFS.DocumentDirectoryPath}/${EXPORT_FILE_NAME}`
await RNFS.writeFile(path, data)
await Share.open({
title: t('title'),
url: `file://${path}`,
subject: t('title'),
type: 'text/csv',
showAppsToView: true,
failOnCancel: false,
})
} catch (err) {
return alertError(t('error.share'))
}
}
return (
<Segment title={t('button')}>
<AppText>{t('text')}</AppText>
<Button isCTA onPress={startExport}>
{t('button')}
</Button>
</Segment>
)
}
ExportData.propTypes = {
resetIsDeletingData: PropTypes.func.isRequired,
setIsLoading: PropTypes.func.isRequired,
}
@@ -1,43 +0,0 @@
import Share from 'react-native-share'
import { getCycleDaysSortedByDate, mapRealmObjToJsObj } from '../../../db'
import getDataAsCsvDataUri from '../../../lib/import-export/export-to-csv'
import alertError from '../common/alert-error'
import settings from '../../../i18n/en/settings'
import { EXPORT_FILE_NAME } from './constants'
import RNFS from 'react-native-fs'
export default async function exportData() {
let data
const labels = settings.export
const cycleDaysByDate = mapRealmObjToJsObj(getCycleDaysSortedByDate())
if (!cycleDaysByDate.length) return alertError(labels.errors.noData)
try {
data = getDataAsCsvDataUri(cycleDaysByDate)
if (!data) {
return alertError(labels.errors.noData)
}
} catch (err) {
console.error(err)
return alertError(labels.errors.couldNotConvert)
}
try {
const path = `${RNFS.DocumentDirectoryPath}/${EXPORT_FILE_NAME}`
await RNFS.writeFile(path, data)
await Share.open({
title: labels.title,
url: `file://${path}`,
subject: labels.subject,
type: 'text/csv',
showAppsToView: true,
failOnCancel: false,
})
} catch (err) {
console.error(err)
return alertError(labels.errors.problemSharing)
}
}
+10
View File
@@ -81,6 +81,16 @@
},
"settings": {
"data": {
"export": {
"button": "Export data",
"error": {
"convert": "Could not convert data to CSV",
"data": "There is no data to export",
"share": "There was a problem sharing the data export file"
},
"text": "Export data in CSV format for backup or so you can use it elsewhere",
"title": "My drip. data export"
},
"import": {
"button": "Import data",
"dialog": {
-12
View File
@@ -19,18 +19,6 @@ export default {
text: '',
},
},
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',
segmentExplainer:
'Export data in CSV format for backup or so you can use it elsewhere',
},
deleteSegment: {
title: 'Delete app data',
explainer: 'Delete app data from this phone',