Skip to content

Commit 6e8307f

Browse files
committed
Migrate over to async/await on Users.svelte
1 parent 19c95a5 commit 6e8307f

File tree

1 file changed

+27
-45
lines changed

1 file changed

+27
-45
lines changed

src/Admin/Users.svelte

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,35 @@
1313
1414
let principalId = undefined;
1515
16-
loadThings();
17-
18-
function loadThings() {
16+
async function loadThings() {
1917
loaded = false;
20-
21-
fetch(`${baseurl}/users/get`, {headers: {"Authorization": "Bearer " + Cookies.get("key")}})
22-
.then((response) => response.json())
23-
.then((json) => {
24-
items = json["data"];
25-
loaded = true;
26-
for (let i in items) {
27-
let item = items[i]
28-
if (item.Role === "principal") {
29-
principalId = item.ID;
30-
break
31-
}
32-
}
33-
},
34-
);
18+
let response = await fetch(`${baseurl}/users/get`, {headers: {"Authorization": "Bearer " + Cookies.get("key")}})
19+
let json = await response.json();
20+
items = json["data"];
21+
loaded = true;
22+
for (let i in items) {
23+
let item = items[i]
24+
if (item.Role === "principal") {
25+
principalId = item.ID;
26+
break
27+
}
28+
}
3529
}
3630
37-
let choices = ["unverified", "student", "parent", "teacher", "food organizer", "school psychologist", "principal assistant", "principal"];
38-
31+
const choices = ["unverified", "student", "parent", "teacher", "food organizer", "school psychologist", "principal assistant", "principal"];
3932
4033
import { navigate } from "svelte-navigator";
4134
import Cookies from "js-cookie";
4235
import FormField from "@smui/form-field";
4336
import Switch from "@smui/switch";
37+
import {onMount} from "svelte";
4438
4539
const token = Cookies.get("key");
4640
if (token === null || token === undefined) {
4741
navigate("/login");
4842
}
43+
44+
onMount(loadThings);
4945
</script>
5046

5147
<DataTable table$aria-label="User list" style="width: 100%;">
@@ -69,25 +65,18 @@
6965
<Cell>{item["Email"]}</Cell>
7066
<Cell>
7167
{#if localStorage.getItem("userId") !== item["ID"]}
72-
<SegmentedButton segments={choices} let:segment singleSelect on:change={(e) => {
68+
<SegmentedButton segments={choices} let:segment singleSelect on:change={async (e) => {
7369
e.stopPropagation();
74-
console.log(e);
75-
7670
let fd = new FormData();
7771
fd.append("role", e.detail.segmentId);
78-
7972
principalId = undefined;
8073

81-
fetch(`${baseurl}/user/role/update/${item["ID"]}`, {
74+
await fetch(`${baseurl}/user/role/update/${item["ID"]}`, {
8275
headers: {"Authorization": "Bearer " + Cookies.get("key")},
8376
body: fd,
8477
method: "PATCH"
8578
})
86-
.then((response) => response.json())
87-
.then((json) => {
88-
loadThings()
89-
},
90-
);
79+
await loadThings()
9180
}} bind:selected="{item['Role']}">
9281
{#if
9382
(localStorage.getItem("role") === "principal assistant" && !(segment === "principal" || segment === "principal assistant")) ||
@@ -109,37 +98,30 @@
10998
<Switch bind:checked={item["IsLocked"]} on:click={async (e) => {
11099
e.stopPropagation();
111100
await fetch(`${baseurl}/user/lock_unlock/${item["ID"]}`, {headers: {"Authorization": "Bearer " + Cookies.get("key")}, method: "PATCH"});
112-
loadThings();
101+
await loadThings();
113102
}} />
114103
</FormField>
115104
{/if}
116105
</Cell>
117106
<Cell>
118107
{#if localStorage.getItem("email") !== item["Email"]}
119-
<IconButton class="material-icons" on:click={(e) => {
108+
<IconButton class="material-icons" on:click={async (e) => {
120109
e.stopPropagation();
121-
fetch(`${baseurl}/user/get/password_reset/${item["ID"]}`, {headers: {"Authorization": "Bearer " + Cookies.get("key")}})
122-
.then((response) => response.blob())
123-
.then((blob) => saveBlob(blob))
124-
.catch((err) => {
125-
console.log(err);
126-
});
110+
let response = await fetch(`${baseurl}/user/get/password_reset/${item["ID"]}`, {headers: {"Authorization": "Bearer " + Cookies.get("key")}})
111+
let blob = await response.blob()
112+
await saveBlob(blob)
127113
}}>download</IconButton>
128114
{/if}
129115
</Cell>
130116
<Cell>
131117
{#if localStorage.getItem("email") !== item["Email"]}
132-
<IconButton class="material-icons" on:click={(e) => {
118+
<IconButton class="material-icons" on:click={async (e) => {
133119
e.stopPropagation();
134-
fetch(`${baseurl}/user/delete/${item["ID"]}`, {
120+
await fetch(`${baseurl}/user/delete/${item["ID"]}`, {
135121
headers: {"Authorization": "Bearer " + Cookies.get("key")},
136122
method: "DELETE"
137123
})
138-
.then((response) => response.json())
139-
.then((json) => {
140-
loadThings()
141-
},
142-
);
124+
await loadThings()
143125
}}>delete</IconButton>
144126
{/if}
145127
</Cell>

0 commit comments

Comments
 (0)