StorageTables.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="flex mt-6">
  3. <ui-input
  4. v-model="state.query"
  5. :placeholder="t('common.search')"
  6. prepend-icon="riSearch2Line"
  7. />
  8. <div class="flex-grow"></div>
  9. <ui-button variant="accent" @click="state.showAddTable = true">
  10. {{ t('storage.table.add') }}
  11. </ui-button>
  12. </div>
  13. <ui-table
  14. item-key="id"
  15. :headers="tableHeaders"
  16. :items="items"
  17. :search="state.query"
  18. class="w-full mt-4"
  19. >
  20. <template #item-name="{ item }">
  21. <router-link
  22. :to="`/storage/tables/${item.id}`"
  23. class="w-full block"
  24. style="min-height: 29px"
  25. >
  26. {{ item.name }}
  27. </router-link>
  28. </template>
  29. <template #item-createdAt="{ item }">
  30. {{ formatDate(item.createdAt) }}
  31. </template>
  32. <template #item-modifiedAt="{ item }">
  33. {{ formatDate(item.modifiedAt) }}
  34. </template>
  35. <template #item-actions="{ item }">
  36. <v-remixicon
  37. name="riDeleteBin7Line"
  38. class="cursor-pointer"
  39. @click="deleteTable(item)"
  40. />
  41. </template>
  42. </ui-table>
  43. <storage-edit-table v-model="state.showAddTable" @save="saveTable" />
  44. </template>
  45. <script setup>
  46. import { reactive } from 'vue';
  47. import { useI18n } from 'vue-i18n';
  48. import dayjs from 'dayjs';
  49. import { useDialog } from '@/composable/dialog';
  50. import { useWorkflowStore } from '@/stores/workflow';
  51. import { useLiveQuery } from '@/composable/liveQuery';
  52. import dbStorage from '@/db/storage';
  53. import StorageEditTable from './StorageEditTable.vue';
  54. const { t } = useI18n();
  55. const dialog = useDialog();
  56. const workflowStore = useWorkflowStore();
  57. const state = reactive({
  58. query: '',
  59. showAddTable: false,
  60. });
  61. const tableHeaders = [
  62. {
  63. value: 'name',
  64. filterable: true,
  65. text: t('common.name'),
  66. attrs: {
  67. class: 'w-4/12',
  68. },
  69. },
  70. {
  71. align: 'center',
  72. value: 'createdAt',
  73. text: t('storage.table.createdAt'),
  74. },
  75. {
  76. align: 'center',
  77. value: 'modifiedAt',
  78. text: t('storage.table.modifiedAt'),
  79. },
  80. {
  81. value: 'rowsCount',
  82. align: 'center',
  83. text: t('storage.table.rowsCount'),
  84. },
  85. {
  86. value: 'actions',
  87. align: 'right',
  88. text: '',
  89. sortable: false,
  90. },
  91. ];
  92. const items = useLiveQuery(() => dbStorage.tablesItems.reverse().toArray());
  93. function formatDate(date) {
  94. return dayjs(date).format('DD MMM YYYY, hh:mm:ss A');
  95. }
  96. async function saveTable({ columns, name }) {
  97. try {
  98. const columnsIndex = columns.reduce(
  99. (acc, column) => {
  100. acc[column.id] = {
  101. index: 0,
  102. type: column.type,
  103. name: column.name,
  104. };
  105. return acc;
  106. },
  107. { column: { index: 0, type: 'any', name: 'column' } }
  108. );
  109. const tableId = await dbStorage.tablesItems.add({
  110. rowsCount: 0,
  111. name,
  112. createdAt: Date.now(),
  113. modifiedAt: Date.now(),
  114. columns,
  115. });
  116. await dbStorage.tablesData.add({
  117. tableId,
  118. items: [],
  119. columnsIndex,
  120. });
  121. state.showAddTable = false;
  122. } catch (error) {
  123. console.error(error);
  124. }
  125. }
  126. function deleteTable(table) {
  127. dialog.confirm({
  128. title: t('storage.table.delete'),
  129. okVariant: 'danger',
  130. body: t('message.delete', { name: table.name }),
  131. onConfirm: async () => {
  132. try {
  133. await dbStorage.tablesItems.where('id').equals(table.id).delete();
  134. await dbStorage.tablesData.where('tableId').equals(table.id).delete();
  135. await workflowStore.update({
  136. id: (workflow) => workflow.connectedTable === table.id,
  137. data: { connectedTable: null },
  138. });
  139. } catch (error) {
  140. console.error(error);
  141. }
  142. },
  143. });
  144. }
  145. </script>