Skip to content

Commit e330627

Browse files
committed
Improve actions
1 parent 036ce33 commit e330627

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

Enterprise/static/application.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ const logger = new Logger('output');
2323

2424
const action = (id, handler) => {
2525
const element = document.getElementById(id);
26-
if (element) element.onclick = handler;
26+
if (!element) return;
27+
element.onclick = () => {
28+
handler().catch((error) => {
29+
logger.log(error.message);
30+
});
31+
};
2732
};
2833

2934
const db = new Database('EnterpriseApplication', 1, (db) => {
@@ -38,7 +43,6 @@ const userService = new UserService(userRepository);
3843
action('add', async () => {
3944
const name = prompt('Enter user name:');
4045
const age = parseInt(prompt('Enter age:'), 10);
41-
if (!name || !Number.isInteger(age)) return;
4246
const user = await userService.createUser(name, age);
4347
logger.log('Added:', user);
4448
});
@@ -49,12 +53,8 @@ action('get', async () => {
4953
});
5054

5155
action('update', async () => {
52-
try {
53-
const user = await userService.incrementAge(1);
54-
logger.log('Updated:', user);
55-
} catch (err) {
56-
logger.log(err.message);
57-
}
56+
const user = await userService.incrementAge(1);
57+
logger.log('Updated:', user);
5858
});
5959

6060
action('delete', async () => {

Pragmatic/static/application.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ const actions = {
6767
},
6868
};
6969

70-
const init = () => {
71-
for (const [id, handler] of Object.entries(actions)) {
72-
const element = document.getElementById(id);
73-
if (element) element.onclick = handler;
74-
}
70+
const action = (id, handler) => {
71+
const element = document.getElementById(id);
72+
if (!element) return;
73+
element.onclick = () => {
74+
handler().catch((error) => {
75+
logger.log(error.message);
76+
});
77+
};
7578
};
7679

77-
init();
80+
for (const [id, handler] of Object.entries(actions)) {
81+
action(id, handler);
82+
}

0 commit comments

Comments
 (0)