Skip to content

Commit

Permalink
Ask for confirmation when user leaves unsaved reply (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp authored Mar 28, 2023
1 parent 32f0dfb commit b839774
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
12 changes: 5 additions & 7 deletions frontend/src/components/posting/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,13 @@ export default class extends Form {
}

handleSuccess(success) {
snackbar.success(gettext("Reply has been edited."))
window.location = success.url.index

// keep form loading
this.setState({
isLoading: true,
})
this.setState({ isLoading: true })
this.close()

setGlobalState(false, this.onQuote)

snackbar.success(gettext("Reply has been edited."))
window.location = success.url.index
}

handleError(rejection) {
Expand Down
12 changes: 5 additions & 7 deletions frontend/src/components/posting/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,13 @@ export default class extends Form {
}

handleSuccess(success) {
snackbar.success(pgettext("post reply", "Your reply has been posted."))
window.location = success.url.index

// keep form loading
this.setState({
isLoading: true,
})
this.setState({ isLoading: true })
this.close()

setGlobalState(false, this.onQuote)

snackbar.success(pgettext("post reply", "Your reply has been posted."))
window.location = success.url.index
}

handleError(rejection) {
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/components/posting/start-private.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,11 @@ export default class extends Form {
}

handleSuccess(success) {
this.setState({ isLoading: true })
this.close()

snackbar.success(pgettext("post thread", "Your thread has been posted."))
window.location = success.url

// keep form loading
this.setState({
isLoading: true,
})
}

handleError(rejection) {
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/components/posting/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,11 @@ export default class extends Form {
}

handleSuccess(success) {
this.setState({ isLoading: true })
this.close()

snackbar.success(pgettext("post thread", "Your thread has been posted."))
window.location = success.url

// keep form loading
this.setState({
isLoading: true,
})
}

handleError(rejection) {
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/services/posting.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@ export class Posting {

this._isOpen = false
this._isClosing = false

this._beforeunloadSet = false
}

isOpen() {
return this._isOpen
}

setBeforeUnload() {
if (!this._beforeunloadSet) {
window.addEventListener("beforeunload", this.beforeUnload, { capture: true })
this._beforeunloadSet = true
}
}

unsetBeforeUnload() {
window.removeEventListener("beforeunload", this.beforeUnload, { capture: true })
this._beforeunloadSet = false
}

beforeUnload(event) {
event.returnValue = "true"
return "true"
}

open(props) {
if (this._isOpen === false) {
this._mode = props.mode
Expand All @@ -49,9 +68,12 @@ export class Posting {

this._mount.classList.add("show")
this._observer.observe(this._mount)
this.setBeforeUnload()
}

close = () => {
this.unsetBeforeUnload()

if (this._isOpen && !this._isClosing) {
this._isClosing = true
this._mount.classList.remove("show")
Expand Down
2 changes: 1 addition & 1 deletion misago/static/misago/js/misago.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion misago/static/misago/js/misago.js.map

Large diffs are not rendered by default.

0 comments on commit b839774

Please sign in to comment.