Browse Source

refactor/gui-v2-added sort to Share view

pull/3083/head
Muhammed Mustafa 2 years ago
parent
commit
71976bca72
  1. 6
      packages/nc-gui-v2/composables/useSharedView.ts
  2. 44
      packages/nc-gui-v2/composables/useViewSorts.ts

6
packages/nc-gui-v2/composables/useSharedView.ts

@ -1,9 +1,10 @@
import type { ColumnType, FilterType, PaginatedType, TableType, ViewType } from 'nocodb-sdk'
import type { ColumnType, FilterType, PaginatedType, SortType, TableType, ViewType } from 'nocodb-sdk'
import { useNuxtApp } from '#app'
const filters = ref<(FilterType & { status?: 'update' | 'delete' | 'create'; parentId?: string })[]>([])
const paginationData = ref<PaginatedType>({ page: 1, pageSize: 25 })
const sharedView = ref<ViewType>()
const sorts = ref<SortType[]>([])
export function useSharedView() {
const meta = ref<TableType>(() => sharedView.value?.model)
@ -32,10 +33,11 @@ export function useSharedView() {
const { data } = await $api.public.dataList(sharedView?.value?.uuid, {
offset: (page - 1) * pageSize,
filterArrJson: JSON.stringify(filters.value),
sortArrJson: JSON.stringify(sorts.value),
} as any)
return data
}
return { sharedView, loadSharedView, meta, columns, filters, fetchSharedViewData, paginationData }
return { sharedView, loadSharedView, meta, columns, filters, fetchSharedViewData, paginationData, sorts }
}

44
packages/nc-gui-v2/composables/useViewSorts.ts

@ -1,18 +1,41 @@
import type { GalleryType, GridType, KanbanType, SortType } from 'nocodb-sdk'
import type { Ref } from 'vue'
import { useNuxtApp } from '#imports'
import { IsPublicInj, ReloadViewDataHookInj } from '~/context'
export function useViewSorts(
view: Ref<(GridType | KanbanType | GalleryType) & { id?: string }> | undefined,
reloadData?: () => void,
) {
const sorts = ref<SortType[]>([])
const _sorts = ref<SortType[]>([])
const { sorts: sharedViewSorts, sharedView } = useSharedView()
const reloadHook = inject(ReloadViewDataHookInj)
const isPublic = inject(IsPublicInj, ref(false))
const sorts = computed<SortType[]>({
get: () => (isPublic.value ? sharedViewSorts.value : _sorts.value),
set: (value) => {
if (isPublic.value) {
sharedViewSorts.value = value
} else {
_sorts.value = value
}
reloadHook?.trigger()
},
})
const { $api } = useNuxtApp()
const { isUIAllowed } = useUIPermission()
const loadSorts = async () => {
if (isPublic.value) {
console.log('load sorts public', sharedView.value?.sorts)
const sharedSorts = sharedView.value?.sorts || []
sorts.value = [...sharedSorts]
return
}
if (!view?.value) return
sorts.value = ((await $api.dbTableSort.list(view?.value?.id as string)) as any)?.sorts?.list
}
@ -20,6 +43,12 @@ export function useViewSorts(
const saveOrUpdate = async (sort: SortType, i: number) => {
// TODO:
// if (!this.shared && this._isUIAllowed('sortSync')) {
if (isPublic.value) {
sorts.value[i] = sort
sorts.value = [...sorts.value]
return
}
if (isUIAllowed('sortSync')) {
if (sort.id) {
await $api.dbTableSort.update(sort.id, sort)
@ -30,14 +59,23 @@ export function useViewSorts(
reloadData?.()
}
const addSort = () => {
sorts.value.push({
sorts.value = [
...sorts.value,
{
direction: 'asc',
})
},
]
}
const deleteSort = async (sort: SortType, i: number) => {
// TOOD:
// if (!this.shared && sort.id && this._isUIAllowed('sortSync')) {
if (isPublic.value) {
sorts.value.splice(i, 1)
sorts.value = [...sorts.value]
return
}
if (isUIAllowed('sortSync') && sort.id) {
await $api.dbTableSort.delete(sort.id)
} else {

Loading…
Cancel
Save