Skip to content

Commit 6433602

Browse files
committed
Add "new" action to BankAccount
1 parent d068283 commit 6433602

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

src/lib/crud/cruds/BankAccountsCrud.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CrudDefinition,
55
Edit,
66
List,
7+
New,
78
TextField,
89
UrlAction,
910
type RequestParameters,
@@ -17,7 +18,7 @@ import {
1718
getBankAccounts,
1819
updateBankAccount
1920
} from '$lib/db/bank_accounts';
20-
import type BankAccount from '$lib/entities/BankAccount';
21+
import BankAccount from '$lib/entities/BankAccount';
2122
import { goto } from '$app/navigation';
2223
import { success } from '$lib/utils/message';
2324

@@ -34,8 +35,11 @@ export default new CrudDefinition<BankAccount>({
3435
// minStateLoadingTimeMs: 0,
3536

3637
operations: [
37-
new List([...baseFields], [new UrlAction('Edit', '/crud/bank-accounts/edit')]),
38-
new Edit(baseFields)
38+
new List([...baseFields], [new UrlAction('Edit', '/crud/bank-accounts/edit')], {
39+
globalActions: [new UrlAction('New', '/crud/bank-accounts/new')]
40+
}),
41+
new Edit(baseFields),
42+
new New(baseFields),
3943
],
4044

4145
stateProvider: new CallbackStateProvider<BankAccount>(
@@ -67,11 +71,10 @@ export default new CrudDefinition<BankAccount>({
6771
if (!data) {
6872
throw new Error('Cannot create new object: empty data.');
6973
}
70-
if (Array.isArray(data)) {
71-
throw new Error('Cannot update data as array for this action.');
72-
}
7374

74-
return createBankAccount(data);
75+
await createBankAccount(BankAccount.fromObject(data));
76+
success('Success!');
77+
await goto('/crud/bank-accounts/list');
7578
}
7679

7780
if (operation.name === 'edit') {

src/lib/entities/BankAccount.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export default class BankAccount implements Entity {
2222
});
2323
}
2424

25+
static fromObject(object: {[key: string]: any}): BankAccount {
26+
if ((object.id && isNaN(object.id)) || !object.name || !object.slug || !object.currency) {
27+
throw new Error('Invalid object: cannot create a BankAccount.');
28+
}
29+
30+
return new BankAccount(Number(object.id)||0, object.name, object.slug, object.currency);
31+
}
32+
2533
public setId(id: number) {
2634
if (!id) {
2735
throw new Error('Cannot set an empty ID on an object.');

0 commit comments

Comments
 (0)