Skip to content

Commit 15d923b

Browse files
authored
Merge pull request #1873 from roland-ruedenauer/fix_handling_of_flash_messages
User settings: fix handling of flash messages (#1872)
2 parents c689e72 + 7dcf6fe commit 15d923b

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

src/moin/apps/frontend/views.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,26 +2617,25 @@ class UserSettingsUIForm(Form):
26172617
# validation failed
26182618
response["flash"].append((_("Nothing saved."), "error"))
26192619

2620+
# if no flash message was added until here, we add a generic success message
26202621
if not response["flash"]:
2621-
# if no flash message was added until here, we add a generic success message
26222622
msg = _("Your changes have been saved.")
26232623
response["flash"].append((msg, "info"))
2624-
repeat_flash_msg(msg, "info")
2625-
2626-
if response["redirect"] is not None or not is_xhr:
2627-
# if we redirect or it is no XHR request, we just flash() the messages normally
2628-
for f in response["flash"]:
2629-
flash(*f)
26302624

2625+
# if it is a XHR request, render the part from the usersettings_ajax.html template
2626+
# and send the response encoded as an JSON object;
2627+
# the client side is responsible for displaying any flash messages
26312628
if is_xhr:
2632-
# if it is a XHR request, render the part from the usersettings_ajax.html template
2633-
# and send the response encoded as an JSON object
26342629
response["form"] = render_template("usersettings_ajax.html", part=part, form=form)
26352630
return jsonify(**response)
2636-
else:
2637-
# if it is not a XHR request but there is an redirect pending, we use a normal HTTP redirect
2638-
if response["redirect"] is not None:
2639-
return redirect(response["redirect"])
2631+
2632+
# if no XHR request, we just flash() the messages normally
2633+
for f in response["flash"]:
2634+
flash(*f)
2635+
2636+
# if there is a redirect pending, use a normal HTTP redirect
2637+
if response["redirect"] is not None:
2638+
return redirect(response["redirect"])
26402639

26412640
# if the view did not return until here, we add the current form to the forms dict
26422641
# and continue with rendering the normal template

src/moin/static/js/common.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,36 @@ MoinMoin.prototype.toggleSubtree = function (item) {
323323
};
324324

325325

326+
MoinMoin.prototype.displayFlashMessages = function (messages) {
327+
for (i = 0; i < messages.length; i += 1) {
328+
f = $(document.createElement('p'));
329+
f.html(messages[i][0]);
330+
f.addClass('moin-flash');
331+
f.addClass('moin-flash-javascript');
332+
f.addClass('moin-flash-' + messages[i][1]);
333+
$(f).click(function () {
334+
this.remove();
335+
});
336+
$('#moin-flash').append(f);
337+
}
338+
}
339+
340+
// remove all flash messages previously added via javascript
341+
MoinMoin.prototype.clearFlashMessages = function () {
342+
$('#moin-flash .moin-flash-javascript').remove();
343+
}
344+
345+
MoinMoin.prototype.saveFlashMessages = function (messages) {
346+
localStorage.setItem("moin-flash-messages", JSON.stringify(messages))
347+
}
348+
349+
MoinMoin.prototype.restoreFlashMessages = function () {
350+
messages = JSON.parse(localStorage.getItem("moin-flash-messages") || "[]")
351+
localStorage.removeItem("moin-flash-messages")
352+
this.clearFlashMessages()
353+
this.displayFlashMessages(messages)
354+
}
355+
326356
// User Settings page enhancements - make long multi-form page appear as a shorter page
327357
// with a row of tabs at the top or side that may be clicked to select a form.
328358
MoinMoin.prototype.enhanceUserSettings = function () {
@@ -421,20 +451,10 @@ MoinMoin.prototype.enhanceUserSettings = function () {
421451
clearInterval(buttonDotAnimation);
422452
// if the response indicates a redirect, set the new location
423453
if (data.redirect) {
454+
MoinMoin.prototype.saveFlashMessages(data.flash)
424455
location.href = data.redirect;
425456
return;
426457
}
427-
// remove all flash messages previously added via javascript
428-
$('#moin-flash .moin-flash-javascript').remove();
429-
// add new flash messages from the response
430-
for (i = 0; i < data.flash.length; i += 1) {
431-
f = $(document.createElement('p'));
432-
f.html(data.flash[i][0]);
433-
f.addClass('moin-flash');
434-
f.addClass('moin-flash-javascript');
435-
f.addClass('moin-flash-' + data.flash[i][1]);
436-
$('#moin-flash').append(f);
437-
}
438458
// get the new form element from the response
439459
newform = $(data.form);
440460
// set event handlers on the new form
@@ -445,9 +465,13 @@ MoinMoin.prototype.enhanceUserSettings = function () {
445465
// replace the old form with the new one
446466
form.replaceWith(newform);
447467
if (ev.currentTarget.id === 'usersettings_ui' || ev.currentTarget.id === 'usersettings_personal') {
468+
MoinMoin.prototype.saveFlashMessages(data.flash)
448469
// theme or language may have changed, show user the new theme/language
449470
location.reload(true);
471+
return;
450472
}
473+
// show any flash messages received with the server response
474+
MoinMoin.prototype.displayFlashMessages(data.flash)
451475
}, 'json');
452476
return false;
453477
}
@@ -848,4 +872,5 @@ $(document).ready(function () {
848872
// placing initToggleComments after enhanceEdit prevents odd autoscroll issue when editing hidden comments
849873
moin.initToggleComments();
850874

875+
moin.restoreFlashMessages();
851876
});

0 commit comments

Comments
 (0)