Skip to content

Commit

Permalink
Update cruds after SvelteAdmin update, and start using KeyValueObject…
Browse files Browse the repository at this point in the history
… field for related entities
  • Loading branch information
Pierstoval committed Mar 14, 2024
1 parent 69e7c31 commit 1622d10
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
17 changes: 10 additions & 7 deletions src/lib/crud/cruds/BankAccountsCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
List,
TextField,
UrlAction,
type RequestParameters,
type CrudOperation,
type StateProcessorInput,
} from "@orbitale/svelte-admin";
import type {RequestParameters} from "@orbitale/svelte-admin/dist/request";
import type {CrudOperation} from "@orbitale/svelte-admin/dist/Crud/Operations";
import type {StateProcessorInput} from "@orbitale/svelte-admin/dist/State/Processor";

import {createBankAccount, getBankAccountById, getBankAccounts, updateBankAccount} from "$lib/db/bank_accounts";
import type BankAccount from "$lib/entities/BankAccount";
Expand All @@ -21,7 +21,8 @@ const baseFields = [
new TextField('currency', 'Currency'),
];

export default new CrudDefinition<BankAccount>('bank-accounts', {
export default new CrudDefinition<BankAccount>({
name: 'bank-accounts',
defaultOperationName: "list",
label: {plural: "BankAccounts", singular: "BankAccount"},
// minStateLoadingTimeMs: 0,
Expand All @@ -34,7 +35,7 @@ export default new CrudDefinition<BankAccount>('bank-accounts', {
new Edit(baseFields),
],

stateProvider: new CallbackStateProvider<BankAccount>(async (operation: CrudBankAccount, requestParameters: RequestParameters) => {
stateProvider: new CallbackStateProvider<BankAccount>(async (operation: CrudOperation, requestParameters: RequestParameters) => {
if (typeof window === 'undefined') {
// SSR, can't call Tauri API then.
return Promise.resolve([]);
Expand All @@ -45,7 +46,7 @@ export default new CrudDefinition<BankAccount>('bank-accounts', {
}

if (operation.name === 'view' || operation.name === 'edit') {
return getBankAccountById(requestParameters.id);
return getBankAccountById(Number(requestParameters.id));
}

return Promise.resolve(null);
Expand All @@ -57,10 +58,12 @@ export default new CrudDefinition<BankAccount>('bank-accounts', {
}

if (operation.name === 'edit') {
data.id = parseInt(requestParameters.id, 10);
data.id = Number(requestParameters.id);

await updateBankAccount(data);
success('Success!');
await goto('/crud/bank-accounts/list');

return;
}
})
Expand Down
26 changes: 16 additions & 10 deletions src/lib/crud/cruds/OperationCrud.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import {
CallbackStateProcessor,
CallbackStateProvider, CheckboxField,
CrudDefinition, DateField,
CallbackStateProvider,
CheckboxField,
CrudDefinition,
DateField,
List,
NumberField,
PaginatedResults,
TextField,
UrlAction,
View
View,
type RequestParameters,
type CrudOperation,
type ListOperationOptions,
KeyValueObjectField
} from "@orbitale/svelte-admin";
import type {CrudOperation} from "@orbitale/svelte-admin/dist/Crud/Operations";
import type {RequestParameters} from "@orbitale/svelte-admin/dist/request";

import {getOperationById, getOperations, getOperationsCount} from "$lib/db/operations";
import type Operation from "$lib/entities/Operation";

export default new CrudDefinition<Operation>('operations', {
export default new CrudDefinition<Operation>({
name: 'operations',
defaultOperationName: "list",
label: {plural: "Operations", singular: "Operation"},
// minStateLoadingTimeMs: 0,
Expand Down Expand Up @@ -49,7 +54,7 @@ export default new CrudDefinition<Operation>('operations', {
new NumberField('amount', 'Montant'),
new NumberField('hash', 'Hash'),
new TextField('state', 'State'),
new TextField('bank_account', 'Bank account'),
new KeyValueObjectField('bank_account', 'Bank account', 'name'),
new TextField('tags', 'Tags'),
new CheckboxField('ignored_from_charts', 'Is ignored from charts'),
]),
Expand All @@ -62,13 +67,14 @@ export default new CrudDefinition<Operation>('operations', {
}

if (operation.name === 'list') {
const results = await getOperations(requestParameters.page||1);
const options: ListOperationOptions = operation.options;
const results = await getOperations(Number(requestParameters.page)||1);
const numberOfItems = await getOperationsCount(null);
return Promise.resolve(new PaginatedResults(requestParameters.page, numberOfItems / operation.options.pagination.itemsPerPage, numberOfItems, results));
return Promise.resolve(new PaginatedResults(Number(requestParameters.page), numberOfItems / Number(options.pagination?.itemsPerPage||10), numberOfItems, results));
}

if (operation.name === 'view' || operation.name === 'edit') {
return getOperationById(requestParameters.id);
return getOperationById(Number(requestParameters.id));
}

return Promise.resolve(null);
Expand Down
7 changes: 4 additions & 3 deletions src/lib/crud/cruds/TagRulesCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const baseFields = [
new ToggleField('is_regex', 'Regex'),
];

export default new CrudDefinition<TagRule>('tag-rules', {
export default new CrudDefinition<TagRule>({
name: 'tag-rules',
defaultOperationName: "list",
label: {plural: "TagRules", singular: "TagRule"},
// minStateLoadingTimeMs: 0,
Expand All @@ -39,7 +40,7 @@ export default new CrudDefinition<TagRule>('tag-rules', {
new Edit(baseFields),
],

stateProvider: new CallbackStateProvider<TagRule>(async (operation: CrudTagRule, requestParameters: RequestParameters) => {
stateProvider: new CallbackStateProvider<TagRule>(async (operation: CrudOperation, requestParameters: RequestParameters) => {
if (typeof window === 'undefined') {
// SSR, can't call Tauri API then.
return Promise.resolve([]);
Expand All @@ -61,7 +62,7 @@ export default new CrudDefinition<TagRule>('tag-rules', {
data.id = parseInt(requestParameters.id || 0, 10);
// TODO FIXME : remove this and use proper entity injection!
data.is_regex = !!data.is_regex;
data.tags = data.tags.replace(/[^0-9,]/, '').split(',').map(i => parseInt(i, 10));
data.tags = data.tags.replace(/[^0-9,]/, '').split(',').map((i: string) => parseInt(i, 10));
const tag_rule = TagRule.fromJson(data);

if (operation.name === 'new') {
Expand Down
7 changes: 4 additions & 3 deletions src/lib/crud/cruds/TagsCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CallbackStateProvider,
CrudDefinition, Edit,
List, New,
TextField, ToggleField,
TextField,
UrlAction,
} from "@orbitale/svelte-admin";
import type {RequestParameters} from "@orbitale/svelte-admin/dist/request";
Expand All @@ -19,7 +19,8 @@ const baseFields = [
new TextField('name', 'Name'),
];

export default new CrudDefinition<Tag>('tags', {
export default new CrudDefinition<Tag>({
name: 'tags',
defaultOperationName: "list",
label: {plural: "Tags", singular: "Tag"},
// minStateLoadingTimeMs: 0,
Expand All @@ -37,7 +38,7 @@ export default new CrudDefinition<Tag>('tags', {
new Edit(baseFields),
],

stateProvider: new CallbackStateProvider<Tag>(async (operation: CrudTag, requestParameters: RequestParameters) => {
stateProvider: new CallbackStateProvider<Tag>(async (operation: CrudOperation, requestParameters: RequestParameters) => {
if (typeof window === 'undefined') {
// SSR, can't call Tauri API then.
return Promise.resolve([]);
Expand Down
8 changes: 6 additions & 2 deletions src/lib/crud/cruds/TriageCrud.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
CallbackStateProcessor,
CallbackStateProvider,
CrudDefinition, DateField, Delete, Edit,
CrudDefinition,
DateField,
Delete,
Edit,
List,
NumberField,
PaginatedResults,
Expand All @@ -23,7 +26,8 @@ import type Operation from "$lib/entities/Operation";
import {success} from "$lib/utils/message";
import {goto} from "$app/navigation";

export default new CrudDefinition<Operation>('triage', {
export default new CrudDefinition<Operation>({
name: 'triage',
defaultOperationName: "list",
label: {plural: "Triaged operations", singular: "Triaged operation"},
// minStateLoadingTimeMs: 0,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/entities/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum OperationState {
pending_triage = 'pending_triage'
}

export var operations_filters = [
export const operations_filters = [
new ConfigFilter('details', 'Details', FilterType.text),
new ConfigFilter('operation_date', 'Date', FilterType.date),
new ConfigFilter('amount_in_cents', 'Amount', FilterType.number),
Expand Down Expand Up @@ -69,7 +69,7 @@ export default class Operation implements Entity {
}

get date() {
let date = new Date(this.operation_date.toString());
const date = new Date(this.operation_date.toString());

return date.toLocaleDateString();
}
Expand Down Expand Up @@ -103,7 +103,7 @@ export default class Operation implements Entity {
}

const parsedDate = Date.parse(
matches.groups.year + '-' + matches.groups.month + '-' + matches.groups.day + 'T00:00:00.000Z'
matches.groups?.year + '-' + matches.groups?.month + '-' + matches.groups?.day + 'T00:00:00.000Z'
);
if (isNaN(parsedDate)) {
throw new Error(
Expand Down

0 comments on commit 1622d10

Please sign in to comment.