implemented transaction propagation via event bus
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { api, get } from '../../urls.svelte';
|
||||
import { api, eventStream, get } from '../../urls.svelte';
|
||||
import { error, yikes } from '../../warn.svelte';
|
||||
import { t } from '../../translations.svelte';
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
let { id } = $props();
|
||||
let account = $state(null);
|
||||
let eventSource = null;
|
||||
let filter = $state([]);
|
||||
let transactions = $state([]);
|
||||
let filtered = $derived(transactions.filter(t => checker(t.tags,filter)));
|
||||
@@ -27,7 +28,6 @@
|
||||
if (!transaction.destination.id) sums[0] += transaction.amount;
|
||||
if (!transaction.source.id) sums[0] -= transaction.amount;
|
||||
}
|
||||
window.setTimeout(scrollToBottom,100);
|
||||
return sums;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,34 @@
|
||||
filter = filter.filter(x => x != tag.toLowerCase());
|
||||
}
|
||||
|
||||
function handleCreateEvent(evt){
|
||||
const event_data = JSON.parse(evt.data);
|
||||
const new_transaction = event_data.transaction;
|
||||
transactions.push(new_transaction);
|
||||
window.setTimeout(scrollToBottom,100);
|
||||
}
|
||||
|
||||
function handleEvent(evt,method){
|
||||
let event_data = JSON.parse(evt.data);
|
||||
console.log({method, event_data});
|
||||
}
|
||||
|
||||
function handleDeleteEvent(evt){
|
||||
handleEvent(evt,'delete');
|
||||
}
|
||||
|
||||
function handleUpdateEvent(evt){
|
||||
const event_data = JSON.parse(evt.data);
|
||||
const updated_transaction = event_data.transaction;
|
||||
for (var idx in transactions){
|
||||
if (transactions[idx].id == updated_transaction.id) {
|
||||
updated_transaction.tags = transactions[idx].tags;
|
||||
transactions[idx] = updated_transaction;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function load(){
|
||||
let url = api(`accounting/${id}`);
|
||||
let res = await get(url);
|
||||
@@ -59,11 +87,15 @@
|
||||
transactions = json.transactions;
|
||||
users = json.user_list;
|
||||
account = json.account;
|
||||
try {
|
||||
eventSource = eventStream(handleCreateEvent,handleUpdateEvent,handleDeleteEvent);
|
||||
} catch (ignored) {}
|
||||
window.setTimeout(scrollToBottom,100);
|
||||
} else error(res);
|
||||
}
|
||||
|
||||
function onSave(){
|
||||
load();
|
||||
// load();
|
||||
}
|
||||
|
||||
function scrollToBottom(){
|
||||
|
||||
Reference in New Issue
Block a user