Skip to content

Commit

Permalink
#724 - added column to edit category
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlocker8 committed Jan 18, 2023
1 parent 1ae770a commit fafc9cd
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private void overrideFieldsFromCsvTransaction(WebRequest request, Transaction tr
transaction.setDate(csvTransaction.getDate());
transaction.setAmount(csvTransaction.getAmount());
transaction.setIsExpenditure(csvTransaction.getAmount() <= 0);
// TODO: set category from CsvTransaction
transaction.setCategory(csvTransaction.getCategory());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.deadlocker8.budgetmaster.transactions;

import de.deadlocker8.budgetmaster.accounts.AccountService;
import de.deadlocker8.budgetmaster.categories.Category;
import de.deadlocker8.budgetmaster.categories.CategoryService;
import de.deadlocker8.budgetmaster.categories.CategoryType;
import de.deadlocker8.budgetmaster.controller.BaseController;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class TransactionImportController extends BaseController
private static class ModelAttributes
{
public static final String ERROR = "error";
public static final String CATEGORIES = "categories";
}

private static class ReturnValues
Expand Down Expand Up @@ -92,6 +94,8 @@ public String transactionImport(WebRequest request, Model model)
model.addAttribute(ModelAttributes.ERROR, bindingResult);
}

model.addAttribute(ModelAttributes.CATEGORIES, categoryService.getAllEntitiesAsc());

return ReturnValues.TRANSACTION_IMPORT;
}

Expand Down Expand Up @@ -206,7 +210,8 @@ private CsvTransaction createCsvTransactionFromCsvRow(CsvRow csvRow, CsvColumnSe
throw new CsvTransactionParseException(Localization.getString("transactions.import.error.parse.amount", index + 1));
}

return new CsvTransaction(parsedDateOptional.get(), name, parsedAmountOptional.get(), description, CsvTransactionStatus.PENDING);
final Category categoryNone = categoryService.findByType(CategoryType.NONE);
return new CsvTransaction(parsedDateOptional.get(), name, parsedAmountOptional.get(), description, CsvTransactionStatus.PENDING, categoryNone);
}

@GetMapping("/cancel")
Expand Down Expand Up @@ -288,6 +293,7 @@ public String newTransactionInPlace(WebRequest request,
// update original CsvTransaction attributes with values from user (from newCsvTransaction)
csvTransaction.setName(newCsvTransaction.getName());
csvTransaction.setDescription(newCsvTransaction.getDescription());
csvTransaction.setCategory(newCsvTransaction.getCategory());

final Transaction newTransaction = createTransactionFromCsvTransaction(csvTransaction);
transactionService.getRepository().save(newTransaction);
Expand All @@ -304,8 +310,7 @@ private Transaction createTransactionFromCsvTransaction(CsvTransaction csvTransa
newTransaction.setAmount(csvTransaction.getAmount());
newTransaction.setIsExpenditure(csvTransaction.getAmount() <= 0);
newTransaction.setAccount(helpers.getCurrentAccountOrDefault());
// TODO: set category from CsvTransaction
newTransaction.setCategory(categoryService.findByType(CategoryType.NONE));
newTransaction.setCategory(csvTransaction.getCategory());

return newTransaction;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.deadlocker8.budgetmaster.transactions.csvimport;

import de.deadlocker8.budgetmaster.categories.Category;

import java.time.LocalDate;
import java.util.Objects;

Expand All @@ -10,14 +12,16 @@ public final class CsvTransaction
private final Integer amount;
private String description;
private CsvTransactionStatus status;
private Category category;

public CsvTransaction(LocalDate date, String name, Integer amount, String description, CsvTransactionStatus status)
public CsvTransaction(LocalDate date, String name, Integer amount, String description, CsvTransactionStatus status, Category category)
{
this.date = date;
this.name = name;
this.amount = amount;
this.description = description;
this.status = status;
this.category = category;
}

public LocalDate getDate()
Expand Down Expand Up @@ -60,30 +64,41 @@ public void setStatus(CsvTransactionStatus status)
this.status = status;
}

public Category getCategory()
{
return category;
}

public void setCategory(Category category)
{
this.category = category;
}

@Override
public boolean equals(Object o)
{
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
CsvTransaction that = (CsvTransaction) o;
return Objects.equals(date, that.date) && Objects.equals(name, that.name) && Objects.equals(amount, that.amount) && Objects.equals(description, that.description) && status == that.status;
return Objects.equals(date, that.date) && Objects.equals(name, that.name) && Objects.equals(amount, that.amount) && Objects.equals(description, that.description) && status == that.status && Objects.equals(category, that.category);
}

@Override
public int hashCode()
{
return Objects.hash(date, name, amount, description, status);
return Objects.hash(date, name, amount, description, status, category);
}

@Override
public String toString()
{
return "CsvTransaction{" +
"date='" + date + '\'' +
"date=" + date +
", name='" + name + '\'' +
", amount=" + amount +
", description='" + description + '\'' +
", status=" + status +
", category=" + category +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@
border-radius: 0;
}

/* override custom select styling*/
.category-select-wrapper {
margin: 0;
}

.category-select-margin-top {
margin-top: 1rem;
}

.custom-select-trigger {
width: 50px;
}

.custom-select-options {
width: 50vmin;
}

/* label focus color */
#table-transaction-rows_filter input[type=search]:focus + label {
color: var(--color-blue) !important;
Expand Down
14 changes: 11 additions & 3 deletions BudgetMasterServer/src/main/resources/static/js/customSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ function initCustomSelects()
let allCustomSelects = [];

let selectorCategorySelect = '.category-select-wrapper';
if($(selectorCategorySelect).length)
let transactionCategorySelects = document.querySelectorAll(selectorCategorySelect);

for(let i = 0; i < transactionCategorySelects.length; i++)
{
let categorySelect = new CustomSelect(selectorCategorySelect);
let id = transactionCategorySelects[i].id;
let categorySelect = new CustomSelect('#' + id);
categorySelect.init();
allCustomSelects.push(categorySelect);
}
Expand Down Expand Up @@ -248,7 +251,12 @@ class CustomSelect
item.classList.add('selected');

let itemSelector = document.querySelector(this.selector + ' .custom-select-selected-item');
let isOnlyIcon = itemSelector.querySelector('.custom-select-item-name') === null;
itemSelector.innerHTML = item.innerHTML;
if(isOnlyIcon)
{
itemSelector.querySelector('.custom-select-item-name').remove();
}

let itemCircle = itemSelector.querySelector('.category-circle');
itemCircle.classList.add('no-margin-left');
Expand All @@ -258,7 +266,7 @@ class CustomSelect

this.removeSelectionStyleClassFromAll();
this.close(this.selector);
this.selectedId = this.resetSelectedItemId();
this.resetSelectedItemId();

if(this.confirmSelectionCallback && typeof (this.confirmSelectionCallback) == "function")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<form name="DestinationCategory" id="formDestinationCategory" action="<@s.url '/categories/${categoryToDelete.ID?c}/delete'/>" method="post">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<#import "../helpers/validation.ftl" as validation>
<@customSelectMacros.customCategorySelect availableCategories preselectedCategory "col s12 m12 l8 offset-l2" locale.getString("info.title.category.delete.move")/>
<@customSelectMacros.customCategorySelect availableCategories preselectedCategory "col s12 m12 l8 offset-l2" locale.getString("info.title.category.delete.move") "transaction-category"/>
</form>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<#import "../categories/categoriesFunctions.ftl" as categoriesFunctions>

<#macro customSelectStart selector items inputClasses labelText id icon disabled=false>
<div class="row">
<#macro customSelectStart selector items inputClasses labelText id icon disabled=false rowClasses="">
<div class="row ${rowClasses}">
<div class="input-field ${inputClasses}">
<i class="material-icons prefix">${icon}</i>
<label class="input-label" for="${id}">${labelText}</label>
<#if labelText?has_content>
<label class="input-label" for="${id}">${labelText}</label>
</#if>
<div class="custom-select-wrapper ${selector} <#if disabled>disabled</#if>" id="${id}">
<div class="custom-select">
<#nested>
Expand All @@ -18,11 +20,11 @@
</div>
</#macro>

<#macro customCategorySelect categories selectedCategory inputClasses labelText>
<@customSelectStart "category-select-wrapper" categories inputClasses labelText "transaction-category" "label">
<#macro customCategorySelect categories selectedCategory inputClasses labelText id showName=true rowClasses="">
<@customSelectStart "category-select-wrapper" categories inputClasses labelText id "label" false rowClasses>
<div class="custom-select-trigger" tabindex="0">
<div class="custom-select-selected-item">
<#if selectedCategory??><@customSelectOptionCategoryContent category=selectedCategory classes="no-margin-left" datasetValue=true/></#if>
<#if selectedCategory??><@customSelectOptionCategoryContent category=selectedCategory classes="no-margin-left" datasetValue=true showName=showName/></#if>
</div>
<div class="custom-select-arrow"></div>
</div>
Expand Down Expand Up @@ -140,9 +142,11 @@
</div>
</#macro>

<#macro customSelectOptionCategoryContent category classes="" datasetValue=false>
<#macro customSelectOptionCategoryContent category classes="" datasetValue=false showName=true>
<@categoriesFunctions.categoryCircle category=category classes="category-circle-small ${classes}" datasetValue=datasetValue/>
<span class="custom-select-item-name">${categoriesFunctions.getCategoryName(category)}</span>
<#if showName>
<span class="custom-select-item-name">${categoriesFunctions.getCategoryName(category)}</span>
</#if>
</#macro>

<#macro customSelectAccountOption account isSelected>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<@newTransactionMacros.transactionAmount template/>

<#-- category -->
<@customSelectMacros.customCategorySelect categories template.getCategory() "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.category")/>
<@customSelectMacros.customCategorySelect categories template.getCategory() "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.category") "transaction-category"/>

<#-- description -->
<@newTransactionMacros.transactionDescription template/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<@newTransactionMacros.transactionAmount transaction/>

<#-- category -->
<@customSelectMacros.customCategorySelect categories transaction.getCategory() "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.category")/>
<@customSelectMacros.customCategorySelect categories transaction.getCategory() "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.category") "transaction-category"/>

<#-- date -->
<@newTransactionMacros.transactionStartDate transaction currentDate/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<@newTransactionMacros.transactionAmount transaction/>

<#-- category -->
<@customSelectMacros.customCategorySelect categories transaction.getCategory() "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.category")/>
<@customSelectMacros.customCategorySelect categories transaction.getCategory() "col s12 m12 l8 offset-l2" locale.getString("transaction.new.label.category") "transaction-category"/>

<#-- date -->
<@newTransactionMacros.transactionStartDate transaction currentDate/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<@navbar.navbar "importCSV" settings/>

<#import "transactionsMacros.ftl" as transactionMacros>
<#import "../helpers/customSelectMacros.ftl" as customSelectMacros>

<main>
<div class="card main-card background-color">
Expand Down Expand Up @@ -223,6 +224,7 @@
<tr>
<td class="bold">${locale.getString("transactions.import.status")}</td>
<td class="bold">${locale.getString("transaction.new.label.date")}</td>
<td class="bold">${locale.getString("transaction.new.label.category")}</td>
<td class="bold">${locale.getString("transaction.new.label.name")}</td>
<td class="bold">${locale.getString("transaction.new.label.description")}</td>
<td class="bold">${locale.getString("transaction.new.label.amount")}</td>
Expand All @@ -245,6 +247,9 @@
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<td data-order="${locale.getString(csvTransaction.getStatus().getLocalizationKey())}" data-search="${locale.getString(csvTransaction.getStatus().getLocalizationKey())}"><@statusBanner csvTransaction.getStatus()/></td>
<td data-order="${csvTransaction.getDate()}" data-search="${csvTransaction.getDate()}">${csvTransaction.getDate()}</td>
<td data-order="${csvTransaction.getCategory().getName()}" data-search="${csvTransaction.getCategory().getName()}">
<@customSelectMacros.customCategorySelect categories csvTransaction.getCategory() "left-align no-margin-top no-margin-bottom" "" "csvTransaction-category-${index}" false "no-margin-bottom"/>
</td>
<td data-order="${csvTransaction.getName()}" data-search="${csvTransaction.getName()}">
<div class="input-field no-margin-top no-margin-bottom">
<input class="no-margin-bottom" type="text" name="name" required value="${csvTransaction.getName()}">
Expand Down

0 comments on commit fafc9cd

Please sign in to comment.