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

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

@ -1,18 +1,41 @@
import type { GalleryType, GridType, KanbanType, SortType } from 'nocodb-sdk' import type { GalleryType, GridType, KanbanType, SortType } from 'nocodb-sdk'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { useNuxtApp } from '#imports' import { useNuxtApp } from '#imports'
import { IsPublicInj, ReloadViewDataHookInj } from '~/context'
export function useViewSorts( export function useViewSorts(
view: Ref<(GridType | KanbanType | GalleryType) & { id?: string }> | undefined, view: Ref<(GridType | KanbanType | GalleryType) & { id?: string }> | undefined,
reloadData?: () => void, 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 { $api } = useNuxtApp()
const { isUIAllowed } = useUIPermission() const { isUIAllowed } = useUIPermission()
const loadSorts = async () => { 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 if (!view?.value) return
sorts.value = ((await $api.dbTableSort.list(view?.value?.id as string)) as any)?.sorts?.list 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) => { const saveOrUpdate = async (sort: SortType, i: number) => {
// TODO: // TODO:
// if (!this.shared && this._isUIAllowed('sortSync')) { // if (!this.shared && this._isUIAllowed('sortSync')) {
if (isPublic.value) {
sorts.value[i] = sort
sorts.value = [...sorts.value]
return
}
if (isUIAllowed('sortSync')) { if (isUIAllowed('sortSync')) {
if (sort.id) { if (sort.id) {
await $api.dbTableSort.update(sort.id, sort) await $api.dbTableSort.update(sort.id, sort)
@ -30,14 +59,23 @@ export function useViewSorts(
reloadData?.() reloadData?.()
} }
const addSort = () => { const addSort = () => {
sorts.value.push({ sorts.value = [
direction: 'asc', ...sorts.value,
}) {
direction: 'asc',
},
]
} }
const deleteSort = async (sort: SortType, i: number) => { const deleteSort = async (sort: SortType, i: number) => {
// TOOD: // TOOD:
// if (!this.shared && sort.id && this._isUIAllowed('sortSync')) { // 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) { if (isUIAllowed('sortSync') && sort.id) {
await $api.dbTableSort.delete(sort.id) await $api.dbTableSort.delete(sort.id)
} else { } else {

Loading…
Cancel
Save