-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathButtons.svelte
109 lines (98 loc) · 3.25 KB
/
Buttons.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<script>
import { onMount, onDestroy, getContext, setContext } from 'svelte';
import { PageHeader } from '@keenmate/svelte-adminlte';
import { _ } from 'svelte-i18n';
import { setCustomPageTitle, customPageTitleUsed } from '../stores/page-title';
import { fetchButtons, deleteButton, updateSystemSettings } from '../providers/api';
import { successNotification, errorNotification } from '../providers/notification-provider';
import ButtonCard from '../user-controls/ButtonCard.svelte';
let buttons = [];
const { confirmModal } = getContext('confirm');
const loadData = () => {
fetchButtons(false, (data) => (buttons = data));
};
const deleteButtonAction = (button) => {
confirmModal(
$_('buttons.delete.confirm.message', {
default: "Are you sure you want to delete the button ''{name}''?",
values: { name: button.name },
}),
async () => {
try {
await deleteButton(button.id);
successNotification(
$_('buttons.delete.ok.message', {
default: "The button ''{name}'' is deleted.",
values: { name: button.name },
}),
$_('notification.delete.ok.title', { default: 'OK' }),
);
loadData();
} catch (e) {
errorNotification(
$_('buttons.delete.error.message', {
default: "The button ''{name}'' could not be deleted!\nError: {error}",
values: { name: button.name, error: e.message },
}),
$_('notification.delete.error.title', { default: 'ERROR' }),
);
}
},
);
};
const ignoreButtonAction = (button) => {
confirmModal(
$_('buttons.ignore.confirm.message', {
default: "Are you sure you want to ignore the button ''{name}''?",
values: { name: button.name },
}),
async () => {
try {
await updateSystemSettings({ exclude_ids: button.id });
successNotification(
$_('buttons.ignore.ok.message', {
default: "The button ''{name}'' is ignored.",
values: { name: button.name },
}),
$_('notification.exclude.ok.title'),
);
loadData();
} catch (e) {
errorNotification(
$_('buttons.ignore.error.message', {
default: "The button ''{name}'' could not be ignored!\nError: {error}",
values: { name: button.name, error: e.message },
}),
$_('notification.exclude.error.title'),
);
}
},
);
};
setContext('buttonActions', {
deleteAction: (button) => deleteButtonAction(button),
ignoreAction: (button) => ignoreButtonAction(button),
});
onMount(() => {
setCustomPageTitle($_('buttons.title', { default: 'Buttons' }));
loadData();
});
onDestroy(() => {
customPageTitleUsed.set(false);
});
</script>
<PageHeader>
{$_('buttons.title')}
</PageHeader>
<div class="container-fluid">
<div class="row">
{#if buttons.length > 0}
<!-- Sort based on translated names -->
{#each buttons.sort((a, b) => a.name.localeCompare(b.name)) as button}
<div class="col-12">
<ButtonCard {button} />
</div>
{/each}
{/if}
</div>
</div>