feat: export entries
This commit is contained in:
@@ -1,24 +1,28 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import styled from "@emotion/styled"
|
import styled from "@emotion/styled"
|
||||||
|
import { withTheme } from "emotion-theming"
|
||||||
import { compose } from "recompose"
|
import { compose } from "recompose"
|
||||||
|
import { format } from "date-fns"
|
||||||
import { withFirebase } from "../../firebase"
|
import { withFirebase } from "../../firebase"
|
||||||
import { withAuthentication } from "../../session"
|
import { withAuthentication } from "../../session"
|
||||||
|
import { BeatLoader } from "react-spinners"
|
||||||
|
|
||||||
import { SIZES } from "../../../styles/constants"
|
import { SIZES } from "../../../styles/constants"
|
||||||
|
|
||||||
import SignOut from "../../SignOut"
|
import SignOut from "../../SignOut"
|
||||||
|
import { Button } from "../../elements"
|
||||||
|
|
||||||
const ProfileGrid = styled.div`
|
const ProfileGrid = styled.div`
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: 1fr;
|
grid-template-rows: 1fr;
|
||||||
grid-gap: 20px;
|
grid-gap: 10px;
|
||||||
|
margin-top: 20px;
|
||||||
`
|
`
|
||||||
const ProfileSection = styled.div`
|
const ProfileSection = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
padding: 10px 0;
|
|
||||||
`
|
`
|
||||||
const ProfileSectionHeader = styled.h2`
|
const ProfileSectionHeader = styled.h2`
|
||||||
font-size: ${SIZES.normal};
|
font-size: ${SIZES.normal};
|
||||||
@@ -31,30 +35,59 @@ const ProfileSectionText = styled.span`
|
|||||||
|
|
||||||
class User extends React.Component {
|
class User extends React.Component {
|
||||||
state = {
|
state = {
|
||||||
name: "",
|
files: [],
|
||||||
|
exporting: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
updateInput = e => {
|
getEntries = async _ => {
|
||||||
this.setState({
|
const { firebase, authUser } = this.props
|
||||||
[e.target.name]: e.target.value,
|
const entriesRef = await firebase.db
|
||||||
|
.collection("entries")
|
||||||
|
.where("userId", "==", authUser.uid)
|
||||||
|
.get()
|
||||||
|
const entries = entriesRef.docs.map(doc => doc.data())
|
||||||
|
const editedEntries = entries.map(entry => {
|
||||||
|
return { ...entry, userId: undefined }
|
||||||
})
|
})
|
||||||
|
return editedEntries
|
||||||
}
|
}
|
||||||
|
|
||||||
addUser = e => {
|
clearFiles = () => {
|
||||||
e.preventDefault()
|
if (this.state.files.length) {
|
||||||
const { firebase } = this.props
|
this.state.files.forEach(({ data }) => {
|
||||||
|
window.URL.revokeObjectURL(data)
|
||||||
firebase.db
|
|
||||||
.collection("users")
|
|
||||||
.doc()
|
|
||||||
.add({
|
|
||||||
name: this.state.name,
|
|
||||||
})
|
})
|
||||||
this.setState({ name: "" })
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prepareExport = async () => {
|
||||||
|
try {
|
||||||
|
this.clearFiles()
|
||||||
|
|
||||||
|
this.setState({ exporting: true, files: [] })
|
||||||
|
|
||||||
|
const data = await this.getEntries()
|
||||||
|
const blob = new Blob([JSON.stringify(data)], {
|
||||||
|
type: "text/json;charset=utf-8",
|
||||||
|
})
|
||||||
|
|
||||||
|
const file = {
|
||||||
|
name: `journal-export-${format(new Date(), "MMDDYYYY")}.json`,
|
||||||
|
data: window.URL.createObjectURL(blob),
|
||||||
|
}
|
||||||
|
this.setState({ files: [file], exporting: false })
|
||||||
|
} catch (e) {
|
||||||
|
window.alert(
|
||||||
|
"Your export ran into an issue, sorry :( if you continue to have problmes you can reach out to kylerobertgill@gmail.com"
|
||||||
|
)
|
||||||
|
console.error(e)
|
||||||
|
this.setState({ files: [], exporting: 0 })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { authUser } = this.props
|
const { authUser, theme } = this.props
|
||||||
|
const { exporting, files } = this.state
|
||||||
return (
|
return (
|
||||||
<ProfileGrid>
|
<ProfileGrid>
|
||||||
<ProfileSection>
|
<ProfileSection>
|
||||||
@@ -63,7 +96,47 @@ class User extends React.Component {
|
|||||||
</ProfileSectionHeader>
|
</ProfileSectionHeader>
|
||||||
<SignOut />
|
<SignOut />
|
||||||
</ProfileSection>
|
</ProfileSection>
|
||||||
{/* <ProfileSection>Export Entries</ProfileSection> */}
|
<ProfileSection>
|
||||||
|
<ProfileSectionHeader>
|
||||||
|
Export Journal Entries{" "}
|
||||||
|
<div>
|
||||||
|
<ProfileSectionText style={{ fontWeight: 400 }}>
|
||||||
|
download all journal entries into a JSON file
|
||||||
|
</ProfileSectionText>
|
||||||
|
</div>
|
||||||
|
</ProfileSectionHeader>
|
||||||
|
{files.length ? (
|
||||||
|
<a
|
||||||
|
download={files[0].name}
|
||||||
|
href={files[0].data}
|
||||||
|
style={{ textDecoration: "none" }}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
colors={theme.colors}
|
||||||
|
onClick={() => {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.clearFiles()
|
||||||
|
this.setState({ exporting: 0, files: [] })
|
||||||
|
}, 1500)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Download
|
||||||
|
</Button>
|
||||||
|
</a>
|
||||||
|
) : (
|
||||||
|
<Button colors={theme.colors} onClick={() => this.prepareExport()}>
|
||||||
|
{exporting ? (
|
||||||
|
<BeatLoader
|
||||||
|
color={theme.colors.secondary}
|
||||||
|
size={10}
|
||||||
|
margin="4px"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
"Export"
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</ProfileSection>
|
||||||
</ProfileGrid>
|
</ProfileGrid>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -71,5 +144,6 @@ class User extends React.Component {
|
|||||||
|
|
||||||
export default compose(
|
export default compose(
|
||||||
withFirebase,
|
withFirebase,
|
||||||
withAuthentication
|
withAuthentication,
|
||||||
|
withTheme
|
||||||
)(User)
|
)(User)
|
||||||
|
|||||||
Reference in New Issue
Block a user