|
13 | 13 |
|
14 | 14 | let principalId = undefined; |
15 | 15 |
|
16 | | - loadThings(); |
17 | | -
|
18 | | - function loadThings() { |
| 16 | + async function loadThings() { |
19 | 17 | 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 | + } |
35 | 29 | } |
36 | 30 |
|
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"]; |
39 | 32 |
|
40 | 33 | import { navigate } from "svelte-navigator"; |
41 | 34 | import Cookies from "js-cookie"; |
42 | 35 | import FormField from "@smui/form-field"; |
43 | 36 | import Switch from "@smui/switch"; |
| 37 | + import {onMount} from "svelte"; |
44 | 38 |
|
45 | 39 | const token = Cookies.get("key"); |
46 | 40 | if (token === null || token === undefined) { |
47 | 41 | navigate("/login"); |
48 | 42 | } |
| 43 | +
|
| 44 | + onMount(loadThings); |
49 | 45 | </script> |
50 | 46 |
|
51 | 47 | <DataTable table$aria-label="User list" style="width: 100%;"> |
|
69 | 65 | <Cell>{item["Email"]}</Cell> |
70 | 66 | <Cell> |
71 | 67 | {#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) => { |
73 | 69 | e.stopPropagation(); |
74 | | - console.log(e); |
75 | | - |
76 | 70 | let fd = new FormData(); |
77 | 71 | fd.append("role", e.detail.segmentId); |
78 | | - |
79 | 72 | principalId = undefined; |
80 | 73 |
|
81 | | - fetch(`${baseurl}/user/role/update/${item["ID"]}`, { |
| 74 | + await fetch(`${baseurl}/user/role/update/${item["ID"]}`, { |
82 | 75 | headers: {"Authorization": "Bearer " + Cookies.get("key")}, |
83 | 76 | body: fd, |
84 | 77 | method: "PATCH" |
85 | 78 | }) |
86 | | - .then((response) => response.json()) |
87 | | - .then((json) => { |
88 | | - loadThings() |
89 | | - }, |
90 | | - ); |
| 79 | + await loadThings() |
91 | 80 | }} bind:selected="{item['Role']}"> |
92 | 81 | {#if |
93 | 82 | (localStorage.getItem("role") === "principal assistant" && !(segment === "principal" || segment === "principal assistant")) || |
|
109 | 98 | <Switch bind:checked={item["IsLocked"]} on:click={async (e) => { |
110 | 99 | e.stopPropagation(); |
111 | 100 | await fetch(`${baseurl}/user/lock_unlock/${item["ID"]}`, {headers: {"Authorization": "Bearer " + Cookies.get("key")}, method: "PATCH"}); |
112 | | - loadThings(); |
| 101 | + await loadThings(); |
113 | 102 | }} /> |
114 | 103 | </FormField> |
115 | 104 | {/if} |
116 | 105 | </Cell> |
117 | 106 | <Cell> |
118 | 107 | {#if localStorage.getItem("email") !== item["Email"]} |
119 | | - <IconButton class="material-icons" on:click={(e) => { |
| 108 | + <IconButton class="material-icons" on:click={async (e) => { |
120 | 109 | 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) |
127 | 113 | }}>download</IconButton> |
128 | 114 | {/if} |
129 | 115 | </Cell> |
130 | 116 | <Cell> |
131 | 117 | {#if localStorage.getItem("email") !== item["Email"]} |
132 | | - <IconButton class="material-icons" on:click={(e) => { |
| 118 | + <IconButton class="material-icons" on:click={async (e) => { |
133 | 119 | e.stopPropagation(); |
134 | | - fetch(`${baseurl}/user/delete/${item["ID"]}`, { |
| 120 | + await fetch(`${baseurl}/user/delete/${item["ID"]}`, { |
135 | 121 | headers: {"Authorization": "Bearer " + Cookies.get("key")}, |
136 | 122 | method: "DELETE" |
137 | 123 | }) |
138 | | - .then((response) => response.json()) |
139 | | - .then((json) => { |
140 | | - loadThings() |
141 | | - }, |
142 | | - ); |
| 124 | + await loadThings() |
143 | 125 | }}>delete</IconButton> |
144 | 126 | {/if} |
145 | 127 | </Cell> |
|
0 commit comments