-
Notifications
You must be signed in to change notification settings - Fork 4
/
EditUserForm.svelte
104 lines (97 loc) · 3.38 KB
/
EditUserForm.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
<form class="bg-gray-800 text-gray-200 p-2">
<div class="mb-2">
<label class="block bg-gray-800 text-gray-200" for="username">username</label>
<input disabled class="block border border-gray-500 bg-gray-500 text-gray-800 py-0 px-2 w-full" id="username" name="username" type="text" bind:value={username}>
</div>
<div class="mb-4">
<label class="block bg-gray-800 text-gray-200" for="pwd">current password</label>
<input class="block border border-gray-500 bg-gray-200 text-gray-800 py-0 px-2 w-full" id="pwd" name="pwd" type="password" bind:value={pwd}>
</div>
<div class="mb-4">
<label class="block bg-gray-800 text-gray-200" for="pwd2">new password</label>
<input class="block border border-gray-500 bg-gray-200 text-gray-800 py-0 px-2 w-full" id="newpwd" name="newpwd" type="password" bind:value={newpwd}>
</div>
<div class="mb-4">
<label class="block bg-gray-800 text-gray-200" for="pwd2">re-enter new password</label>
<input class="block border border-gray-500 bg-gray-200 text-gray-800 py-0 px-2 w-full" id="newpwd2" name="newpwd2" type="password" bind:value={newpwd2}>
</div>
{#if frm.status != ""}
<div class="mb-2">
<p class="font-bold bg-gray-800 text-gray-200">{frm.status}</p>
</div>
{/if}
<div class="flex flex-row justify-center mb-4">
<div>
{#if frm.mode == "loading"}
<button disabled on:click={onupdate} class="inline mx-auto py-1 px-2 bg-gray-200 text-gray-800 mr-2">Update</button>
{:else}
<button on:click={onupdate} class="inline mx-auto py-1 px-2 bg-gray-200 text-gray-800 mr-2">Update</button>
{/if}
<button on:click={oncancel} class="inline mx-auto py-1 px-2 bg-gray-200 text-gray-800">Cancel</button>
</div>
</div>
</form>
<script>
import {createEventDispatcher} from "svelte";
let dispatch = createEventDispatcher();
let svcurl = "/api";
export let username = "";
let pwd = "";
let newpwd = "";
let newpwd2 = "";
let frm = {};
frm.mode = "";
frm.status = "";
// Post edit username/pwd and async returns loginresult:
// {tok: "", error: ""}
async function edituser(username, pwd, newpwd) {
let sreq = `${svcurl}/edituser/`;
let reqbody = {
username: username,
pwd: pwd,
newpwd: newpwd,
};
try {
let res = await fetch(sreq, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(reqbody),
});
if (!res.ok) {
let err = await res.text();
return {tok: "", error: err};
}
let result = await res.json();
return result;
} catch(err) {
console.error(err);
return {tok: "", error: "server/network error"};
}
}
async function onupdate(e) {
e.preventDefault();
if (newpwd != newpwd2) {
frm.mode = "";
frm.status = "re-entered password doesn't match";
return;
}
frm.mode = "loading";
frm.status = "Updating account...";
let result = await edituser(username, pwd, newpwd);
frm.mode = "";
frm.status = "";
if (result.error != "") {
frm.status = result.error;
return;
}
dispatch("update", {username: username, tok: result.tok});
}
function oncancel(e) {
e.preventDefault();
username = "";
pwd = "";
newpwd = "";
newpwd2 = "";
dispatch("cancel");
}
</script>