Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Drip
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
backup
Drip
Commits
ebb628c6
Commit
ebb628c6
authored
6 years ago
by
Julia Friesel
Browse files
Options
Downloads
Patches
Plain Diff
Move export logic to separate module
parent
9e800596
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
components/settings.js
+17
-48
17 additions, 48 deletions
components/settings.js
lib/export-to-csv.js
+45
-0
45 additions, 0 deletions
lib/export-to-csv.js
with
62 additions
and
48 deletions
components/settings.js
+
17
−
48
View file @
ebb628c6
...
...
@@ -3,12 +3,11 @@ import {
View
,
Button
,
Text
,
ScrollView
ScrollView
,
Alert
}
from
'
react-native
'
import
Share
from
'
react-native-share
'
import
{
Base64
}
from
'
js-base64
'
import
objectPath
from
'
object-path
'
import
{
getColumnNamesForCsv
,
cycleDaysSortedByDate
}
from
'
../db
'
import
getDataAsCsvDataUri
from
'
../lib/export-to-csv
'
import
styles
from
'
../styles/index
'
export
default
class
Settings
extends
Component
{
...
...
@@ -28,9 +27,17 @@ export default class Settings extends Component {
<
View
style
=
{
styles
.
homeButton
}
>
<
Button
onPress
=
{
async
()
=>
{
// TODO show warning that there is nothing to export
if
(
!
cycleDaysSortedByDate
.
length
)
return
const
data
=
makeDataURI
(
cycleDaysSortedByDate
)
let
data
try
{
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
{
await
Share
.
open
({
title
:
'
My Drip data export
'
,
...
...
@@ -40,53 +47,15 @@ export default class Settings extends Component {
showAppsToView
:
true
})
}
catch
(
err
)
{
// TODO hand
le
error
console
.
log
(
err
)
conso
le
.
error
(
err
)
return
Alert
.
alert
(
'
There was a problem sharing the data export file
'
)
}
}}
title
=
"
E
dit symptoms for today
"
>
title
=
"
E
xport data
"
>
<
/Button
>
<
/View
>
<
/View
>
<
/ScrollView
>
)
}
}
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
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
lib/export-to-csv.js
0 → 100644
+
45
−
0
View file @
ebb628c6
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
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment