General.svelte 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <script lang="ts">
  2. import {
  3. getCommunitySharingEnabledStatus,
  4. getWebhookUrl,
  5. toggleCommunitySharingEnabledStatus,
  6. updateWebhookUrl
  7. } from '$lib/apis';
  8. import {
  9. getAdminConfig,
  10. getDefaultUserRole,
  11. getJWTExpiresDuration,
  12. getSignUpEnabledStatus,
  13. toggleSignUpEnabledStatus,
  14. updateAdminConfig,
  15. updateDefaultUserRole,
  16. updateJWTExpiresDuration
  17. } from '$lib/apis/auths';
  18. import Switch from '$lib/components/common/Switch.svelte';
  19. import { onMount, getContext } from 'svelte';
  20. const i18n = getContext('i18n');
  21. export let saveHandler: Function;
  22. let adminConfig = null;
  23. let webhookUrl = '';
  24. const updateHandler = async () => {
  25. webhookUrl = await updateWebhookUrl(localStorage.token, webhookUrl);
  26. const res = await updateAdminConfig(localStorage.token, adminConfig);
  27. if (res) {
  28. toast.success(i18n.t('Settings updated successfully'));
  29. } else {
  30. toast.error(i18n.t('Failed to update settings'));
  31. }
  32. };
  33. onMount(async () => {
  34. await Promise.all([
  35. (async () => {
  36. adminConfig = await getAdminConfig(localStorage.token);
  37. })(),
  38. (async () => {
  39. webhookUrl = await getWebhookUrl(localStorage.token);
  40. })()
  41. ]);
  42. });
  43. </script>
  44. <form
  45. class="flex flex-col h-full justify-between space-y-3 text-sm"
  46. on:submit|preventDefault={() => {
  47. updateHandler();
  48. saveHandler();
  49. }}
  50. >
  51. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  52. {#if adminConfig !== null}
  53. <div>
  54. <div class=" mb-3 text-sm font-medium">{$i18n.t('General Settings')}</div>
  55. <div class=" flex w-full justify-between pr-2">
  56. <div class=" self-center text-xs font-medium">{$i18n.t('Enable New Sign Ups')}</div>
  57. <Switch bind:state={adminConfig.ENABLE_SIGNUP} />
  58. </div>
  59. <div class=" my-3 flex w-full justify-between">
  60. <div class=" self-center text-xs font-medium">{$i18n.t('Default User Role')}</div>
  61. <div class="flex items-center relative">
  62. <select
  63. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 text-xs bg-transparent outline-none text-right"
  64. bind:value={adminConfig.DEFAULT_USER_ROLE}
  65. placeholder="Select a role"
  66. >
  67. <option value="pending">{$i18n.t('pending')}</option>
  68. <option value="user">{$i18n.t('user')}</option>
  69. <option value="admin">{$i18n.t('admin')}</option>
  70. </select>
  71. </div>
  72. </div>
  73. <hr class=" dark:border-gray-850 my-2" />
  74. <div class="my-3 flex w-full items-center justify-between pr-2">
  75. <div class=" self-center text-xs font-medium">
  76. {$i18n.t('Show Admin Details in Account Pending Overlay')}
  77. </div>
  78. <Switch bind:state={adminConfig.SHOW_ADMIN_DETAILS} />
  79. </div>
  80. <div class="my-3 flex w-full items-center justify-between pr-2">
  81. <div class=" self-center text-xs font-medium">{$i18n.t('Enable Community Sharing')}</div>
  82. <Switch bind:state={adminConfig.ENABLE_COMMUNITY_SHARING} />
  83. </div>
  84. <hr class=" dark:border-gray-850 my-2" />
  85. <div class=" w-full justify-between">
  86. <div class="flex w-full justify-between">
  87. <div class=" self-center text-xs font-medium">{$i18n.t('JWT Expiration')}</div>
  88. </div>
  89. <div class="flex mt-2 space-x-2">
  90. <input
  91. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  92. type="text"
  93. placeholder={`e.g.) "30m","1h", "10d". `}
  94. bind:value={adminConfig.JWT_EXPIRES_IN}
  95. />
  96. </div>
  97. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  98. {$i18n.t('Valid time units:')}
  99. <span class=" text-gray-300 font-medium"
  100. >{$i18n.t("'s', 'm', 'h', 'd', 'w' or '-1' for no expiration.")}</span
  101. >
  102. </div>
  103. </div>
  104. <hr class=" dark:border-gray-850 my-2" />
  105. <div class=" w-full justify-between">
  106. <div class="flex w-full justify-between">
  107. <div class=" self-center text-xs font-medium">{$i18n.t('Webhook URL')}</div>
  108. </div>
  109. <div class="flex mt-2 space-x-2">
  110. <input
  111. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  112. type="text"
  113. placeholder={`https://example.com/webhook`}
  114. bind:value={webhookUrl}
  115. />
  116. </div>
  117. </div>
  118. </div>
  119. {/if}
  120. </div>
  121. <div class="flex justify-end pt-3 text-sm font-medium">
  122. <button
  123. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  124. type="submit"
  125. >
  126. {$i18n.t('Save')}
  127. </button>
  128. </div>
  129. </form>