-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathEnclosures.svelte
115 lines (102 loc) · 3.7 KB
/
Enclosures.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
110
111
112
113
114
115
<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 { fetchEnclosures, deleteEnclosure, deleteArea } from '../providers/api';
import { successNotification, errorNotification } from '../providers/notification-provider';
import EnclosureCard from '../user-controls/EnclosureCard.svelte';
let enclosures = [];
const { confirmModal } = getContext('confirm');
const loadData = () => {
fetchEnclosures(false, (data) => (enclosures = data));
};
const deleteEnclosureAction = (enclosure) => {
confirmModal(
$_('enclosures.delete.confirm.message', {
default: "Are you sure you want to delete the enclosure ''{name}''?",
values: { name: enclosure.name },
}),
async () => {
try {
await deleteEnclosure(enclosure.id);
successNotification(
$_('enclosures.delete.ok.message', {
default: "The enclosure ''{name}'' is deleted.",
values: { name: enclosure.name },
}),
$_('notification.delete.ok.title', { default: 'OK' }),
);
loadData();
} catch (e) {
errorNotification(
$_('enclosures.delete.error.message', {
default: "The enclosure ''{name}'' could not be deleted!\nError: {error}",
values: { name: enclosure.name, error: e.message },
}),
$_('notification.delete.error.title', { default: 'ERROR' }),
);
}
},
);
};
const deleteAreaAction = (area) => {
confirmModal(
$_('areas.delete.confirm.message', {
default: "Are you sure you want to delete the area ''{name}''?",
values: { name: area.name },
}),
async () => {
try {
await deleteArea(area.id);
successNotification(
$_('areas.delete.ok.message', { default: "The area ''{name}'' is deleted.", values: { name: area.name } }),
$_('notification.delete.ok.title', { default: 'OK' }),
);
loadData();
} catch (e) {
errorNotification(
$_('areas.delete.error.message', {
default: "The area ''{name}'' could not be deleted!\nError: {error}",
values: { name: area.name, error: e.message },
}),
$_('notification.delete.error.title', { default: 'ERROR' }),
);
}
},
);
};
setContext('enclosureActions', {
deleteAction: (enclosure) => deleteEnclosureAction(enclosure),
deleteArea: (area) => deleteAreaAction(area),
// reloadAction: () => loadData() // TODO: Figure this out. Reload can only be done when on the enclosure page... :(
});
onMount(() => {
setCustomPageTitle($_('enclosures.title', { default: 'Enclosures' }));
loadData();
// Reload every 30 seconds the enclosure data
const interval = setInterval(() => {
loadData();
}, 30 * 1000);
//If a function is returned from onMount, it will be called when the component is unmounted.
return () => clearInterval(interval);
});
onDestroy(() => {
customPageTitleUsed.set(false);
});
</script>
<PageHeader>
{$_('enclosures.title', { default: 'Enclosures' })}
</PageHeader>
<div class="container-fluid">
<div class="row">
{#if enclosures.length > 0}
<!-- Sort based on enclosure names natural sorting -->
{#each enclosures.sort((a, b) => a.name.localeCompare(b.name)) as enclosure}
<div class="col-12">
<EnclosureCard {enclosure} />
</div>
{/each}
{/if}
</div>
</div>