ChannelModal.svelte 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <script lang="ts">
  2. import { getContext, createEventDispatcher, onMount } from 'svelte';
  3. import { createNewChannel, deleteChannelById } from '$lib/apis/channels';
  4. import Modal from '$lib/components/common/Modal.svelte';
  5. import AccessControl from '$lib/components/workspace/common/AccessControl.svelte';
  6. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  7. import { toast } from 'svelte-sonner';
  8. import { page } from '$app/stores';
  9. import { goto } from '$app/navigation';
  10. const i18n = getContext('i18n');
  11. export let show = false;
  12. export let onSubmit: Function = () => {};
  13. export let onUpdate: Function = () => {};
  14. export let channel = null;
  15. export let edit = false;
  16. let name = '';
  17. let accessControl = null;
  18. let loading = false;
  19. $: if (name) {
  20. name = name.replace(/\s/g, '-').toLocaleLowerCase();
  21. }
  22. const submitHandler = async () => {
  23. loading = true;
  24. await onSubmit({
  25. name: name.replace(/\s/g, '-'),
  26. access_control: accessControl
  27. });
  28. show = false;
  29. loading = false;
  30. };
  31. const init = () => {
  32. name = channel.name;
  33. accessControl = channel.access_control;
  34. };
  35. $: if (channel) {
  36. init();
  37. }
  38. let showDeleteConfirmDialog = false;
  39. const deleteHandler = async () => {
  40. showDeleteConfirmDialog = false;
  41. const res = await deleteChannelById(localStorage.token, channel.id).catch((error) => {
  42. toast.error(error.message);
  43. });
  44. if (res) {
  45. toast.success('Channel deleted successfully');
  46. onUpdate();
  47. if ($page.url.pathname === `/channels/${channel.id}`) {
  48. goto('/');
  49. }
  50. }
  51. show = false;
  52. };
  53. </script>
  54. <Modal size="sm" bind:show>
  55. <div>
  56. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-1">
  57. <div class=" text-lg font-medium self-center">
  58. {#if edit}
  59. {$i18n.t('Edit Channel')}
  60. {:else}
  61. {$i18n.t('Create Channel')}
  62. {/if}
  63. </div>
  64. <button
  65. class="self-center"
  66. on:click={() => {
  67. show = false;
  68. }}
  69. >
  70. <svg
  71. xmlns="http://www.w3.org/2000/svg"
  72. viewBox="0 0 20 20"
  73. fill="currentColor"
  74. class="w-5 h-5"
  75. >
  76. <path
  77. fill-rule="evenodd"
  78. 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"
  79. clip-rule="evenodd"
  80. />
  81. </svg>
  82. </button>
  83. </div>
  84. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  85. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  86. <form
  87. class="flex flex-col w-full"
  88. on:submit|preventDefault={() => {
  89. submitHandler();
  90. }}
  91. >
  92. <div class="flex flex-col w-full mt-2">
  93. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Channel Name')}</div>
  94. <div class="flex-1">
  95. <input
  96. class="w-full text-sm bg-transparent placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none"
  97. type="text"
  98. bind:value={name}
  99. placeholder={$i18n.t('new-channel')}
  100. autocomplete="off"
  101. />
  102. </div>
  103. </div>
  104. <hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" />
  105. <div class="my-2 -mx-2">
  106. <div class="px-3 py-2 bg-gray-50 dark:bg-gray-950 rounded-lg">
  107. <AccessControl bind:accessControl />
  108. </div>
  109. </div>
  110. <div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
  111. {#if edit}
  112. <button
  113. class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-black/90 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
  114. type="button"
  115. on:click={() => {
  116. showDeleteConfirmDialog = true;
  117. }}
  118. >
  119. {$i18n.t('Delete')}
  120. </button>
  121. {/if}
  122. <button
  123. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-950 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center {loading
  124. ? ' cursor-not-allowed'
  125. : ''}"
  126. type="submit"
  127. disabled={loading}
  128. >
  129. {#if edit}
  130. {$i18n.t('Update')}
  131. {:else}
  132. {$i18n.t('Create')}
  133. {/if}
  134. {#if loading}
  135. <div class="ml-2 self-center">
  136. <svg
  137. class=" w-4 h-4"
  138. viewBox="0 0 24 24"
  139. fill="currentColor"
  140. xmlns="http://www.w3.org/2000/svg"
  141. ><style>
  142. .spinner_ajPY {
  143. transform-origin: center;
  144. animation: spinner_AtaB 0.75s infinite linear;
  145. }
  146. @keyframes spinner_AtaB {
  147. 100% {
  148. transform: rotate(360deg);
  149. }
  150. }
  151. </style><path
  152. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  153. opacity=".25"
  154. /><path
  155. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  156. class="spinner_ajPY"
  157. /></svg
  158. >
  159. </div>
  160. {/if}
  161. </button>
  162. </div>
  163. </form>
  164. </div>
  165. </div>
  166. </div>
  167. </Modal>
  168. <DeleteConfirmDialog
  169. bind:show={showDeleteConfirmDialog}
  170. message={$i18n.t('Are you sure you want to delete this channel?')}
  171. confirmLabel={$i18n.t('Delete')}
  172. on:confirm={() => {
  173. deleteHandler();
  174. }}
  175. />