From b2143cae53395390b3d150faeca7f940a45df1ae Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 29 Jan 2020 22:50:10 -0500 Subject: [PATCH] [frio] Add new asynchronous submission of modal form - Add loading state for jot submit buttons --- include/conversation.php | 1 + mod/editpost.php | 1 + view/theme/frio/templates/jot-header.tpl | 46 ++++++++++++++++++++++-- view/theme/frio/templates/jot.tpl | 12 +++++-- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/include/conversation.php b/include/conversation.php index 0194e4ea56..a588484766 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -1214,6 +1214,7 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false) '$return_path' => $query_str, '$action' => 'item', '$share' => ($x['button'] ?? '') ?: DI::l10n()->t('Share'), + '$loading' => DI::l10n()->t('Loading...'), '$upload' => DI::l10n()->t('Upload photo'), '$shortupload' => DI::l10n()->t('upload photo'), '$attach' => DI::l10n()->t('Attach file'), diff --git a/mod/editpost.php b/mod/editpost.php index 2bbc2f2b61..ca5db3e081 100644 --- a/mod/editpost.php +++ b/mod/editpost.php @@ -70,6 +70,7 @@ function editpost_content(App $a) '$return_path' => '/display/' . $item['guid'], '$action' => 'item', '$share' => DI::l10n()->t('Save'), + '$loading' => DI::l10n()->t('Loading...'), '$upload' => DI::l10n()->t('Upload photo'), '$shortupload' => DI::l10n()->t('upload photo'), '$attach' => DI::l10n()->t('Attach file'), diff --git a/view/theme/frio/templates/jot-header.tpl b/view/theme/frio/templates/jot-header.tpl index 9f226cda49..67b136ae16 100644 --- a/view/theme/frio/templates/jot-header.tpl +++ b/view/theme/frio/templates/jot-header.tpl @@ -67,11 +67,53 @@ jotTextOpenUI(document.getElementById("profile-jot-text")); jotActive(); addeditortext(embedcode); - }); - $('body').on('fbrowser.file.main', function(e, filename, embedcode, id) { + }) + .on('fbrowser.file.main', function(e, filename, embedcode, id) { jotTextOpenUI(document.getElementById("profile-jot-text")); jotActive(); addeditortext(embedcode); + }) + // Asynchronous jot submission + .on('submit', '#profile-jot-form', function (e) { + e.preventDefault(); + + // Disable jot submit buttons during processing + let $share = $('#profile-jot-submit').button('loading'); + let $sharePreview = $('#profile-jot-preview-submit').button('loading'); + + let formData = new FormData(e.target); + // This cancels the automatic redirection after item submission + formData.delete('return'); + + $.ajax({ + url: 'item', + data: formData, + processData: false, + contentType: false, + type: 'POST', + }) + .then(function () { + // Reset to form for jot reuse in the same page + e.target.reset(); + $('#jot-modal').modal('hide'); + }) + .always(function() { + // Reset the post_id_random to avoid duplicate post errors + let new_post_id_random = Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER - (Number.MAX_SAFE_INTEGER / 10))) + Number.MAX_SAFE_INTEGER / 10; + $('#profile-jot-form [name=post_id_random]').val(new_post_id_random); + + // Reset jot submit button state + $share.button('reset'); + $sharePreview.button('reset'); + + // Force the display update of the edited post/comment + if (formData.get('post_id')) { + force_update = true; + update_item = formData.get('post_id'); + } + + NavUpdate(); + }) }); $('#wall-image-upload').on('click', function(){ diff --git a/view/theme/frio/templates/jot.tpl b/view/theme/frio/templates/jot.tpl index d4a430eccf..fe756880fe 100644 --- a/view/theme/frio/templates/jot.tpl +++ b/view/theme/frio/templates/jot.tpl @@ -104,7 +104,11 @@
  • --> - +