From b5bd4cb2e2313b91ed0701dc3b6c99ad2d4ea887 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sat, 23 Jul 2022 16:38:48 +0200
Subject: [PATCH] #677 - differentiate between user clicked button "save" or
 "save and continue"

---
 .../main/resources/static/js/transactions.js  | 35 ++++++++++++-------
 .../resources/templates/helpers/header.ftl    |  4 +--
 .../templates/templates/newTemplate.ftl       |  2 +-
 .../transactions/newTransactionMacros.ftl     |  4 +--
 .../transactions/newTransactionNormal.ftl     |  2 +-
 5 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/BudgetMasterServer/src/main/resources/static/js/transactions.js b/BudgetMasterServer/src/main/resources/static/js/transactions.js
index 46d684333..ccb8fe381 100644
--- a/BudgetMasterServer/src/main/resources/static/js/transactions.js
+++ b/BudgetMasterServer/src/main/resources/static/js/transactions.js
@@ -381,12 +381,12 @@ function convertDateWithoutDots(dateString)
     return dateString.substr(0, 2) + '.' + dateString.substr(2, 2) + '.' + dateString.substr(4, yearLength);
 }
 
-function validateForm(allowEmptyAmount = false, skipKeywordCheck = false)
+function validateForm(isSaveAndContinue = false, allowEmptyAmount = false, skipKeywordCheck = false)
 {
     // name (keyword check)
     if(!skipKeywordCheck)
     {
-        let nameContainsKeywords = checkNameForKeywords();
+        let nameContainsKeywords = checkNameForKeywords(isSaveAndContinue);
         if(nameContainsKeywords)
         {
             return false;
@@ -477,7 +477,7 @@ function validateForm(allowEmptyAmount = false, skipKeywordCheck = false)
     return true;
 }
 
-function checkNameForKeywords()
+function checkNameForKeywords(isSaveAndContinue)
 {
     let url = document.getElementById('keywordCheckUrl').dataset.url;
     let transactionName = document.getElementById('transaction-name').value;
@@ -495,7 +495,7 @@ function checkNameForKeywords()
             {
                 // name contains at least one keyword
                 result = true;
-                openKeywordWarningModal(data);
+                openKeywordWarningModal(data, isSaveAndContinue);
             }
             else
             {
@@ -511,7 +511,7 @@ function checkNameForKeywords()
     return result;
 }
 
-function openKeywordWarningModal(htmlData)
+function openKeywordWarningModal(htmlData, isSaveAndContinue)
 {
     let modalID = '#modalTransactionNameKeywordWarning';
 
@@ -524,21 +524,30 @@ function openKeywordWarningModal(htmlData)
     {
         $(modalID).modal('close');
 
+        let button;
+        if(isSaveAndContinue)
+        {
+            button = document.getElementById('button-save-transaction-and-continue');
+        }
+        else
+        {
+            button = document.getElementById('button-save-transaction');
+        }
+
         let allowEmptyAmount = document.getElementById('template-name') !== null;
 
-        // rebind onsubmit function to skip keyword check once
-        document.getElementById('mainForm').onsubmit = function()
+        // rebind onclick function of button to skip keyword check once
+        button.onclick = function()
         {
-            return validateForm(allowEmptyAmount, true);
+            return validateForm(isSaveAndContinue, allowEmptyAmount, true);
         };
 
-        // TODO differentiate between user clicked button "save" or "save and continue" before
-        document.getElementById('button-save-transaction').click();
+        button.click();
 
-        // reset onsubmit function in case user edits transaction name too after fixing validation errors
-        document.getElementById('mainForm').onsubmit = function()
+        // reset onsubmit function of button in case user edits transaction name too after fixing validation errors
+        button.onclick = function()
         {
-            return validateForm(allowEmptyAmount, false);
+            return validateForm(isSaveAndContinue, allowEmptyAmount, false);
         };
     });
 }
diff --git a/BudgetMasterServer/src/main/resources/templates/helpers/header.ftl b/BudgetMasterServer/src/main/resources/templates/helpers/header.ftl
index d25b1bb0a..3b1c9a7be 100644
--- a/BudgetMasterServer/src/main/resources/templates/helpers/header.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/helpers/header.ftl
@@ -130,8 +130,8 @@
     </a>
 </#macro>
 
-<#macro buttonSubmit name icon localizationKey id="" color="background-blue" classes="" disabled=false formaction="" value="">
-    <button id="${id}" class="btn waves-effect waves-light ${color} ${classes}" type="submit" name="${name}" <#if disabled>disabled</#if> <#if formaction?has_content>formaction="<@s.url formaction/>"</#if> <#if value?has_content>value="${value}"</#if>>
+<#macro buttonSubmit name icon localizationKey id="" color="background-blue" classes="" disabled=false formaction="" value="" onclick="">
+    <button id="${id}" class="btn waves-effect waves-light ${color} ${classes}" type="submit" name="${name}" <#if disabled>disabled</#if> <#if formaction?has_content>formaction="<@s.url formaction/>"</#if> <#if value?has_content>value="${value}"</#if> <#if onclick??>onclick="${onclick}"</#if>>
         <i class="material-icons left <#if !localizationKey?has_content>no-margin</#if>">${icon}</i><#if localizationKey?has_content>${locale.getString(localizationKey)}</#if>
     </button>
 </#macro>
diff --git a/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl b/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl
index c0d5c73e2..497073d3d 100644
--- a/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl
@@ -36,7 +36,7 @@
                 <@header.content>
                     <div class="container">
                     <#import "../helpers/validation.ftl" as validation>
-                    <form id="mainForm" name="NewTemplate" action="<@s.url '/templates/newTemplate'/>" method="post" onsubmit="return validateForm(true)">
+                    <form id="mainForm" name="NewTemplate" action="<@s.url '/templates/newTemplate'/>" method="post">
                         <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                         <input type="hidden" name="ID" value="<#if template.getID()??>${template.getID()?c}</#if>">
                         <input type="hidden" name="templateGroup" value="<#if template.getTemplateGroup()??>${template.getTemplateGroup().getID()?c}</#if>">
diff --git a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionMacros.ftl b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionMacros.ftl
index 0f09255a7..6d871b891 100644
--- a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionMacros.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionMacros.ftl
@@ -346,11 +346,11 @@
 </#macro>
 
 <#macro buttonSave>
-    <@header.buttonSubmit name='action' icon='save' localizationKey='save' id='button-save-transaction' color='green'/>
+    <@header.buttonSubmit name='action' icon='save' localizationKey='save' id='button-save-transaction' color='green' onclick='return validateForm(false, false, false)'/>
 </#macro>
 
 <#macro buttonSaveAndContinue>
-    <@header.buttonSubmit name='action' icon='save' localizationKey='saveAndContinue' id='button-save-transaction-and-continue' value='continue'/>
+    <@header.buttonSubmit name='action' icon='save' localizationKey='saveAndContinue' id='button-save-transaction-and-continue' value='continue'  onclick='return validateForm(true, false, false)'/>
 </#macro>
 
 <#macro buttonTransactionActions canChangeType canCreateTemplate changeTypeInProgress>
diff --git a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl
index cf77495d3..c529aaa42 100644
--- a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl
@@ -33,7 +33,7 @@
                 <@header.content>
                     <div class="container">
                         <#import "../helpers/validation.ftl" as validation>
-                        <form id="mainForm" name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
+                        <form id="mainForm" name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post">
                             <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                             <!-- only set ID for transactions not templates, otherwise the input is filled with the template ID and saving the transaction
                             may then override an existing transactions if the ID is also already used in transactions table -->
-- 
GitLab