Replace modelValue by models for id and name

This commit is contained in:
Jan Bader 2022-02-25 21:09:31 +00:00
parent a452482381
commit be3829baf8
4 changed files with 32 additions and 41 deletions

View File

@ -8,21 +8,15 @@ export interface Suggestion {
Name: string Name: string
} }
interface Data {
Selected: Suggestion | undefined
SearchQuery: String
Suggestions: Suggestion[]
}
const props = defineProps<{ const props = defineProps<{
modelValue: Suggestion | undefined, text: String,
id: String | undefined,
type: String type: String
}>(); }>();
const Selected = ref<Suggestion | undefined>(props.modelValue || undefined); const SearchQuery = ref(props.text || "");
const SearchQuery = ref(props.modelValue?.Name || "");
const Suggestions = ref<Array<Suggestion>>([]); const Suggestions = ref<Array<Suggestion>>([]);
const emit = defineEmits(["update:modelValue"]); const emit = defineEmits(["update:id", "update:text"]);
watch(SearchQuery, () => { watch(SearchQuery, () => {
load(SearchQuery.value); load(SearchQuery.value);
}); });
@ -30,7 +24,8 @@ function saveTransaction(e: MouseEvent) {
e.preventDefault(); e.preventDefault();
}; };
function load(text: String) { function load(text: String) {
emit('update:modelValue', { ID: null, Name: text }); emit('update:id', null);
emit('update:text', text);
if (text == "") { if (text == "") {
Suggestions.value = []; Suggestions.value = [];
return; return;
@ -56,13 +51,12 @@ function keypress(e: KeyboardEvent) {
const currentIndex = inputElements.indexOf(el); const currentIndex = inputElements.indexOf(el);
const nextElement = inputElements[currentIndex < inputElements.length - 1 ? currentIndex + 1 : 0]; const nextElement = inputElements[currentIndex < inputElements.length - 1 ? currentIndex + 1 : 0];
(<HTMLInputElement>nextElement).focus(); (<HTMLInputElement>nextElement).focus();
} }
}; };
function selectElement(element: Suggestion) { function selectElement(element: Suggestion) {
Selected.value = element; emit('update:id', element.ID);
emit('update:text', element.Name);
Suggestions.value = []; Suggestions.value = [];
emit('update:modelValue', element);
}; };
function select(e: MouseEvent) { function select(e: MouseEvent) {
const target = (<HTMLInputElement>e.target); const target = (<HTMLInputElement>e.target);
@ -74,8 +68,8 @@ function select(e: MouseEvent) {
selectElement(selected); selectElement(selected);
}; };
function clear() { function clear() {
Selected.value = undefined; emit('update:id', null);
emit('update:modelValue', { ID: null, Name: SearchQuery.value }); emit('update:text', SearchQuery.value);
}; };
</script> </script>
@ -84,10 +78,10 @@ function clear() {
<input <input
class="border-b-2 border-black" class="border-b-2 border-black"
@keypress="keypress" @keypress="keypress"
v-if="Selected == undefined" v-if="id == undefined"
v-model="SearchQuery" v-model="SearchQuery"
/> />
<span @click="clear" v-if="Selected != undefined" class="bg-gray-300">{{ Selected.Name }}</span> <span @click="clear" v-if="id != undefined" class="bg-gray-300">{{ text }}</span>
<div v-if="Suggestions.length > 0" class="absolute bg-gray-400 w-64 p-2"> <div v-if="Suggestions.length > 0" class="absolute bg-gray-400 w-64 p-2">
<span <span
v-for="suggestion in Suggestions" v-for="suggestion in Suggestions"

View File

@ -1,54 +1,49 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref } from "vue"; import { computed } from "vue";
import Autocomplete, { Suggestion } from './Autocomplete.vue' import Autocomplete from './Autocomplete.vue'
import { Transaction, useAccountStore } from '../stores/budget-account' import { useAccountStore } from '../stores/budget-account'
const props = defineProps<{ const props = defineProps<{
transaction: Transaction transactionid: string
}>() }>()
const TransactionDate = ref(props.transaction.Date.substring(0, 10)); const accountStore = useAccountStore();
const Payee = ref<Suggestion | undefined>({ID: "", Name: props.transaction.Payee}); const Transaction = accountStore.Transactions.get(props.transactionid)!;
const Category = ref<Suggestion | undefined>({ID: "", Name: props.transaction.Category});
const Memo = ref(props.transaction.Memo);
const Amount = ref(props.transaction.Amount);
const payload = computed(() => JSON.stringify({ const payload = computed(() => JSON.stringify({
date: TransactionDate.value, date: Transaction.Date,
payee: Payee.value, payee: Transaction.Payee,
category: Category.value, category: Transaction.Category,
memo: Memo.value, memo: Transaction.Memo,
amount: Amount.value, amount: Transaction.Amount,
state: "Uncleared" state: "Uncleared"
})); }));
const accountStore = useAccountStore();
function saveTransaction(e: MouseEvent) { function saveTransaction(e: MouseEvent) {
e.preventDefault(); e.preventDefault();
console.log(props.transaction); accountStore.saveTransaction(payload.value);
//accountStore.saveTransaction(payload.value);
} }
</script> </script>
<template> <template>
<tr> <tr>
<td style="width: 90px;" class="text-sm"> <td style="width: 90px;" class="text-sm">
<input class="border-b-2 border-black" type="date" v-model="TransactionDate" /> <input class="border-b-2 border-black" type="date" v-model="Transaction.Date" />
</td> </td>
<td style="max-width: 150px;"> <td style="max-width: 150px;">
<Autocomplete v-model="Payee" type="payees" /> <Autocomplete v-model:text="Transaction.Payee" v-model:id="Transaction.PayeeID" type="payees" />
</td> </td>
<td style="max-width: 200px;"> <td style="max-width: 200px;">
<Autocomplete v-model="Category" type="categories" /> <Autocomplete v-model:text="Transaction.Category" v-model:id="Transaction.CategoryID" type="categories" />
</td> </td>
<td> <td>
<input class="block w-full border-b-2 border-black" type="text" v-model="Memo" /> <input class="block w-full border-b-2 border-black" type="text" v-model="Transaction.Memo" />
</td> </td>
<td style="width: 80px;" class="text-right"> <td style="width: 80px;" class="text-right">
<input <input
class="text-right block w-full border-b-2 border-black" class="text-right block w-full border-b-2 border-black"
type="currency" type="currency"
v-model="Amount" v-model="Transaction.Amount"
/> />
</td> </td>
<td style="width: 20px;"> <td style="width: 20px;">

View File

@ -37,7 +37,7 @@ const CurrentBudgetID = computed(()=> useBudgetsStore().CurrentBudgetID);
</td> </td>
<td class="text-right">{{ transaction.GroupID ? "☀" : "" }}<a @click="edit = true;"></a></td> <td class="text-right">{{ transaction.GroupID ? "☀" : "" }}<a @click="edit = true;"></a></td>
</tr> </tr>
<TransactionEditRow v-if="edit" :transaction="transaction" /> <TransactionEditRow v-if="edit" :transactionid="transaction.ID" />
</template> </template>
<style> <style>

View File

@ -18,10 +18,12 @@ export interface Transaction {
TransferAccount: string, TransferAccount: string,
CategoryGroup: string, CategoryGroup: string,
Category: string, Category: string,
CategoryID: string,
Memo: string, Memo: string,
Status: string, Status: string,
GroupID: string, GroupID: string,
Payee: string, Payee: string,
PayeeID: string,
Amount: number, Amount: number,
} }