mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.9 KiB
73 lines
1.9 KiB
import type { TableType, ViewType } from 'nocodb-sdk' |
|
import { ViewTypes } from 'nocodb-sdk' |
|
import type { Ref } from 'vue' |
|
import { useToast } from 'vue-toastification' |
|
import { useNuxtApp } from '#app' |
|
// import useMetas from '~/composables/useMetas' |
|
|
|
export default (meta: Ref<TableType>, onViewCreate?: (viewMeta: any) => void) => { |
|
const view = reactive<{ title: string; type?: ViewTypes }>({ |
|
title: '', |
|
}) |
|
|
|
const loading = ref(false) |
|
|
|
const { $api } = useNuxtApp() |
|
const toast = useToast() |
|
// unused |
|
// const { metas } = useMetas() |
|
|
|
const createView = async (viewType: ViewTypes, selectedViewId = null) => { |
|
loading.value = true |
|
|
|
try { |
|
let data |
|
switch (viewType) { |
|
case ViewTypes.GRID: |
|
// todo: update swagger |
|
data = await $api.dbView.gridCreate( |
|
meta?.value?.id as string, |
|
{ |
|
title: view?.title, |
|
copy_from_id: selectedViewId, |
|
} as any, |
|
) |
|
break |
|
case ViewTypes.GALLERY: |
|
data = await $api.dbView.galleryCreate( |
|
meta?.value?.id as string, |
|
{ |
|
title: view?.title, |
|
copy_from_id: selectedViewId, |
|
} as any, |
|
) |
|
break |
|
case ViewTypes.FORM: |
|
data = await $api.dbView.formCreate( |
|
meta?.value?.id as string, |
|
{ |
|
title: view?.title, |
|
copy_from_id: selectedViewId, |
|
} as any, |
|
) |
|
break |
|
} |
|
toast.success('View created successfully') |
|
onViewCreate?.(data) |
|
} catch (e: any) { |
|
toast.error(e.message) |
|
} |
|
|
|
loading.value = false |
|
} |
|
|
|
const generateUniqueTitle = (views: ViewType[]) => { |
|
let c = 1 |
|
while (views?.some((t) => t.title === `${meta?.value?.title}${c}`)) { |
|
c++ |
|
} |
|
view.title = `${meta?.value?.title}${c}` |
|
} |
|
|
|
return { view, createView, generateUniqueTitle, loading } |
|
}
|
|
|