1
0

AddDocModal.svelte 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { onMount, getContext } from 'svelte';
  5. import { getDocs } from '$lib/apis/documents';
  6. import Modal from '../common/Modal.svelte';
  7. import { documents } from '$lib/stores';
  8. import { SUPPORTED_FILE_EXTENSIONS, SUPPORTED_FILE_TYPE } from '$lib/constants';
  9. import Tags from '../common/Tags.svelte';
  10. const i18n = getContext('i18n');
  11. export let show = false;
  12. export let uploadDoc: Function;
  13. let uploadDocInputElement: HTMLInputElement;
  14. let inputFiles;
  15. let tags = [];
  16. let doc = {
  17. name: '',
  18. title: '',
  19. content: null
  20. };
  21. const submitHandler = async () => {
  22. if (inputFiles && inputFiles.length > 0) {
  23. for (const file of inputFiles) {
  24. console.log(file, file.name.split('.').at(-1));
  25. if (
  26. SUPPORTED_FILE_TYPE.includes(file['type']) ||
  27. SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
  28. ) {
  29. uploadDoc(file, tags);
  30. } else {
  31. toast.error(
  32. `Unknown File Type '${file['type']}', but accepting and treating as plain text`
  33. );
  34. uploadDoc(file, tags);
  35. }
  36. }
  37. inputFiles = null;
  38. uploadDocInputElement.value = '';
  39. } else {
  40. toast.error($i18n.t(`File not found.`));
  41. }
  42. show = false;
  43. documents.set(await getDocs(localStorage.token));
  44. };
  45. const addTagHandler = async (tagName) => {
  46. if (!tags.find((tag) => tag.name === tagName) && tagName !== '') {
  47. tags = [...tags, { name: tagName }];
  48. } else {
  49. console.log('tag already exists');
  50. }
  51. };
  52. const deleteTagHandler = async (tagName) => {
  53. tags = tags.filter((tag) => tag.name !== tagName);
  54. };
  55. onMount(() => {});
  56. </script>
  57. <Modal size="sm" bind:show>
  58. <div>
  59. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4">
  60. <div class=" text-lg font-medium self-center">{$i18n.t('Add Docs')}</div>
  61. <button
  62. class="self-center"
  63. on:click={() => {
  64. show = false;
  65. }}
  66. >
  67. <svg
  68. xmlns="http://www.w3.org/2000/svg"
  69. viewBox="0 0 20 20"
  70. fill="currentColor"
  71. class="w-5 h-5"
  72. >
  73. <path
  74. 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"
  75. />
  76. </svg>
  77. </button>
  78. </div>
  79. <div class="flex flex-col md:flex-row w-full px-5 py-4 md:space-x-4 dark:text-gray-200">
  80. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  81. <form
  82. class="flex flex-col w-full"
  83. on:submit|preventDefault={() => {
  84. submitHandler();
  85. }}
  86. >
  87. <div class="mb-3 w-full">
  88. <input
  89. id="upload-doc-input"
  90. bind:this={uploadDocInputElement}
  91. hidden
  92. bind:files={inputFiles}
  93. type="file"
  94. multiple
  95. />
  96. <button
  97. class="w-full text-sm font-medium py-3 bg-gray-100 hover:bg-gray-200 dark:bg-gray-850 dark:hover:bg-gray-800 text-center rounded-xl"
  98. type="button"
  99. on:click={() => {
  100. uploadDocInputElement.click();
  101. }}
  102. >
  103. {#if inputFiles}
  104. {inputFiles.length > 0 ? `${inputFiles.length}` : ''} document(s) selected.
  105. {:else}
  106. {$i18n.t('Click here to select documents.')}
  107. {/if}
  108. </button>
  109. </div>
  110. <div class=" flex flex-col space-y-1.5">
  111. <div class="flex flex-col w-full">
  112. <div class=" mb-1.5 text-xs text-gray-500">{$i18n.t('Tags')}</div>
  113. <Tags {tags} addTag={addTagHandler} deleteTag={deleteTagHandler} />
  114. </div>
  115. </div>
  116. <div class="flex justify-end pt-5 text-sm font-medium">
  117. <button
  118. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  119. type="submit"
  120. >
  121. {$i18n.t('Save')}
  122. </button>
  123. </div>
  124. </form>
  125. </div>
  126. </div>
  127. </div>
  128. </Modal>
  129. <style>
  130. input::-webkit-outer-spin-button,
  131. input::-webkit-inner-spin-button {
  132. /* display: none; <- Crashes Chrome on hover */
  133. -webkit-appearance: none;
  134. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  135. }
  136. .tabs::-webkit-scrollbar {
  137. display: none; /* for Chrome, Safari and Opera */
  138. }
  139. .tabs {
  140. -ms-overflow-style: none; /* IE and Edge */
  141. scrollbar-width: none; /* Firefox */
  142. }
  143. input[type='number'] {
  144. -moz-appearance: textfield; /* Firefox */
  145. }
  146. </style>