Skip to content
Snippets Groups Projects
Commit a96b49ac authored by Robert Goldmann's avatar Robert Goldmann
Browse files

Fixed #501 - New transaction: show name suggestions

parent 5c2060d5
Branches
Tags
1 merge request!229Feature/index page
Pipeline #2746 passed
package de.deadlocker8.budgetmaster.transactions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import de.deadlocker8.budgetmaster.accounts.Account;
import de.deadlocker8.budgetmaster.accounts.AccountService;
import de.deadlocker8.budgetmaster.categories.CategoryService;
......@@ -21,6 +25,7 @@ import de.deadlocker8.budgetmaster.tags.TagService;
import de.deadlocker8.budgetmaster.utils.ResourceNotFoundException;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.ISODateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -29,14 +34,18 @@ import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@Controller
public class TransactionController extends BaseController
{
private static final int MAX_SUGGESTIONS = 25;
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.create();
private final TransactionService transactionService;
private final CategoryService categoryService;
private final AccountService accountService;
......@@ -288,6 +297,14 @@ public class TransactionController extends BaseController
model.addAttribute("transaction", emptyTransaction);
model.addAttribute("settings", settingsService.getSettings());
model.addAttribute("isPayment", isPayment);
final List<Transaction> allByOrderByDateDesc = transactionService.getRepository().findAllByOrderByDateDesc();
final List<String> nameSuggestions = allByOrderByDateDesc.stream()
.map(Transaction::getName)
.distinct()
.limit(MAX_SUGGESTIONS)
.collect(Collectors.toList());
model.addAttribute("suggestionsJSON", GSON.toJson(nameSuggestions));
}
private Transaction addTagForTransaction(String name, Transaction transaction)
......
......@@ -33,4 +33,6 @@ public interface TransactionRepository extends JpaRepository<Transaction, Intege
Integer getRestForTransferDestination(int accountID, String startDate, String endDate);
List<Transaction> findAllByTransferAccount(Account account);
List<Transaction> findAllByOrderByDateDesc();
}
\ No newline at end of file
......@@ -10,6 +10,10 @@ $(document).ready(function()
if($("#transaction-name").length)
{
var elements = document.querySelectorAll('#transaction-name');
M.Autocomplete.init(elements, {
data: transactionNameSuggestions,
});
document.getElementById('transaction-name').focus();
}
......
......@@ -46,13 +46,21 @@
<a class="waves-effect waves-light btn ${color} buttonExpenditure"><i class="material-icons left">file_upload</i>${locale.getString("title.expenditure")}</a>
</#macro>
<#macro transactionName transaction>
<#macro transactionName transaction suggestionsJSON>
<div class="row">
<div class="input-field col s12 m12 l8 offset-l2">
<input id="transaction-name" type="text" name="name" <@validation.validation "name"/> value="<#if transaction.getName()??>${transaction.getName()}</#if>">
<input class="autocomplete" autocomplete="off" id="transaction-name" type="text" name="name" <@validation.validation "name"/> value="<#if transaction.getName()??>${transaction.getName()}</#if>">
<label for="transaction-name">${locale.getString("transaction.new.label.name")}</label>
</div>
</div>
<script>
transactionNameSuggestions = {};
var nameSuggestions = ${suggestionsJSON};
nameSuggestions.forEach(function(name){
transactionNameSuggestions[name] = null;
});
</script>
</#macro>
<#macro transactionAmount transaction>
......
......@@ -31,7 +31,7 @@
<@newTransactionMacros.isExpenditureSwitch transaction isPayment/>
<#-- name -->
<@newTransactionMacros.transactionName transaction/>
<@newTransactionMacros.transactionName transaction suggestionsJSON/>
<#-- amount -->
<@newTransactionMacros.transactionAmount transaction/>
......
......@@ -32,7 +32,7 @@
<@newTransactionMacros.isExpenditureSwitch transaction isPayment/>
<#-- name -->
<@newTransactionMacros.transactionName transaction/>
<@newTransactionMacros.transactionName transaction suggestionsJSON/>
<#-- amount -->
<@newTransactionMacros.transactionAmount transaction/>
......
......@@ -29,7 +29,7 @@
<input type="hidden" name="isPayment" value="true">
<#-- name -->
<@newTransactionMacros.transactionName transaction/>
<@newTransactionMacros.transactionName transaction suggestionsJSON/>
<#-- amount -->
<@newTransactionMacros.transactionAmount transaction/>
......
......@@ -301,6 +301,12 @@ public class DatabaseImportTest
return null;
}
@Override
public List<Transaction> findAllByOrderByDateDesc()
{
return null;
}
@Override
public List<Transaction> findAll()
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment