Browse Source

chore(gui-v2): update types in `useViewColumns`

pull/2972/head
braks 2 years ago
parent
commit
213c028011
  1. 79
      packages/nc-gui-v2/composables/useViewColumns.ts

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

@ -29,15 +29,18 @@ export function useViewColumns(
let order = 1 let order = 1
if (view?.value?.id) { if (view?.value?.id) {
const data = await $api.dbViewColumn.list(view?.value?.id as string) const data = (await $api.dbViewColumn.list(view?.value?.id)) as any[]
const fieldById: Record<string, any> = data.reduce((o: Record<string, any>, f: any) => {
f.show = !!f.show const fieldById = data.reduce<Record<string, any>>((acc, curr) => {
curr.show = !!curr.show
return { return {
...o, ...acc,
[f.fk_column_id as string]: f, [curr.fk_column_id]: curr,
} }
}, {}) }, {})
fields.value = meta.value?.columns
fields.value = meta.value.columns
?.map((column) => { ?.map((column) => {
const currentColumnField = fieldById[column.id!] || {} const currentColumnField = fieldById[column.id!] || {}
@ -56,32 +59,37 @@ export function useViewColumns(
} }
const showAll = async () => { const showAll = async () => {
await $api.dbView.showAllColumn(view?.value?.id as string) if (view?.value?.id) await $api.dbView.showAllColumn(view.value.id)
await loadViewColumns() await loadViewColumns()
reloadData?.() reloadData?.()
} }
const hideAll = async () => { const hideAll = async () => {
await $api.dbView.hideAllColumn(view?.value?.id as string) if (view?.value?.id) await $api.dbView.hideAllColumn(view.value.id)
await loadViewColumns() await loadViewColumns()
reloadData?.() reloadData?.()
} }
const saveOrUpdate = async (field: any, index: number) => { const saveOrUpdate = async (field: any, index: number) => {
if (field.id) { if (field.id && view?.value?.id) {
await $api.dbViewColumn.update(view?.value?.id as string, field.id, field) await $api.dbViewColumn.update(view.value.id, field.id, field)
} else { } else if (view?.value?.id) {
if (fields.value) fields.value[index] = (await $api.dbViewColumn.create(view?.value?.id as string, field)) as any if (fields.value) fields.value[index] = (await $api.dbViewColumn.create(view.value.id, field)) as any
} }
reloadData?.() reloadData?.()
} }
const metaColumnById = computed(() => { const metaColumnById = computed(() => {
return meta?.value?.columns?.reduce<Record<string, ColumnType>>((o: Record<string, any>, c: any) => { return (
return { meta.value.columns?.reduce<Record<string, ColumnType>>((acc, curr) => {
...o, return {
[c.id]: c, ...acc,
} [curr.id!]: curr,
}, {}) }
}, {}) || {}
)
}) })
const showSystemFields = computed({ const showSystemFields = computed({
@ -90,28 +98,27 @@ export function useViewColumns(
return (view?.value as any)?.show_system_fields || false return (view?.value as any)?.show_system_fields || false
}, },
set(v) { set(v) {
if (view?.value) { if (view?.value?.id) {
$api.dbView.update( $api.dbView.update(view.value.id, {
view?.value?.id as string, // todo: update swagger
{ show_system_fields: v,
// todo: update swagger } as any)
show_system_fields: v,
} as any,
)
;(view.value as any).show_system_fields = v ;(view.value as any).show_system_fields = v
} }
}, },
}) })
const filteredFieldList = computed(() => { const filteredFieldList = computed(() => {
return fields.value?.filter((field) => { return (
// hide system columns if not enabled fields.value?.filter((field) => {
if (!showSystemFields.value && isSystemColumn(metaColumnById?.value?.[field.fk_column_id as string])) { // hide system columns if not enabled
return false if (!showSystemFields.value && isSystemColumn(metaColumnById?.value?.[field.fk_column_id!])) {
} return false
}
return !filterQuery?.value || field.title.toLowerCase().includes(filterQuery.value.toLowerCase()) return !filterQuery?.value || field.title.toLowerCase().includes(filterQuery.value.toLowerCase())
}) }) || {}
)
}) })
const sortedAndFilteredFields = computed<ColumnType[]>(() => { const sortedAndFilteredFields = computed<ColumnType[]>(() => {
@ -121,15 +128,15 @@ export function useViewColumns(
if ( if (
!showSystemFields.value && !showSystemFields.value &&
metaColumnById.value && metaColumnById.value &&
metaColumnById?.value?.[c.fk_column_id as string] && metaColumnById?.value?.[c.fk_column_id!] &&
isSystemColumn(metaColumnById?.value?.[c.fk_column_id as string]) isSystemColumn(metaColumnById.value?.[c.fk_column_id!])
) { ) {
return false return false
} }
return c.show return c.show
}) })
?.sort((a, b) => a.order - b.order) ?.sort((a, b) => a.order - b.order)
?.map((c) => metaColumnById?.value?.[c.fk_column_id as string]) || []) as ColumnType[] ?.map((c) => metaColumnById?.value?.[c.fk_column_id!]) || []) as ColumnType[]
}) })
// reload view columns when table meta changes // reload view columns when table meta changes

Loading…
Cancel
Save