General.svelte 6.8 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
2
3
4
5
6
7
	import {
		getCommunitySharingEnabledStatus,
		getWebhookUrl,
		toggleCommunitySharingEnabledStatus,
		updateWebhookUrl
	} from '$lib/apis';
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
	import {
		getDefaultUserRole,
Timothy J. Baek's avatar
Timothy J. Baek committed
10
		getJWTExpiresDuration,
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
		getSignUpEnabledStatus,
		toggleSignUpEnabledStatus,
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
		updateDefaultUserRole,
		updateJWTExpiresDuration
Timothy J. Baek's avatar
Timothy J. Baek committed
15
	} from '$lib/apis/auths';
16
17
18
	import { onMount, getContext } from 'svelte';

	const i18n = getContext('i18n');
Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
22

	export let saveHandler: Function;
	let signUpEnabled = true;
	let defaultUserRole = 'pending';
Timothy J. Baek's avatar
Timothy J. Baek committed
23
	let JWTExpiresIn = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
24

25
	let webhookUrl = '';
26
	let communitySharingEnabled = true;
27

Timothy J. Baek's avatar
Timothy J. Baek committed
28
29
30
31
32
33
34
35
	const toggleSignUpEnabled = async () => {
		signUpEnabled = await toggleSignUpEnabledStatus(localStorage.token);
	};

	const updateDefaultUserRoleHandler = async (role) => {
		defaultUserRole = await updateDefaultUserRole(localStorage.token, role);
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
36
37
38
39
	const updateJWTExpiresDurationHandler = async (duration) => {
		JWTExpiresIn = await updateJWTExpiresDuration(localStorage.token, duration);
	};

40
41
42
43
	const updateWebhookUrlHandler = async () => {
		webhookUrl = await updateWebhookUrl(localStorage.token, webhookUrl);
	};

44
45
46
47
	const toggleCommunitySharingEnabled = async () => {
		communitySharingEnabled = await toggleCommunitySharingEnabledStatus(localStorage.token);
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
48
	onMount(async () => {
49
50
51
52
53
54
55
56
57
58
59
60
		await Promise.all([
			(async () => {
				signUpEnabled = await getSignUpEnabledStatus(localStorage.token);
			})(),
			(async () => {
				defaultUserRole = await getDefaultUserRole(localStorage.token);
			})(),
			(async () => {
				JWTExpiresIn = await getJWTExpiresDuration(localStorage.token);
			})(),
			(async () => {
				webhookUrl = await getWebhookUrl(localStorage.token);
61
62
63
			})(),
			(async () => {
				communitySharingEnabled = await getCommunitySharingEnabledStatus(localStorage.token);
64
65
			})()
		]);
Timothy J. Baek's avatar
Timothy J. Baek committed
66
67
68
69
70
71
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
72
		updateJWTExpiresDurationHandler(JWTExpiresIn);
73
		updateWebhookUrlHandler();
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
77
78
		saveHandler();
	}}
>
	<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
		<div>
79
			<div class=" mb-2 text-sm font-medium">{$i18n.t('General Settings')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81

			<div class="  flex w-full justify-between">
82
				<div class=" self-center text-xs font-medium">{$i18n.t('Enable New Sign Ups')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleSignUpEnabled();
					}}
					type="button"
				>
					{#if signUpEnabled}
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								d="M11.5 1A3.5 3.5 0 0 0 8 4.5V7H2.5A1.5 1.5 0 0 0 1 8.5v5A1.5 1.5 0 0 0 2.5 15h7a1.5 1.5 0 0 0 1.5-1.5v-5A1.5 1.5 0 0 0 9.5 7V4.5a2 2 0 1 1 4 0v1.75a.75.75 0 0 0 1.5 0V4.5A3.5 3.5 0 0 0 11.5 1Z"
							/>
						</svg>
102
						<span class="ml-2 self-center">{$i18n.t('Enabled')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
					{:else}
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								fill-rule="evenodd"
								d="M8 1a3.5 3.5 0 0 0-3.5 3.5V7A1.5 1.5 0 0 0 3 8.5v5A1.5 1.5 0 0 0 4.5 15h7a1.5 1.5 0 0 0 1.5-1.5v-5A1.5 1.5 0 0 0 11.5 7V4.5A3.5 3.5 0 0 0 8 1Zm2 6V4.5a2 2 0 1 0-4 0V7h4Z"
								clip-rule="evenodd"
							/>
						</svg>

117
						<span class="ml-2 self-center">{$i18n.t('Disabled')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
120
121
122
					{/if}
				</button>
			</div>

			<div class=" flex w-full justify-between">
123
				<div class=" self-center text-xs font-medium">{$i18n.t('Default User Role')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125
				<div class="flex items-center relative">
					<select
Jannik Streidl's avatar
Jannik Streidl committed
126
						class="dark:bg-gray-900 w-fit pr-8 rounded py-2 px-2 text-xs bg-transparent outline-none text-right"
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128
129
130
131
132
						bind:value={defaultUserRole}
						placeholder="Select a theme"
						on:change={(e) => {
							updateDefaultUserRoleHandler(e.target.value);
						}}
					>
Jannik Streidl's avatar
Jannik Streidl committed
133
134
135
						<option value="pending">{$i18n.t('pending')}</option>
						<option value="user">{$i18n.t('user')}</option>
						<option value="admin">{$i18n.t('admin')}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
136
137
138
					</select>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
			<div class="  flex w-full justify-between">
				<div class=" self-center text-xs font-medium">{$i18n.t('Enable Community Sharing')}</div>

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleCommunitySharingEnabled();
					}}
					type="button"
				>
					{#if communitySharingEnabled}
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								d="M11.5 1A3.5 3.5 0 0 0 8 4.5V7H2.5A1.5 1.5 0 0 0 1 8.5v5A1.5 1.5 0 0 0 2.5 15h7a1.5 1.5 0 0 0 1.5-1.5v-5A1.5 1.5 0 0 0 9.5 7V4.5a2 2 0 1 1 4 0v1.75a.75.75 0 0 0 1.5 0V4.5A3.5 3.5 0 0 0 11.5 1Z"
							/>
						</svg>
						<span class="ml-2 self-center">{$i18n.t('Enabled')}</span>
					{:else}
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								fill-rule="evenodd"
								d="M8 1a3.5 3.5 0 0 0-3.5 3.5V7A1.5 1.5 0 0 0 3 8.5v5A1.5 1.5 0 0 0 4.5 15h7a1.5 1.5 0 0 0 1.5-1.5v-5A1.5 1.5 0 0 0 11.5 7V4.5A3.5 3.5 0 0 0 8 1Zm2 6V4.5a2 2 0 1 0-4 0V7h4Z"
								clip-rule="evenodd"
							/>
						</svg>

						<span class="ml-2 self-center">{$i18n.t('Disabled')}</span>
					{/if}
				</button>
			</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
181
			<hr class=" dark:border-gray-700 my-3" />
182
183
184
185
186
187
188
189

			<div class=" w-full justify-between">
				<div class="flex w-full justify-between">
					<div class=" self-center text-xs font-medium">{$i18n.t('Webhook URL')}</div>
				</div>

				<div class="flex mt-2 space-x-2">
					<input
Timothy J. Baek's avatar
Timothy J. Baek committed
190
						class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
191
192
193
194
195
196
197
198
						type="text"
						placeholder={`https://example.com/webhook`}
						bind:value={webhookUrl}
					/>
				</div>
			</div>

			<hr class=" dark:border-gray-700 my-3" />
Timothy J. Baek's avatar
Timothy J. Baek committed
199
200
201

			<div class=" w-full justify-between">
				<div class="flex w-full justify-between">
202
					<div class=" self-center text-xs font-medium">{$i18n.t('JWT Expiration')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
203
204
205
206
				</div>

				<div class="flex mt-2 space-x-2">
					<input
Timothy J. Baek's avatar
Timothy J. Baek committed
207
						class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
208
209
210
211
212
213
214
						type="text"
						placeholder={`e.g.) "30m","1h", "10d". `}
						bind:value={JWTExpiresIn}
					/>
				</div>

				<div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
215
216
217
					{$i18n.t('Valid time units:')}
					<span class=" text-gray-300 font-medium"
						>{$i18n.t("'s', 'm', 'h', 'd', 'w' or '-1' for no expiration.")}</span
Timothy J. Baek's avatar
Timothy J. Baek committed
218
219
220
					>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
221
222
223
224
225
		</div>
	</div>

	<div class="flex justify-end pt-3 text-sm font-medium">
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
226
			class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
Timothy J. Baek's avatar
Timothy J. Baek committed
227
228
			type="submit"
		>
Jannik Streidl's avatar
Jannik Streidl committed
229
			{$i18n.t('Save')}
Timothy J. Baek's avatar
Timothy J. Baek committed
230
231
232
		</button>
	</div>
</form>