Skip to content
Snippets Groups Projects
Commit 227aa696 authored by mashazyu's avatar mashazyu
Browse files

Add loading screen to data import

parent c00684eb
Branches
Tags
No related merge requests found
import React from 'react'
import { View } from 'react-native'
import AppText from './app-text'
import { shared } from '../i18n/en/labels'
const AppLoadingView = () => {
return (
<View flex={1}>
<View style={{flex:1, justifyContent: 'center'}}>
<AppText style={{alignSelf: 'center'}}>{shared.loading}</AppText>
</View>
</View>
)
}
export default AppLoadingView
\ No newline at end of file
......@@ -10,6 +10,7 @@ import { cycleDayColor } from '../../styles'
import { scaleObservable } from '../../local-storage'
import config from '../../config'
import AppText from '../app-text'
import AppLoadingView from '../app-loading'
import { shared as labels } from '../../i18n/en/labels'
import DripIcon from '../../assets/drip-icons'
import DripHomeIcon from '../../assets/drip-home-icons'
......@@ -133,11 +134,7 @@ export default class CycleChart extends Component {
onLayout={this.onLayout}
style={{ flexDirection: 'row', flex: 1 }}
>
{!this.state.chartLoaded &&
<View style={{width: '100%', justifyContent: 'center', alignItems: 'center'}}>
<AppText>{labels.loading}</AppText>
</View>
}
{!this.state.chartLoaded && <AppLoadingView />}
{this.state.chartHeight && this.state.chartLoaded &&
<View>
......
......@@ -6,23 +6,23 @@ import { shared as sharedLabels } from '../../../i18n/en/labels'
import labels from '../../../i18n/en/settings'
import alertError from '../shared/alert-error'
export default function openImportDialogAndImport() {
export function openImportDialog(onImportData) {
Alert.alert(
labels.import.title,
labels.import.message,
[{
text: labels.import.replaceOption,
onPress: () => getFileContentAndImport({ deleteExisting: false })
onPress: () => onImportData(false)
}, {
text: labels.import.deleteOption,
onPress: () => getFileContentAndImport({ deleteExisting: true })
onPress: () => onImportData(true)
}, {
text: sharedLabels.cancel, style: 'cancel', onPress: () => { }
}]
)
}
async function getFileContentAndImport({ deleteExisting }) {
export async function getFileContent() {
let fileInfo
try {
fileInfo = await new Promise((resolve, reject) => {
......@@ -45,8 +45,13 @@ async function getFileContentAndImport({ deleteExisting }) {
return importError(labels.import.errors.couldNotOpenFile)
}
return fileContent
}
export async function importData(shouldDeleteExistingData, fileContent) {
try {
await importCsv(fileContent, deleteExisting)
await importCsv(fileContent, shouldDeleteExistingData)
Alert.alert(sharedLabels.successTitle, labels.import.success.message)
} catch(err) {
importError(err.message)
......
import React from 'react'
import { ScrollView } from 'react-native'
import React, { Component } from 'react'
import { ScrollView, View } from 'react-native'
import AppText from '../../app-text'
import FramedSegment from '../../framed-segment'
import AppLoadingView from '../../app-loading'
import SettingsButton from '../shared/settings-button'
import openImportDialogAndImport from './import-dialog'
import { openImportDialog, getFileContent, importData } from './import-dialog'
import openShareDialogAndExport from './export-dialog'
import DeleteData from './delete-data'
import labels from '../../../i18n/en/settings'
const DataManagement = () => {
export default class DataManagement extends Component {
constructor(props) {
super(props)
this.state = { isLoading: false }
}
onStartLoading = () => {
this.setState({ isLoading: true })
}
onEndLoading = () => {
this.setState({ isLoading: false })
}
onImportData = async (shouldDeleteExistingData) => {
try {
this.onStartLoading()
const fileContent = await getFileContent()
if (fileContent) {
await importData(shouldDeleteExistingData, fileContent)
}
} catch(err) {
return
} finally {
this.onEndLoading()
}
}
render() {
return (
<View flex={1}>
{this.state.isLoading && <AppLoadingView />}
{!this.state.isLoading &&
<ScrollView>
<View>
<FramedSegment title={labels.export.button}>
<AppText>{labels.export.segmentExplainer}</AppText>
<SettingsButton onPress={openShareDialogAndExport}>
......@@ -19,7 +54,9 @@ const DataManagement = () => {
</FramedSegment>
<FramedSegment title={labels.import.button}>
<AppText>{labels.import.segmentExplainer}</AppText>
<SettingsButton onPress={openImportDialogAndImport}>
<SettingsButton
onPress= {() => openImportDialog(this.onImportData)}
>
{labels.import.button}
</SettingsButton>
</FramedSegment>
......@@ -30,8 +67,10 @@ const DataManagement = () => {
<AppText>{labels.deleteSegment.explainer}</AppText>
<DeleteData />
</FramedSegment>
</View>
</ScrollView>
}
</View>
)
}
export default DataManagement
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment