From d8cdc56ef2b9c1403a3c34252d1f37c153bdef50 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Wed, 12 Oct 2022 12:14:40 +0200 Subject: [PATCH] refactor(nc-gui): use `useDialog` to open view create modal --- packages/nc-gui/components/dlg/ViewCreate.vue | 31 ++++--- .../components/smartsheet/sidebar/MenuTop.vue | 36 ++++---- .../components/smartsheet/sidebar/index.vue | 85 ++++++++----------- .../nc-gui/composables/useDialog/index.ts | 22 ++++- packages/nc-gui/composables/useViews.ts | 2 +- packages/nc-gui/context/index.ts | 1 - packages/nc-gui/lang/en.json | 1 + 7 files changed, 89 insertions(+), 89 deletions(-) diff --git a/packages/nc-gui/components/dlg/ViewCreate.vue b/packages/nc-gui/components/dlg/ViewCreate.vue index 6f038f5a35..5cc9b9fa0c 100644 --- a/packages/nc-gui/components/dlg/ViewCreate.vue +++ b/packages/nc-gui/components/dlg/ViewCreate.vue @@ -2,17 +2,16 @@ import type { ComponentPublicInstance } from '@vue/runtime-core' import type { Form as AntForm, SelectProps } from 'ant-design-vue' import { capitalize } from '@vue/runtime-core' -import type { FormType, GalleryType, GridType, KanbanType } from 'nocodb-sdk' +import type { FormType, GalleryType, GridType, KanbanType, TableType, ViewType } from 'nocodb-sdk' import { UITypes, ViewTypes } from 'nocodb-sdk' import { - MetaInj, - ViewListInj, computed, generateUniqueTitle, - inject, message, nextTick, + onBeforeMount, reactive, + ref, unref, useApi, useI18n, @@ -26,6 +25,8 @@ interface Props { title?: string selectedViewId?: string groupingFieldColumnId?: string + viewList: ViewType[] + meta: TableType } interface Emits { @@ -41,7 +42,7 @@ interface Form { fk_grp_col_id: string | null } -const props = defineProps() +const { viewList = [], meta, selectedViewId, ...props } = defineProps() const emits = defineEmits() @@ -55,10 +56,6 @@ const { t } = useI18n() const { isLoading: loading, api } = useApi() -const meta = inject(MetaInj, ref()) - -const viewList = inject(ViewListInj) - const form = reactive
({ title: props.title || '', type: props.type, @@ -75,7 +72,7 @@ const viewNameRules = [ { validator: (_: unknown, v: string) => new Promise((resolve, reject) => { - ;(unref(viewList) || []).every((v1) => ((v1 as GridType | KanbanType | GalleryType).alias || v1.title) !== v) + viewList.every((v1) => ((v1 as GridType | KanbanType | GalleryType).alias || v1.title) !== v) ? resolve(true) : reject(new Error(`View name should be unique`)) }), @@ -98,7 +95,7 @@ const typeAlias = computed( }[props.type]), ) -watch(vModel, (value) => value && init()) +onBeforeMount(init) watch( () => props.type, @@ -108,22 +105,23 @@ watch( ) function init() { - form.title = generateUniqueTitle(capitalize(ViewTypes[props.type].toLowerCase()), viewList?.value || [], 'title') + form.title = generateUniqueTitle(capitalize(ViewTypes[props.type].toLowerCase()), viewList, 'title') - if (props.selectedViewId) { - form.copy_from_id = props.selectedViewId + if (selectedViewId) { + form.copy_from_id = selectedViewId } // preset the grouping field column if (props.type === ViewTypes.KANBAN) { singleSelectFieldOptions.value = meta - .value!.columns!.filter((el) => el.uidt === UITypes.SingleSelect) + .columns!.filter((el) => el.uidt === UITypes.SingleSelect) .map((field) => { return { value: field.id, label: field.title, } }) + if (props.groupingFieldColumnId) { // take from the one from copy view form.fk_grp_col_id = props.groupingFieldColumnId @@ -186,7 +184,8 @@ async function onSubmit() { diff --git a/packages/nc-gui/composables/useDialog/index.ts b/packages/nc-gui/composables/useDialog/index.ts index 1d779e8625..45711931cd 100644 --- a/packages/nc-gui/composables/useDialog/index.ts +++ b/packages/nc-gui/composables/useDialog/index.ts @@ -1,8 +1,20 @@ import type { VNode } from '@vue/runtime-dom' import { isVNode, render } from '@vue/runtime-dom' import type { ComponentPublicInstance } from '@vue/runtime-core' +import type { MaybeRef } from '@vueuse/core' import { isClient } from '@vueuse/core' -import { createEventHook, h, ref, toReactive, tryOnScopeDispose, useNuxtApp, watch } from '#imports' +import { + createEventHook, + getCurrentInstance, + getCurrentScope, + h, + ref, + toReactive, + tryOnScopeDispose, + unref, + useNuxtApp, + watch, +} from '#imports' /** * Programmatically create a component and attach it to the body (or a specific mount target), like a dialog or modal. @@ -38,7 +50,7 @@ import { createEventHook, h, ref, toReactive, tryOnScopeDispose, useNuxtApp, wat export function useDialog( componentOrVNode: any, props: NonNullable[1]> = {}, - mountTarget?: Element | ComponentPublicInstance, + mountTarget?: MaybeRef, ) { if (typeof document === 'undefined' || !isClient) { console.warn('[useDialog]: Cannot use outside of browser!') @@ -53,10 +65,12 @@ export function useDialog( const vNodeRef = ref() - mountTarget = mountTarget ? ('$el' in mountTarget ? (mountTarget.$el as HTMLElement) : mountTarget) : document.body + let _mountTarget = unref(mountTarget) + + _mountTarget = _mountTarget ? ('$el' in _mountTarget ? (_mountTarget.$el as HTMLElement) : _mountTarget) : document.body /** if specified, append vnode to mount target instead of document.body */ - mountTarget.appendChild(domNode) + _mountTarget.appendChild(domNode) /** When props change, we want to re-render the element with the new prop values */ const stop = watch( diff --git a/packages/nc-gui/composables/useViews.ts b/packages/nc-gui/composables/useViews.ts index f04030b1d9..9083b7192c 100644 --- a/packages/nc-gui/composables/useViews.ts +++ b/packages/nc-gui/composables/useViews.ts @@ -18,7 +18,7 @@ export function useViews(meta: MaybeRef) { } } - watch(meta, loadViews, { immediate: true }) + watch(() => meta, loadViews, { immediate: true }) return { views: $$(views), loadViews } } diff --git a/packages/nc-gui/context/index.ts b/packages/nc-gui/context/index.ts index 339c17006e..e56434931f 100644 --- a/packages/nc-gui/context/index.ts +++ b/packages/nc-gui/context/index.ts @@ -27,7 +27,6 @@ export const ReloadViewMetaHookInj: InjectionKey> = Sy export const ReloadRowDataHookInj: InjectionKey> = Symbol('reload-row-data-injection') export const OpenNewRecordFormHookInj: InjectionKey> = Symbol('open-new-record-form-injection') export const FieldsInj: InjectionKey> = Symbol('fields-injection') -export const ViewListInj: InjectionKey> = Symbol('view-list-injection') export const EditModeInj: InjectionKey> = Symbol('edit-mode-injection') export const SharedViewPasswordInj: InjectionKey> = Symbol('shared-view-password-injection') export const CellUrlDisableOverlayInj: InjectionKey> = Symbol('cell-url-disable-url') diff --git a/packages/nc-gui/lang/en.json b/packages/nc-gui/lang/en.json index 42947645d2..f302abc244 100644 --- a/packages/nc-gui/lang/en.json +++ b/packages/nc-gui/lang/en.json @@ -16,6 +16,7 @@ "cancel": "Cancel", "submit": "Submit", "create": "Create", + "duplicate": "Duplicate", "insert": "Insert", "delete": "Delete", "update": "Update",