fix: autosave with incorrect dates after navigating
This commit is contained in:
@@ -3,6 +3,7 @@ import styled from "@emotion/styled"
|
|||||||
/** @jsx jsx */
|
/** @jsx jsx */
|
||||||
import { jsx, css, keyframes } from "@emotion/core"
|
import { jsx, css, keyframes } from "@emotion/core"
|
||||||
import { compose } from "recompose"
|
import { compose } from "recompose"
|
||||||
|
import { Prompt } from "react-router"
|
||||||
import { withRouter } from "react-router-dom"
|
import { withRouter } from "react-router-dom"
|
||||||
import { withTheme } from "emotion-theming"
|
import { withTheme } from "emotion-theming"
|
||||||
import { withFirebase } from "../../firebase"
|
import { withFirebase } from "../../firebase"
|
||||||
@@ -150,10 +151,15 @@ class Day extends React.Component {
|
|||||||
onChangeText = e => {
|
onChangeText = e => {
|
||||||
if (this.timeout) clearTimeout(this.timeout)
|
if (this.timeout) clearTimeout(this.timeout)
|
||||||
const text = e.target.value
|
const text = e.target.value
|
||||||
|
const {
|
||||||
|
match: {
|
||||||
|
params: { year, month, day },
|
||||||
|
},
|
||||||
|
} = this.props
|
||||||
|
|
||||||
this.setState({ text, lastEditedAt: new Date() })
|
this.setState({ text, lastEditedAt: new Date() })
|
||||||
this.timeout = setTimeout(() => {
|
this.timeout = setTimeout(() => {
|
||||||
this.saveText(text)
|
this.saveText(text, year, month, day)
|
||||||
}, AUTOSAVE_DELAY)
|
}, AUTOSAVE_DELAY)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,6 +167,11 @@ class Day extends React.Component {
|
|||||||
const entryTextArea = document.getElementById("entry-text-area")
|
const entryTextArea = document.getElementById("entry-text-area")
|
||||||
const cursorIndex = entryTextArea.selectionStart
|
const cursorIndex = entryTextArea.selectionStart
|
||||||
const { text } = this.state
|
const { text } = this.state
|
||||||
|
const {
|
||||||
|
match: {
|
||||||
|
params: { year, month, day },
|
||||||
|
},
|
||||||
|
} = this.props
|
||||||
const insertAt = (str, sub, pos) =>
|
const insertAt = (str, sub, pos) =>
|
||||||
`${str.slice(0, pos)}${sub}${str.slice(pos)}`
|
`${str.slice(0, pos)}${sub}${str.slice(pos)}`
|
||||||
const newText = insertAt(text, format(new Date(), "h:mma "), cursorIndex)
|
const newText = insertAt(text, format(new Date(), "h:mma "), cursorIndex)
|
||||||
@@ -168,15 +179,12 @@ class Day extends React.Component {
|
|||||||
text: newText,
|
text: newText,
|
||||||
})
|
})
|
||||||
entryTextArea.focus()
|
entryTextArea.focus()
|
||||||
this.saveText(newText)
|
this.saveText(newText, year, month, day)
|
||||||
}
|
}
|
||||||
|
|
||||||
saveText = text => {
|
saveText = (text, year, month, day) => {
|
||||||
this.setState({ saving: true })
|
this.setState({ saving: true })
|
||||||
const {
|
const {
|
||||||
match: {
|
|
||||||
params: { year, month, day },
|
|
||||||
},
|
|
||||||
firebase,
|
firebase,
|
||||||
authUser,
|
authUser,
|
||||||
} = this.props
|
} = this.props
|
||||||
@@ -210,9 +218,11 @@ class Day extends React.Component {
|
|||||||
const { text, loading, saving, lastSavedAt, lastEditedAt } = this.state
|
const { text, loading, saving, lastSavedAt, lastEditedAt } = this.state
|
||||||
const currentDay = new Date(year, month - 1, day)
|
const currentDay = new Date(year, month - 1, day)
|
||||||
if (!currentDay) return
|
if (!currentDay) return
|
||||||
|
const hasSavedChanges = lastSavedAt >= lastEditedAt
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<Prompt when={!hasSavedChanges} message="You have unsaved changes, are you sure you want to leave?" />
|
||||||
<Seek
|
<Seek
|
||||||
title={format(currentDay, "YYYY MMM DD - dddd")}
|
title={format(currentDay, "YYYY MMM DD - dddd")}
|
||||||
prev={format(subDays(currentDay, 1), "/YYYY/MM/DD")}
|
prev={format(subDays(currentDay, 1), "/YYYY/MM/DD")}
|
||||||
@@ -233,7 +243,7 @@ class Day extends React.Component {
|
|||||||
`}
|
`}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
) : lastSavedAt >= lastEditedAt ? (
|
) : hasSavedChanges ? (
|
||||||
`Last saved at ${format(lastSavedAt, "h:mma")}`
|
`Last saved at ${format(lastSavedAt, "h:mma")}`
|
||||||
) : (
|
) : (
|
||||||
"Unsaved changes"
|
"Unsaved changes"
|
||||||
@@ -255,6 +265,7 @@ class Day extends React.Component {
|
|||||||
<>
|
<>
|
||||||
<JournalEntryArea
|
<JournalEntryArea
|
||||||
id="entry-text-area"
|
id="entry-text-area"
|
||||||
|
autoFocus={true}
|
||||||
placeholder="Start writing..."
|
placeholder="Start writing..."
|
||||||
onChange={e => this.onChangeText(e)}
|
onChange={e => this.onChangeText(e)}
|
||||||
value={text}
|
value={text}
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ class Search extends Component {
|
|||||||
<SearchLayout>
|
<SearchLayout>
|
||||||
<SearchGrid>
|
<SearchGrid>
|
||||||
<Input
|
<Input
|
||||||
|
autoFocus={true}
|
||||||
value={searchInput}
|
value={searchInput}
|
||||||
onChange={e => this.onChange(e)}
|
onChange={e => this.onChange(e)}
|
||||||
type="text"
|
type="text"
|
||||||
|
|||||||
Reference in New Issue
Block a user