From 92e82782b5e6dd09f778ede051436244f7b6c948 Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Thu, 4 Apr 2024 23:03:18 +0200 Subject: [PATCH] Update OperationCrud display with tags and sorting --- src/lib/crud/cruds/OperationCrud.ts | 35 +++++++++++++++++------------ src/lib/db/bank_accounts.ts | 21 ++++++++++------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/lib/crud/cruds/OperationCrud.ts b/src/lib/crud/cruds/OperationCrud.ts index 71e547e..7c681f2 100644 --- a/src/lib/crud/cruds/OperationCrud.ts +++ b/src/lib/crud/cruds/OperationCrud.ts @@ -1,23 +1,26 @@ import { + ArrayField, CallbackStateProcessor, CallbackStateProvider, CheckboxField, CrudDefinition, DateField, + KeyValueObjectField, List, NumberField, PaginatedResults, TextField, UrlAction, View, - type RequestParameters, type CrudOperation, type ListOperationOptions, - KeyValueObjectField + type RequestParameters, } from '@orbitale/svelte-admin'; -import { getOperationById, getOperations, getOperationsCount } from '$lib/db/operations'; +import {getOperationById, getOperations, getOperationsCount} from '$lib/db/operations'; import type Operation from '$lib/entities/Operation'; +import SortableField from "$lib/admin/src/SortableField"; +import type {OrderBy} from "$lib/admin/src/OrderBy"; export default new CrudDefinition({ name: 'operations', @@ -28,11 +31,11 @@ export default new CrudDefinition({ operations: [ new List( [ - new DateField('operation_date', 'Date'), + new DateField('operation_date', 'Date', {sortable: true}), new TextField('op_type', 'Type 1'), new TextField('type_display', 'Type 2'), new TextField('details', 'Details'), - new TextField('tags', 'Tags'), + new ArrayField('tags', 'Tags', new KeyValueObjectField('', '', 'name')), new NumberField('amount_display', 'Montant') ], [new UrlAction('View', '/crud/operations/view')], @@ -54,7 +57,7 @@ export default new CrudDefinition({ new NumberField('hash', 'Hash'), new TextField('state', 'State'), new KeyValueObjectField('bank_account', 'Bank account', 'name'), - new TextField('tags', 'Tags'), + new ArrayField('tags', 'Tags', new KeyValueObjectField('', '', 'name')), new CheckboxField('ignored_from_charts', 'Is ignored from charts') ]) ], @@ -68,15 +71,19 @@ export default new CrudDefinition({ if (operation.name === 'list') { const options: ListOperationOptions = operation.options; - const results = await getOperations(Number(requestParameters.page) || 1); + const page = Number(requestParameters.page || 1); + let sort: SortableField|null = null; + if (requestParameters.sort) { + const firstKey = Object.keys(requestParameters.sort)[0]; + sort = new SortableField(firstKey, requestParameters.sort[firstKey] as OrderBy, firstKey); + } + const results = await getOperations(page, sort); const numberOfItems = await getOperationsCount(null); - return Promise.resolve( - new PaginatedResults( - Number(requestParameters.page), - numberOfItems / Number(options.pagination?.itemsPerPage || 10), - numberOfItems, - results - ) + return new PaginatedResults( + page, + numberOfItems / Number(options.pagination?.itemsPerPage || 10), + numberOfItems, + results ); } diff --git a/src/lib/db/bank_accounts.ts b/src/lib/db/bank_accounts.ts index a1486c7..b5a32d5 100644 --- a/src/lib/db/bank_accounts.ts +++ b/src/lib/db/bank_accounts.ts @@ -1,20 +1,15 @@ import BankAccount from '$lib/entities/BankAccount'; import api_call from '$lib/utils/api_call'; -import { writable } from 'svelte/store'; -import type { Writable } from 'svelte/store'; -import DeserializedOperation from '$lib/db/operations'; -import BankAccountsCrud from '$lib/crud/cruds/BankAccountsCrud'; -export const bankAccountsStore: Writable = writable(); +const cache: Record = {}; export async function getBankAccounts(): Promise> { const res: string = await api_call('bank_account_find_all'); - const bank_accounts = JSON.parse(res).map((data: BankAccount) => { + const bank_accounts: Array = JSON.parse(res).map((data: BankAccount) => { return new BankAccount(data.id, data.name, data.slug, data.currency); }); - - bankAccountsStore.set(bank_accounts); + bank_accounts.forEach(a => cache[a.id] = a); return bank_accounts; } @@ -31,6 +26,10 @@ export async function getBankAccountsAsChoices(): Promise { + if (cache[id]) { + return Promise.resolve(cache[id]); + } + const res: string = await api_call('bank_account_get_by_id', { id: id.toString() }); if (!res) { @@ -43,6 +42,8 @@ export async function getBankAccountById(id: number): Promise