Skip to content

Commit 33d3542

Browse files
committed
Update OperationCrud display with tags and sorting
1 parent ee07e06 commit 33d3542

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/lib/crud/cruds/OperationCrud.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import {
2+
ArrayField,
23
CallbackStateProcessor,
34
CallbackStateProvider,
45
CheckboxField,
56
CrudDefinition,
67
DateField,
8+
KeyValueObjectField,
79
List,
810
NumberField,
911
PaginatedResults,
1012
TextField,
1113
UrlAction,
1214
View,
13-
type RequestParameters,
1415
type CrudOperation,
1516
type ListOperationOptions,
16-
KeyValueObjectField
17+
type RequestParameters,
1718
} from '@orbitale/svelte-admin';
1819

19-
import { getOperationById, getOperations, getOperationsCount } from '$lib/db/operations';
20+
import {getOperationById, getOperations, getOperationsCount} from '$lib/db/operations';
2021
import type Operation from '$lib/entities/Operation';
22+
import SortableField from "$lib/admin/src/SortableField";
23+
import type {OrderBy} from "$lib/admin/src/OrderBy";
2124

2225
export default new CrudDefinition<Operation>({
2326
name: 'operations',
@@ -28,11 +31,11 @@ export default new CrudDefinition<Operation>({
2831
operations: [
2932
new List(
3033
[
31-
new DateField('operation_date', 'Date'),
34+
new DateField('operation_date', 'Date', {sortable: true}),
3235
new TextField('op_type', 'Type 1'),
3336
new TextField('type_display', 'Type 2'),
3437
new TextField('details', 'Details'),
35-
new TextField('tags', 'Tags'),
38+
new ArrayField('tags', 'Tags', new KeyValueObjectField('', '', 'name')),
3639
new NumberField('amount_display', 'Montant')
3740
],
3841
[new UrlAction('View', '/crud/operations/view')],
@@ -54,7 +57,7 @@ export default new CrudDefinition<Operation>({
5457
new NumberField('hash', 'Hash'),
5558
new TextField('state', 'State'),
5659
new KeyValueObjectField('bank_account', 'Bank account', 'name'),
57-
new TextField('tags', 'Tags'),
60+
new ArrayField('tags', 'Tags', new KeyValueObjectField('', '', 'name')),
5861
new CheckboxField('ignored_from_charts', 'Is ignored from charts')
5962
])
6063
],
@@ -68,15 +71,19 @@ export default new CrudDefinition<Operation>({
6871

6972
if (operation.name === 'list') {
7073
const options: ListOperationOptions = operation.options;
71-
const results = await getOperations(Number(requestParameters.page) || 1);
74+
const page = Number(requestParameters.page || 1);
75+
let sort: SortableField|null = null;
76+
if (requestParameters.sort) {
77+
const firstKey = Object.keys(requestParameters.sort)[0];
78+
sort = new SortableField(firstKey, requestParameters.sort[firstKey] as OrderBy, firstKey);
79+
}
80+
const results = await getOperations(page, sort);
7281
const numberOfItems = await getOperationsCount(null);
73-
return Promise.resolve(
74-
new PaginatedResults(
75-
Number(requestParameters.page),
76-
numberOfItems / Number(options.pagination?.itemsPerPage || 10),
77-
numberOfItems,
78-
results
79-
)
82+
return new PaginatedResults(
83+
page,
84+
numberOfItems / Number(options.pagination?.itemsPerPage || 10),
85+
numberOfItems,
86+
results
8087
);
8188
}
8289

src/lib/db/bank_accounts.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import BankAccount from '$lib/entities/BankAccount';
22
import api_call from '$lib/utils/api_call';
3-
import { writable } from 'svelte/store';
4-
import type { Writable } from 'svelte/store';
5-
import DeserializedOperation from '$lib/db/operations';
6-
import BankAccountsCrud from '$lib/crud/cruds/BankAccountsCrud';
73

8-
export const bankAccountsStore: Writable<BankAccount[]> = writable();
4+
const cache: Record<string, BankAccount> = {};
95

106
export async function getBankAccounts(): Promise<Array<BankAccount>> {
117
const res: string = await api_call('bank_account_find_all');
128

13-
const bank_accounts = JSON.parse(res).map((data: BankAccount) => {
9+
const bank_accounts: Array<BankAccount> = JSON.parse(res).map((data: BankAccount) => {
1410
return new BankAccount(data.id, data.name, data.slug, data.currency);
1511
});
16-
17-
bankAccountsStore.set(bank_accounts);
12+
bank_accounts.forEach(a => cache[a.id] = a);
1813

1914
return bank_accounts;
2015
}
@@ -31,6 +26,10 @@ export async function getBankAccountsAsChoices(): Promise<Array<{ name: string;
3126
}
3227

3328
export async function getBankAccountById(id: number): Promise<BankAccount | null> {
29+
if (cache[id]) {
30+
return Promise.resolve(cache[id]);
31+
}
32+
3433
const res: string = await api_call('bank_account_get_by_id', { id: id.toString() });
3534

3635
if (!res) {
@@ -43,6 +42,8 @@ export async function getBankAccountById(id: number): Promise<BankAccount | null
4342
throw new Error('Could not deserialize bank account.');
4443
}
4544

45+
cache[id] = bank_account;
46+
4647
return bank_account;
4748
}
4849

@@ -52,6 +53,8 @@ export async function createBankAccount(bank_account: BankAccount): Promise<void
5253
if (isNaN(+id)) {
5354
throw new Error('Internal error: API returned a non-number ID.');
5455
}
56+
57+
cache[id] = bank_account;
5558
}
5659

5760
export async function updateBankAccount(bank_account: BankAccount) {
@@ -64,4 +67,6 @@ export async function updateBankAccount(bank_account: BankAccount) {
6467
name: bank_account.name,
6568
currency: bank_account.currency
6669
});
70+
71+
cache[bank_account.id] = bank_account;
6772
}

0 commit comments

Comments
 (0)