Banners.svelte 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <script lang="ts">
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { getContext, onMount } from 'svelte';
  4. import { banners as _banners } from '$lib/stores';
  5. import type { Banner } from '$lib/types';
  6. import { getBanners, setBanners } from '$lib/apis/configs';
  7. import type { Writable } from 'svelte/store';
  8. import type { i18n as i18nType } from 'i18next';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import Switch from '$lib/components/common/Switch.svelte';
  11. const i18n: Writable<i18nType> = getContext('i18n');
  12. export let saveHandler: Function;
  13. let banners: Banner[] = [];
  14. onMount(async () => {
  15. banners = await getBanners(localStorage.token);
  16. });
  17. const updateBanners = async () => {
  18. _banners.set(await setBanners(localStorage.token, banners));
  19. };
  20. </script>
  21. <form
  22. class="flex flex-col h-full justify-between space-y-3 text-sm"
  23. on:submit|preventDefault={async () => {
  24. updateBanners();
  25. saveHandler();
  26. }}
  27. >
  28. <div class=" space-y-3 pr-1.5 overflow-y-scroll scrollbar-hidden h-full">
  29. <div class=" space-y-3 pr-1.5">
  30. <div class="flex w-full justify-between mb-2">
  31. <div class=" self-center text-sm font-semibold">
  32. {$i18n.t('Banners')}
  33. </div>
  34. <button
  35. class="p-1 px-3 text-xs flex rounded transition"
  36. type="button"
  37. on:click={() => {
  38. if (banners.length === 0 || banners.at(-1).content !== '') {
  39. banners = [
  40. ...banners,
  41. {
  42. id: uuidv4(),
  43. type: '',
  44. title: '',
  45. content: '',
  46. dismissible: true,
  47. timestamp: Math.floor(Date.now() / 1000)
  48. }
  49. ];
  50. }
  51. }}
  52. >
  53. <svg
  54. xmlns="http://www.w3.org/2000/svg"
  55. viewBox="0 0 20 20"
  56. fill="currentColor"
  57. class="w-4 h-4"
  58. >
  59. <path
  60. d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
  61. />
  62. </svg>
  63. </button>
  64. </div>
  65. <div class="flex flex-col space-y-1">
  66. {#each banners as banner, bannerIdx}
  67. <div class=" flex justify-between">
  68. <div class="flex flex-row flex-1 border rounded-xl dark:border-gray-800">
  69. <select
  70. class="w-fit capitalize rounded-xl py-2 px-4 text-xs bg-transparent outline-none"
  71. bind:value={banner.type}
  72. >
  73. {#if banner.type == ''}
  74. <option value="" selected disabled class="text-gray-900">{$i18n.t('Type')}</option
  75. >
  76. {/if}
  77. <option value="info" class="text-gray-900">{$i18n.t('Info')}</option>
  78. <option value="warning" class="text-gray-900">{$i18n.t('Warning')}</option>
  79. <option value="error" class="text-gray-900">{$i18n.t('Error')}</option>
  80. <option value="success" class="text-gray-900">{$i18n.t('Success')}</option>
  81. </select>
  82. <input
  83. class="pr-5 py-1.5 text-xs w-full bg-transparent outline-none"
  84. placeholder={$i18n.t('Content')}
  85. bind:value={banner.content}
  86. />
  87. <div class="relative top-1.5 -left-2">
  88. <Tooltip content={$i18n.t('Dismissible')} className="flex h-fit items-center">
  89. <Switch bind:state={banner.dismissible} />
  90. </Tooltip>
  91. </div>
  92. </div>
  93. <button
  94. class="px-2"
  95. type="button"
  96. on:click={() => {
  97. banners.splice(bannerIdx, 1);
  98. banners = banners;
  99. }}
  100. >
  101. <svg
  102. xmlns="http://www.w3.org/2000/svg"
  103. viewBox="0 0 20 20"
  104. fill="currentColor"
  105. class="w-4 h-4"
  106. >
  107. <path
  108. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  109. />
  110. </svg>
  111. </button>
  112. </div>
  113. {/each}
  114. </div>
  115. </div>
  116. </div>
  117. <div class="flex justify-end pt-3 text-sm font-medium">
  118. <button
  119. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  120. type="submit"
  121. >
  122. Save
  123. </button>
  124. </div>
  125. </form>