Browse Source

Merge pull request #3251 from nocodb/fix/gui-v2-added-filter-sort-for-viewer-user

fix/Added filter sorting for user with viewer role
pull/3281/head
Raju Udava 2 years ago committed by GitHub
parent
commit
0fe07bd155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      packages/nc-gui-v2/components/shared-view/Grid.vue
  2. 12
      packages/nc-gui-v2/components/smartsheet-toolbar/ColumnFilter.vue
  3. 6
      packages/nc-gui-v2/components/smartsheet-toolbar/ColumnFilterMenu.vue
  4. 14
      packages/nc-gui-v2/composables/useSmartsheetStore.ts
  5. 6
      packages/nc-gui-v2/composables/useViewColumns.ts
  6. 9
      packages/nc-gui-v2/composables/useViewData.ts
  7. 164
      packages/nc-gui-v2/composables/useViewFilters.ts
  8. 68
      packages/nc-gui-v2/composables/useViewSorts.ts

4
packages/nc-gui-v2/components/shared-view/Grid.vue

@ -4,7 +4,7 @@ import type { TableType } from 'nocodb-sdk'
import { ActiveViewInj, FieldsInj, IsPublicInj, MetaInj, ReadonlyInj, ReloadViewDataHookInj } from '~/context' import { ActiveViewInj, FieldsInj, IsPublicInj, MetaInj, ReadonlyInj, ReloadViewDataHookInj } from '~/context'
const { sharedView, meta } = useSharedView() const { sharedView, meta, sorts, nestedFilters } = useSharedView()
const reloadEventHook = createEventHook<void>() const reloadEventHook = createEventHook<void>()
provide(ReloadViewDataHookInj, reloadEventHook) provide(ReloadViewDataHookInj, reloadEventHook)
@ -14,7 +14,7 @@ provide(ActiveViewInj, sharedView)
provide(FieldsInj, ref(meta.value.columns as any[])) provide(FieldsInj, ref(meta.value.columns as any[]))
provide(IsPublicInj, ref(true)) provide(IsPublicInj, ref(true))
useProvideSmartsheetStore(sharedView as Ref<TableType>, meta) useProvideSmartsheetStore(sharedView as Ref<TableType>, meta, true, sorts, nestedFilters)
</script> </script>
<template> <template>

12
packages/nc-gui-v2/components/smartsheet-toolbar/ColumnFilter.vue

@ -41,6 +41,7 @@ const reloadDataHook = inject(ReloadViewDataHookInj)!
const { $e } = useNuxtApp() const { $e } = useNuxtApp()
const { nestedFilters } = useSmartsheetStoreOrThrow()
const { filters, deleteFilter, saveOrUpdate, loadFilters, addFilter, addFilterGroup, sync } = useViewFilters( const { filters, deleteFilter, saveOrUpdate, loadFilters, addFilter, addFilterGroup, sync } = useViewFilters(
activeView, activeView,
parentId, parentId,
@ -48,10 +49,11 @@ const { filters, deleteFilter, saveOrUpdate, loadFilters, addFilter, addFilterGr
() => { () => {
reloadDataHook.trigger() reloadDataHook.trigger()
}, },
modelValue, modelValue || nestedFilters.value,
!modelValue,
) )
const nestedFilters = ref() const localNestedFilters = ref()
const filterUpdateCondition = (filter: FilterType, i: number) => { const filterUpdateCondition = (filter: FilterType, i: number) => {
saveOrUpdate(filter, i) saveOrUpdate(filter, i)
@ -115,9 +117,9 @@ watch(
const applyChanges = async (hookId?: string) => { const applyChanges = async (hookId?: string) => {
await sync(hookId) await sync(hookId)
if (!nestedFilters.value?.length) return if (!localNestedFilters.value?.length) return
for (const nestedFilter of nestedFilters.value) { for (const nestedFilter of localNestedFilters.value) {
if (nestedFilter.parentId) { if (nestedFilter.parentId) {
await nestedFilter.applyChanges(hookId, true) await nestedFilter.applyChanges(hookId, true)
} }
@ -166,7 +168,7 @@ defineExpose({
<div class="col-span-5"> <div class="col-span-5">
<SmartsheetToolbarColumnFilter <SmartsheetToolbarColumnFilter
v-if="filter.id || filter.children" v-if="filter.id || filter.children"
ref="nestedFilters" ref="localNestedFilters"
v-model="filter.children" v-model="filter.children"
:parent-id="filter.id" :parent-id="filter.id"
nested nested

6
packages/nc-gui-v2/components/smartsheet-toolbar/ColumnFilterMenu.vue

@ -12,11 +12,15 @@ const { filterAutoSave } = useGlobal()
const filterComp = ref<typeof ColumnFilter>() const filterComp = ref<typeof ColumnFilter>()
const { nestedFilters } = useSmartsheetStoreOrThrow()
// todo: avoid duplicate api call by keeping a filter store // todo: avoid duplicate api call by keeping a filter store
const { filters, loadFilters } = useViewFilters( const { filters, loadFilters } = useViewFilters(
activeView, activeView,
undefined, undefined,
computed(() => false), computed(() => true),
() => false,
nestedFilters.value,
true,
) )
const filtersLength = ref(0) const filtersLength = ref(0)

14
packages/nc-gui-v2/composables/useSmartsheetStore.ts

@ -1,10 +1,16 @@
import { ViewTypes } from 'nocodb-sdk' import { ViewTypes } from 'nocodb-sdk'
import type { TableType, ViewType } from 'nocodb-sdk' import type { FilterType, SortType, TableType, ViewType } from 'nocodb-sdk'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { computed, reactive, useInjectionState, useNuxtApp, useProject, useTemplateRefsList } from '#imports' import { computed, reactive, useInjectionState, useNuxtApp, useProject, useTemplateRefsList } from '#imports'
const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState( const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
(view: Ref<ViewType>, meta: Ref<TableType>, shared = false) => { (
view: Ref<ViewType>,
meta: Ref<TableType>,
shared = false,
initalSorts?: Ref<SortType[]>,
initialFilters?: Ref<FilterType[]>,
) => {
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
const { sqlUi } = useProject() const { sqlUi } = useProject()
@ -37,6 +43,8 @@ const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
} }
return where return where
}) })
const sorts = initalSorts ?? ref<SortType[]>([])
const nestedFilters: Ref<FilterType[]> = initialFilters ?? ref<FilterType[]>([])
return { return {
view, view,
@ -51,6 +59,8 @@ const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
isGallery, isGallery,
cellRefs, cellRefs,
isSharedForm, isSharedForm,
sorts,
nestedFilters,
} }
}, },
'smartsheet-store', 'smartsheet-store',

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

@ -52,7 +52,7 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe
} }
const showAll = async (ignoreIds?: any) => { const showAll = async (ignoreIds?: any) => {
if (isPublic.value || isSharedBase) { if (isPublic.value || isSharedBase.value) {
fields.value = fields.value?.map((field: Field) => ({ fields.value = fields.value?.map((field: Field) => ({
...field, ...field,
show: true, show: true,
@ -75,7 +75,7 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe
reloadData?.() reloadData?.()
} }
const hideAll = async (ignoreIds?: any) => { const hideAll = async (ignoreIds?: any) => {
if (isPublic.value || isSharedBase) { if (isPublic.value || isSharedBase.value) {
fields.value = fields.value?.map((field: Field) => ({ fields.value = fields.value?.map((field: Field) => ({
...field, ...field,
show: false, show: false,
@ -144,7 +144,7 @@ export function useViewColumns(view: Ref<ViewType> | undefined, meta: ComputedRe
}, },
set(v: boolean) { set(v: boolean) {
if (view?.value?.id) { if (view?.value?.id) {
if (!isPublic.value && !isSharedBase) { if (!isPublic.value && !isSharedBase.value) {
$api.dbView $api.dbView
.update(view.value.id, { .update(view.value.id, {
show_system_fields: v, show_system_fields: v,

9
packages/nc-gui-v2/composables/useViewData.ts

@ -39,10 +39,10 @@ export function useViewData(
const formattedData = ref<Row[]>([]) const formattedData = ref<Row[]>([])
const isPublic = inject(IsPublicInj, ref(false)) const isPublic = inject(IsPublicInj, ref(false))
const { project } = useProject() const { project, isSharedBase } = useProject()
const { fetchSharedViewData, paginationData: sharedPaginationData } = useSharedView() const { fetchSharedViewData, paginationData: sharedPaginationData } = useSharedView()
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
const { sorts, nestedFilters: filters } = useSharedView() const { sorts, nestedFilters } = useSmartsheetStoreOrThrow()
const { isUIAllowed } = useUIPermission() const { isUIAllowed } = useUIPermission()
const paginationData = computed({ const paginationData = computed({
@ -83,8 +83,7 @@ export function useViewData(
/** load row comments count */ /** load row comments count */
const loadAggCommentsCount = async () => { const loadAggCommentsCount = async () => {
// todo: handle in public api if (isPublic.value || isSharedBase.value) return
if (isPublic.value) return
const ids = formattedData.value const ids = formattedData.value
?.filter(({ rowMeta: { new: isNew } }) => !isNew) ?.filter(({ rowMeta: { new: isNew } }) => !isNew)
@ -112,7 +111,7 @@ export function useViewData(
? await $api.dbViewRow.list('noco', project.value.id!, meta.value.id!, viewMeta!.value.id, { ? await $api.dbViewRow.list('noco', project.value.id!, meta.value.id!, viewMeta!.value.id, {
...params, ...params,
...(isUIAllowed('sortSync') ? {} : { sortArrJson: JSON.stringify(sorts.value) }), ...(isUIAllowed('sortSync') ? {} : { sortArrJson: JSON.stringify(sorts.value) }),
...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(filters.value) }), ...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(nestedFilters.value) }),
where: where?.value, where: where?.value,
}) })
: await fetchSharedViewData() : await fetchSharedViewData()

164
packages/nc-gui-v2/composables/useViewFilters.ts

@ -1,14 +1,15 @@
import type { ViewType } from 'nocodb-sdk' import type { ViewType } from 'nocodb-sdk'
import type { ComputedRef, Ref } from 'vue' import type { ComputedRef, Ref } from 'vue'
import { message } from 'ant-design-vue'
import { import {
IsPublicInj, IsPublicInj,
ReloadViewDataHookInj, ReloadViewDataHookInj,
computed, computed,
extractSdkResponseErrorMsg,
inject, inject,
ref, ref,
useMetas, useMetas,
useNuxtApp, useNuxtApp,
useSharedView,
useUIPermission, useUIPermission,
watch, watch,
} from '#imports' } from '#imports'
@ -19,13 +20,12 @@ export function useViewFilters(
parentId?: string, parentId?: string,
autoApply?: ComputedRef<boolean>, autoApply?: ComputedRef<boolean>,
reloadData?: () => void, reloadData?: () => void,
siblingFilters?: Filter[], currentFilters?: Filter[],
isNestedRoot?: boolean,
) { ) {
const { nestedFilters } = useSharedView()
const reloadHook = inject(ReloadViewDataHookInj) const reloadHook = inject(ReloadViewDataHookInj)
const _filters = ref<Filter[]>([]) const { nestedFilters } = useSmartsheetStoreOrThrow()
const isPublic = inject(IsPublicInj, ref(false)) const isPublic = inject(IsPublicInj, ref(false))
@ -34,21 +34,23 @@ export function useViewFilters(
const { isUIAllowed } = useUIPermission() const { isUIAllowed } = useUIPermission()
const { metas } = useMetas() const { metas } = useMetas()
const _filters = ref<Filter[]>([])
const nestedMode = computed(() => isPublic.value || !isUIAllowed('filterSync' || !isUIAllowed('filterChildrenRead')))
const filters = computed<Filter[]>({
get: () => (nestedMode.value ? currentFilters! : _filters.value),
set: (value: Filter[]) => {
if (nestedMode.value) {
currentFilters = value
if (isNestedRoot) nestedFilters.value = value
const filters = computed({
get: () => (isPublic.value ? siblingFilters || nestedFilters.value : _filters.value) ?? [],
set: (value) => {
if (isPublic.value) {
if (siblingFilters) {
siblingFilters = value
} else {
nestedFilters.value = value
}
nestedFilters.value = [...nestedFilters.value] nestedFilters.value = [...nestedFilters.value]
reloadHook?.trigger() reloadHook?.trigger()
} else { return
_filters.value = value
} }
_filters.value = value
}, },
}) })
@ -60,53 +62,65 @@ export function useViewFilters(
} }
const loadFilters = async (hookId?: string) => { const loadFilters = async (hookId?: string) => {
if (hookId) { if (nestedMode.value) return
if (parentId) {
filters.value = await $api.dbTableFilter.childrenRead(parentId) try {
} else { if (hookId) {
filters.value = (await $api.dbTableWebhookFilter.read(hookId as string)) as any if (parentId) {
} filters.value = await $api.dbTableFilter.childrenRead(parentId)
} else { } else {
if (parentId) { filters.value = (await $api.dbTableWebhookFilter.read(hookId as string)) as any
filters.value = await $api.dbTableFilter.childrenRead(parentId) }
} else { } else {
filters.value = await $api.dbTableFilter.read(view?.value?.id as string) if (parentId) {
filters.value = await $api.dbTableFilter.childrenRead(parentId)
} else {
filters.value = await $api.dbTableFilter.read(view?.value?.id as string)
}
} }
} catch (e: any) {
console.log(e)
message.error(await extractSdkResponseErrorMsg(e))
} }
} }
const sync = async (hookId?: string, _nested = false) => { const sync = async (hookId?: string, _nested = false) => {
for (const [i, filter] of Object.entries(filters.value)) { try {
if (filter.status === 'delete') { for (const [i, filter] of Object.entries(filters.value)) {
await $api.dbTableFilter.delete(filter.id as string) if (filter.status === 'delete') {
} else if (filter.status === 'update') { await $api.dbTableFilter.delete(filter.id as string)
await $api.dbTableFilter.update(filter.id as string, { } else if (filter.status === 'update') {
...filter, await $api.dbTableFilter.update(filter.id as string, {
fk_parent_id: parentId,
})
} else if (filter.status === 'create') {
if (hookId) {
filters.value[+i] = (await $api.dbTableWebhookFilter.create(hookId, {
...filter, ...filter,
fk_parent_id: parentId, fk_parent_id: parentId,
})) as any })
} else { } else if (filter.status === 'create') {
filters.value[+i] = (await $api.dbTableFilter.create(view?.value?.id as string, { if (hookId) {
...filter, filters.value[+i] = (await $api.dbTableWebhookFilter.create(hookId, {
fk_parent_id: parentId, ...filter,
})) as any fk_parent_id: parentId,
})) as any
} else {
filters.value[+i] = (await $api.dbTableFilter.create(view?.value?.id as string, {
...filter,
fk_parent_id: parentId,
})) as any
}
} }
} }
}
reloadData?.() reloadData?.()
} catch (e: any) {
console.log(e)
message.error(await extractSdkResponseErrorMsg(e))
}
} }
const deleteFilter = async (filter: Filter, i: number) => { const deleteFilter = async (filter: Filter, i: number) => {
// if shared or sync permission not allowed simply remove it from array // if shared or sync permission not allowed simply remove it from array
if (isPublic.value || !isUIAllowed('filterSync')) { if (nestedMode.value) {
filters.value.splice(i, 1) filters.value.splice(i, 1)
filters.value = [...filters.value]
reloadData?.() reloadData?.()
} else { } else {
if (filter.id) { if (filter.id) {
@ -115,11 +129,16 @@ export function useViewFilters(
filter.status = 'delete' filter.status = 'delete'
// if auto-apply enabled invoke delete api and remove from array // if auto-apply enabled invoke delete api and remove from array
} else { } else {
await $api.dbTableFilter.delete(filter.id) try {
await $api.dbTableFilter.delete(filter.id)
reloadData?.() reloadData?.()
filters.value.splice(i, 1) filters.value.splice(i, 1)
} catch (e: any) {
console.log(e)
message.error(await extractSdkResponseErrorMsg(e))
}
} }
// if not synced yet remove it from array // if not synced yet remove it from array
} else { } else {
@ -129,30 +148,29 @@ export function useViewFilters(
} }
const saveOrUpdate = async (filter: Filter, i: number, force = false) => { const saveOrUpdate = async (filter: Filter, i: number, force = false) => {
if (isPublic.value) {
filters.value[i] = { ...filter }
filters.value = [...filters.value]
return
}
if (!view?.value) return if (!view?.value) return
if (!isUIAllowed('filterSync')) { try {
// skip if (nestedMode.value) {
} else if (!autoApply?.value && !force) { filters.value[i] = { ...filter }
filter.status = filter.id ? 'update' : 'create' filters.value = [...filters.value]
} else if (filter.id) { } else if (!autoApply?.value && !force) {
await $api.dbTableFilter.update(filter.id, { filter.status = filter.id ? 'update' : 'create'
...filter, } else if (filter.id) {
fk_parent_id: parentId, await $api.dbTableFilter.update(filter.id, {
}) ...filter,
} else { fk_parent_id: parentId,
// todo: return type of dbTableFilter is void? })
filters.value[i] = (await $api.dbTableFilter.create(view?.value?.id as string, { } else {
...filter, // todo: return type of dbTableFilter is void?
fk_parent_id: parentId, filters.value[i] = (await $api.dbTableFilter.create(view?.value?.id as string, {
})) as any ...filter,
fk_parent_id: parentId,
})) as any
}
} catch (e: any) {
console.log(e)
message.error(await extractSdkResponseErrorMsg(e))
} }
reloadData?.() reloadData?.()
@ -168,7 +186,7 @@ export function useViewFilters(
logical_op: 'and', logical_op: 'and',
} }
if (isPublic.value) placeHolderGroupFilter.children = [child] if (nestedMode.value) placeHolderGroupFilter.children = [child]
filters.value.push(placeHolderGroupFilter) filters.value.push(placeHolderGroupFilter)

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

@ -1,36 +1,23 @@
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 { IsPublicInj, ReloadViewDataHookInj, useNuxtApp } from '#imports' import { message } from 'ant-design-vue'
import { IsPublicInj, ReloadViewDataHookInj, extractSdkResponseErrorMsg, useNuxtApp } from '#imports'
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 { sharedView } = useSharedView()
const { sorts } = useSmartsheetStoreOrThrow()
const { sorts: sharedViewSorts, sharedView } = useSharedView()
const reloadHook = inject(ReloadViewDataHookInj) const reloadHook = inject(ReloadViewDataHookInj)
const isPublic = inject(IsPublicInj, ref(false)) const isPublic = inject(IsPublicInj, ref(false))
const { isSharedBase } = useProject()
const sorts = computed<SortType[]>({
get: () => (isPublic.value || isSharedBase.value ? sharedViewSorts.value : _sorts.value),
set: (value) => {
if (isPublic.value || isSharedBase.value) {
sharedViewSorts.value = value
} else {
_sorts.value = value
}
reloadHook?.trigger()
},
})
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
const { isUIAllowed } = useUIPermission() const { isUIAllowed } = useUIPermission()
const { isSharedBase } = useProject()
const loadSorts = async () => { const loadSorts = async () => {
if (isPublic.value) { if (isPublic.value) {
@ -38,8 +25,14 @@ export function useViewSorts(
sorts.value = [...sharedSorts] sorts.value = [...sharedSorts]
return return
} }
if (!view?.value) return
sorts.value = ((await $api.dbTableSort.list(view?.value?.id as string)) as any)?.sorts?.list try {
if (!view?.value) return
sorts.value = ((await $api.dbTableSort.list(view?.value?.id as string)) as any)?.sorts?.list
} catch (e: any) {
console.error(e)
message.error(await extractSdkResponseErrorMsg(e))
}
} }
const saveOrUpdate = async (sort: SortType, i: number) => { const saveOrUpdate = async (sort: SortType, i: number) => {
@ -49,14 +42,19 @@ export function useViewSorts(
return return
} }
if (isUIAllowed('sortSync')) { try {
if (sort.id) { if (isUIAllowed('sortSync')) {
await $api.dbTableSort.update(sort.id, sort) if (sort.id) {
} else { await $api.dbTableSort.update(sort.id, sort)
sorts.value[i] = (await $api.dbTableSort.create(view?.value?.id as string, sort)) as any } else {
sorts.value[i] = (await $api.dbTableSort.create(view?.value?.id as string, sort)) as any
}
} }
reloadData?.()
} catch (e: any) {
console.error(e)
message.error(await extractSdkResponseErrorMsg(e))
} }
reloadData?.()
} }
const addSort = () => { const addSort = () => {
sorts.value = [ sorts.value = [
@ -68,11 +66,21 @@ export function useViewSorts(
} }
const deleteSort = async (sort: SortType, i: number) => { const deleteSort = async (sort: SortType, i: number) => {
if (isUIAllowed('sortSync') && sort.id && !isPublic.value && !isSharedBase.value) { try {
await $api.dbTableSort.delete(sort.id) if (isUIAllowed('sortSync') && sort.id && !isPublic.value && !isSharedBase.value) {
await $api.dbTableSort.delete(sort.id)
}
sorts.value.splice(i, 1)
sorts.value = [...sorts.value]
} catch (e: any) {
console.error(e)
message.error(await extractSdkResponseErrorMsg(e))
} }
sorts.value.splice(i, 1)
sorts.value = [...sorts.value]
} }
watch(sorts, () => {
reloadHook?.trigger()
})
return { sorts, loadSorts, addSort, deleteSort, saveOrUpdate } return { sorts, loadSorts, addSort, deleteSort, saveOrUpdate }
} }

Loading…
Cancel
Save