621 Use translation library for data export
This commit is contained in:
@@ -3,34 +3,26 @@ import React, { useState } from 'react'
|
|||||||
import AppLoadingView from '../../common/app-loading'
|
import AppLoadingView from '../../common/app-loading'
|
||||||
import AppPage from '../../common/app-page'
|
import AppPage from '../../common/app-page'
|
||||||
import AppText from '../../common/app-text'
|
import AppText from '../../common/app-text'
|
||||||
import Button from '../../common/button'
|
|
||||||
import Segment from '../../common/segment'
|
import Segment from '../../common/segment'
|
||||||
|
|
||||||
import openShareDialogAndExport from './export-dialog'
|
|
||||||
import DeleteData from './delete-data'
|
import DeleteData from './delete-data'
|
||||||
|
|
||||||
import labels from '../../../i18n/en/settings'
|
import labels from '../../../i18n/en/settings'
|
||||||
import ImportData from './ImportData'
|
import ImportData from './ImportData'
|
||||||
|
import ExportData from './ExportData'
|
||||||
|
|
||||||
const DataManagement = () => {
|
const DataManagement = () => {
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
const [isDeletingData, setIsDeletingData] = useState(false)
|
const [isDeletingData, setIsDeletingData] = useState(false)
|
||||||
|
|
||||||
const startExport = () => {
|
|
||||||
setIsDeletingData(false)
|
|
||||||
openShareDialogAndExport()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isLoading) return <AppLoadingView />
|
if (isLoading) return <AppLoadingView />
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppPage>
|
<AppPage>
|
||||||
<Segment title={labels.export.button}>
|
<ExportData
|
||||||
<AppText>{labels.export.segmentExplainer}</AppText>
|
resetIsDeletingData={() => setIsDeletingData(false)}
|
||||||
<Button isCTA onPress={startExport}>
|
setIsLoading={setIsLoading}
|
||||||
{labels.export.button}
|
/>
|
||||||
</Button>
|
|
||||||
</Segment>
|
|
||||||
<ImportData
|
<ImportData
|
||||||
resetIsDeletingData={() => setIsDeletingData(false)}
|
resetIsDeletingData={() => setIsDeletingData(false)}
|
||||||
setIsLoading={setIsLoading}
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -81,6 +81,16 @@
|
|||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"data": {
|
"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": {
|
"import": {
|
||||||
"button": "Import data",
|
"button": "Import data",
|
||||||
"dialog": {
|
"dialog": {
|
||||||
|
|||||||
@@ -19,18 +19,6 @@ export default {
|
|||||||
text: '',
|
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: {
|
deleteSegment: {
|
||||||
title: 'Delete app data',
|
title: 'Delete app data',
|
||||||
explainer: 'Delete app data from this phone',
|
explainer: 'Delete app data from this phone',
|
||||||
|
|||||||
Reference in New Issue
Block a user