@nymphjs/tilmeld-components 1.0.0-beta.15 → 1.0.0-beta.151

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,311 @@
1
+ <svelte:options runes />
2
+
3
+ {#if $user != null}
4
+ <Dialog
5
+ {use}
6
+ bind:open
7
+ aria-labelledby="tilmeld-password-title"
8
+ aria-describedby="tilmeld-password-content"
9
+ surface$class="tilmeld-password-dialog-surface"
10
+ {...exclude(restProps, [
11
+ 'password$',
12
+ 'newPassword$',
13
+ 'newPassword2$',
14
+ 'revokeTokens$',
15
+ 'closeButton$',
16
+ 'saveButton$',
17
+ 'progress$',
18
+ ])}
19
+ >
20
+ <!-- Title cannot contain leading whitespace due to mdc-typography-baseline-top() -->
21
+ <Title id="tilmeld-password-title">{title}</Title>
22
+ <Content id="tilmeld-password-content">
23
+ <form onsubmit={preventDefault(stopPropagation(changePassword))}>
24
+ <div>
25
+ <Textfield
26
+ bind:value={currentPassword}
27
+ label="Current Password"
28
+ type="password"
29
+ style="width: 100%;"
30
+ input$autocomplete="current-password"
31
+ {...prefixFilter(restProps, 'password$')}
32
+ />
33
+ </div>
34
+
35
+ <div>
36
+ <Textfield
37
+ bind:value={newPassword}
38
+ label="New Password"
39
+ type="password"
40
+ style="width: 100%;"
41
+ input$autocomplete="new-password"
42
+ {...prefixFilter(restProps, 'newPassword$')}
43
+ />
44
+ </div>
45
+
46
+ <div>
47
+ <Textfield
48
+ bind:value={newPassword2}
49
+ label="Re-enter New Password"
50
+ type="password"
51
+ style="width: 100%;"
52
+ input$autocomplete="new-password"
53
+ {...prefixFilter(restProps, 'newPassword2$')}
54
+ />
55
+ </div>
56
+
57
+ <div style="margin: 1em 0;">
58
+ <FormField>
59
+ <Checkbox
60
+ bind:checked={revokeCurrentTokens}
61
+ {...prefixFilter(restProps, 'revokeTokens$')}
62
+ />
63
+ {#snippet label()}
64
+ Log out of all other sessions.
65
+ <span
66
+ style="display: block; font-size: 0.7em; height: 0; overflow: visible;"
67
+ >
68
+ Use this if you think you've been hacked.
69
+ </span>
70
+ {/snippet}
71
+ </FormField>
72
+ </div>
73
+
74
+ {@render additional?.()}
75
+
76
+ {#if failureMessage}
77
+ <div class="tilmeld-password-failure">
78
+ {failureMessage}
79
+ </div>
80
+ {/if}
81
+
82
+ {#if loading}
83
+ <div class="tilmeld-password-loading">
84
+ <CircularProgress
85
+ style="height: 24px; width: 24px;"
86
+ indeterminate
87
+ {...prefixFilter(restProps, 'progress$')}
88
+ />
89
+ </div>
90
+ {/if}
91
+ </form>
92
+ </Content>
93
+ <Actions>
94
+ <Button disabled={loading} {...prefixFilter(restProps, 'closeButton$')}>
95
+ <Label>Close</Label>
96
+ </Button>
97
+ <Button
98
+ onclick={preventDefault(stopPropagation(changePassword))}
99
+ disabled={loading}
100
+ {...prefixFilter(restProps, 'saveButton$')}
101
+ >
102
+ <Label>Change Password</Label>
103
+ </Button>
104
+ </Actions>
105
+ </Dialog>
106
+ {/if}
107
+
108
+ <script lang="ts">
109
+ import type { ComponentProps, Snippet } from 'svelte';
110
+ import { onMount, onDestroy } from 'svelte';
111
+ import type { Writable } from 'svelte/store';
112
+ import { writable } from 'svelte/store';
113
+ import CircularProgress from '@smui/circular-progress';
114
+ import Dialog, { Title, Content, Actions } from '@smui/dialog';
115
+ import Textfield from '@smui/textfield';
116
+ import Checkbox from '@smui/checkbox';
117
+ import FormField from '@smui/form-field';
118
+ import Button, { Label } from '@smui/button';
119
+ import type { ActionArray } from '@smui/common/internal';
120
+ import { exclude, prefixFilter } from '@smui/common/internal';
121
+ import { preventDefault, stopPropagation } from '@smui/common/events';
122
+ import type { CurrentUserData } from '@nymphjs/tilmeld-client';
123
+ import type { User as UserClass } from '@nymphjs/tilmeld-client';
124
+
125
+ type OwnProps = {
126
+ /**
127
+ * An array of Action or [Action, ActionProps] to be applied to the element.
128
+ */
129
+ use?: ActionArray;
130
+ /**
131
+ * Whether the dialog is open.
132
+ */
133
+ open?: boolean;
134
+ /**
135
+ * The title of the dialog.
136
+ */
137
+ title?: string;
138
+ /**
139
+ * The User class from Nymph.
140
+ */
141
+ User: typeof UserClass;
142
+ /**
143
+ * A writable store of the current user.
144
+ *
145
+ * It will be retrieved from the server if not provided.
146
+ */
147
+ user?: Writable<(UserClass & CurrentUserData) | null | undefined>;
148
+ /**
149
+ * User provided. You can bind to it if you need to.
150
+ */
151
+ currentPassword?: string;
152
+ /**
153
+ * User provided. You can bind to it if you need to.
154
+ */
155
+ newPassword?: string;
156
+ /**
157
+ * User provided. You can bind to it if you need to.
158
+ */
159
+ newPassword2?: string;
160
+ /**
161
+ * User provided. You can bind to it if you need to.
162
+ */
163
+ revokeCurrentTokens?: boolean;
164
+
165
+ /**
166
+ * A spot for additional content.
167
+ */
168
+ additional?: Snippet;
169
+ };
170
+ let {
171
+ use = [],
172
+ open = $bindable(false),
173
+ title = 'Change Your Password',
174
+ User,
175
+ user = $bindable(writable(false as unknown as undefined)),
176
+ currentPassword = $bindable(''),
177
+ newPassword = $bindable(''),
178
+ newPassword2 = $bindable(''),
179
+ revokeCurrentTokens = $bindable(false),
180
+ additional,
181
+ ...restProps
182
+ }: OwnProps & {
183
+ [
184
+ k in keyof ComponentProps<typeof Textfield> as `password\$${k}`
185
+ ]?: ComponentProps<typeof Textfield>[k];
186
+ } & {
187
+ [
188
+ k in keyof ComponentProps<typeof Textfield> as `newPassword\$${k}`
189
+ ]?: ComponentProps<typeof Textfield>[k];
190
+ } & {
191
+ [
192
+ k in keyof ComponentProps<typeof Textfield> as `newPassword2\$${k}`
193
+ ]?: ComponentProps<typeof Textfield>[k];
194
+ } & {
195
+ [
196
+ k in keyof ComponentProps<typeof Checkbox> as `revokeTokens\$${k}`
197
+ ]?: ComponentProps<typeof Checkbox>[k];
198
+ } & {
199
+ [
200
+ k in keyof ComponentProps<typeof CircularProgress> as `progress\$${k}`
201
+ ]?: ComponentProps<typeof CircularProgress>[k];
202
+ } & {
203
+ [
204
+ k in keyof ComponentProps<
205
+ typeof Button<undefined, 'button'>
206
+ > as `closeButton\$${k}`
207
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
208
+ } & {
209
+ [
210
+ k in keyof ComponentProps<
211
+ typeof Button<undefined, 'button'>
212
+ > as `saveButton\$${k}`
213
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
214
+ } = $props();
215
+
216
+ let loading = $state(false);
217
+ let failureMessage: string | undefined = $state();
218
+
219
+ $effect(() => {
220
+ if (!open) {
221
+ loading = false;
222
+ failureMessage = undefined;
223
+ currentPassword = '';
224
+ newPassword = '';
225
+ newPassword2 = '';
226
+ revokeCurrentTokens = false;
227
+ }
228
+ });
229
+
230
+ const onLogin = (currentUser: UserClass & CurrentUserData) => {
231
+ $user = currentUser;
232
+ };
233
+ const onLogout = () => {
234
+ $user = null;
235
+ };
236
+
237
+ onMount(async () => {
238
+ User.on('login', onLogin);
239
+ User.on('logout', onLogout);
240
+ if ($user === (false as unknown as undefined)) {
241
+ $user = undefined;
242
+ $user = await User.current();
243
+ }
244
+ });
245
+
246
+ onDestroy(() => {
247
+ User.off('login', onLogin);
248
+ User.off('logout', onLogout);
249
+ });
250
+
251
+ async function changePassword() {
252
+ if (currentPassword === '') {
253
+ failureMessage = 'You need to enter your current password';
254
+ return;
255
+ }
256
+ if (newPassword !== newPassword2) {
257
+ failureMessage = 'Your passwords do not match.';
258
+ return;
259
+ }
260
+ if (newPassword === '') {
261
+ failureMessage = 'You need to enter a new password';
262
+ return;
263
+ }
264
+
265
+ failureMessage = undefined;
266
+ loading = true;
267
+
268
+ // Get the current user again, in case their data has changed.
269
+ $user = await User.current();
270
+
271
+ if ($user == null) {
272
+ failureMessage = 'You must be logged in.';
273
+ loading = false;
274
+ return;
275
+ }
276
+
277
+ try {
278
+ // Change the user's password.
279
+ const data = await $user.$changePassword({
280
+ currentPassword,
281
+ newPassword,
282
+ revokeCurrentTokens,
283
+ });
284
+
285
+ if (!data.result) {
286
+ failureMessage = data.message;
287
+ } else {
288
+ open = false;
289
+ }
290
+ } catch (e: any) {
291
+ failureMessage = e?.message;
292
+ }
293
+ loading = false;
294
+ }
295
+ </script>
296
+
297
+ <style>
298
+ :global(.mdc-dialog .mdc-dialog__surface.tilmeld-password-dialog-surface) {
299
+ width: 360px;
300
+ max-width: calc(100vw - 32px);
301
+ }
302
+ .tilmeld-password-failure {
303
+ margin-top: 1em;
304
+ color: var(--mdc-theme-error, #f00);
305
+ }
306
+ .tilmeld-password-loading {
307
+ display: flex;
308
+ justify-content: center;
309
+ align-items: center;
310
+ }
311
+ </style>