General.svelte 4.35 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
	import {
9
		getAdminConfig,
Timothy J. Baek's avatar
Timothy J. Baek committed
10
		getDefaultUserRole,
Timothy J. Baek's avatar
Timothy J. Baek committed
11
		getJWTExpiresDuration,
Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
		getSignUpEnabledStatus,
		toggleSignUpEnabledStatus,
14
		updateAdminConfig,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
		updateDefaultUserRole,
		updateJWTExpiresDuration
Timothy J. Baek's avatar
Timothy J. Baek committed
17
	} from '$lib/apis/auths';
18
	import Switch from '$lib/components/common/Switch.svelte';
19
20
21
	import { onMount, getContext } from 'svelte';

	const i18n = getContext('i18n');
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24

	export let saveHandler: Function;

25
	let adminConfig = null;
26
27
	let webhookUrl = '';

28
	const updateHandler = async () => {
29
		webhookUrl = await updateWebhookUrl(localStorage.token, webhookUrl);
30
		const res = await updateAdminConfig(localStorage.token, adminConfig);
31

32
33
34
35
36
		if (res) {
			toast.success(i18n.t('Settings updated successfully'));
		} else {
			toast.error(i18n.t('Failed to update settings'));
		}
37
38
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
39
	onMount(async () => {
40
41
		await Promise.all([
			(async () => {
42
				adminConfig = await getAdminConfig(localStorage.token);
43
			})(),
44

45
46
47
48
			(async () => {
				webhookUrl = await getWebhookUrl(localStorage.token);
			})()
		]);
Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
51
52
53
54
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={() => {
55
		updateHandler();
Timothy J. Baek's avatar
Timothy J. Baek committed
56
57
58
		saveHandler();
	}}
>
59
60
61
62
63
64
65
	<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
		{#if adminConfig !== null}
			<div>
				<div class=" mb-3 text-sm font-medium">{$i18n.t('General Settings')}</div>

				<div class="  flex w-full justify-between pr-2">
					<div class=" self-center text-xs font-medium">{$i18n.t('Enable New Sign Ups')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
66

67
					<Switch bind:state={adminConfig.ENABLE_SIGNUP} />
Timothy J. Baek's avatar
Timothy J. Baek committed
68
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
69

70
71
72
73
74
75
76
				<div class="  my-3 flex w-full justify-between">
					<div class=" self-center text-xs font-medium">{$i18n.t('Default User Role')}</div>
					<div class="flex items-center relative">
						<select
							class="dark:bg-gray-900 w-fit pr-8 rounded px-2 text-xs bg-transparent outline-none text-right"
							bind:value={adminConfig.DEFAULT_USER_ROLE}
							placeholder="Select a role"
77
						>
78
79
80
81
82
83
							<option value="pending">{$i18n.t('pending')}</option>
							<option value="user">{$i18n.t('user')}</option>
							<option value="admin">{$i18n.t('admin')}</option>
						</select>
					</div>
				</div>
84

85
				<hr class=" dark:border-gray-850 my-2" />
86

87
88
89
90
				<div class="my-3 flex w-full items-center justify-between pr-2">
					<div class=" self-center text-xs font-medium">
						{$i18n.t('Show Admin Details in Account Pending Overlay')}
					</div>
91

92
					<Switch bind:state={adminConfig.SHOW_ADMIN_DETAILS} />
93
94
				</div>

95
96
				<div class="my-3 flex w-full items-center justify-between pr-2">
					<div class=" self-center text-xs font-medium">{$i18n.t('Enable Community Sharing')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
97

98
					<Switch bind:state={adminConfig.ENABLE_COMMUNITY_SHARING} />
Timothy J. Baek's avatar
Timothy J. Baek committed
99
100
				</div>

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
				<hr class=" dark:border-gray-850 my-2" />

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

					<div class="flex mt-2 space-x-2">
						<input
							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
							type="text"
							placeholder={`e.g.) "30m","1h", "10d". `}
							bind:value={adminConfig.JWT_EXPIRES_IN}
						/>
					</div>

					<div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
						{$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
						>
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
123
124
				</div>

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
				<hr class=" dark:border-gray-850 my-2" />

				<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
							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
							type="text"
							placeholder={`https://example.com/webhook`}
							bind:value={webhookUrl}
						/>
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
140
141
				</div>
			</div>
142
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
143
144
145
146
	</div>

	<div class="flex justify-end pt-3 text-sm font-medium">
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
147
			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
148
149
			type="submit"
		>
Jannik Streidl's avatar
Jannik Streidl committed
150
			{$i18n.t('Save')}
Timothy J. Baek's avatar
Timothy J. Baek committed
151
152
153
		</button>
	</div>
</form>