Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix(nc-gui): enable save changes webhook btn only if unsaved changes present

Ramesh Mane489b6db9a96d
diff --git a/packages/nc-gui/components/webhook/index.vue b/packages/nc-gui/components/webhook/index.vue
index aec3499649..7494317a78 100644
--- a/packages/nc-gui/components/webhook/index.vue
+++ b/packages/nc-gui/components/webhook/index.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { diff } from 'deep-object-diff'
import { type HookReqType, type HookTestReqType, type HookType, PlanLimitTypes, hasInputCalls } from 'nocodb-sdk'
import type { Ref } from 'vue'
import { onKeyDown } from '@vueuse/core'
@@ -33,6 +34,8 @@ const { api, isLoading: loading } = useApi()
const modalVisible = useVModel(props, 'value')
+const { clone } = useUndoRedo()
+
const { hooks } = storeToRefs(useWebhooksStore())
const { base } = storeToRefs(useBase())
@@ -89,6 +92,8 @@ const eventsEnum = computed(() => {
return result
})
+const oldHookRef = ref<HookType | undefined>()
+
let hookRef = reactive<
Omit<HookType, 'notification'> & {
notification: Record<string, any> & {
@@ -133,6 +138,12 @@ let hookRef = reactive<
version: 'v3',
})
+const hasUnsavedChanges = computed(() => {
+ if (!props.hook || !hookRef.id || !oldHookRef.value || !hookRef) return true
+
+ return !ncIsEmptyObject(diff(removeUndefinedFromObj(oldHookRef.value), removeUndefinedFromObj(hookRef)))
+})
+
const operationsEnum = computed(() => {
if (!hookRef.event) {
return [] as {
@@ -500,6 +511,8 @@ function setHook(newHook: HookType) {
hookRef.trigger_field = !!hookRef?.trigger_field
+ oldHookRef.value = clone(hookRef)
+
loadSampleData()
}
@@ -957,7 +970,14 @@ const webhookV2AndV3Diff = computed(() => {
{{ $t('general.cancel') }}
</NcButton>
- <NcButton :loading="loading" type="primary" size="small" data-testid="nc-save-webhook" @click.stop="saveHooks">
+ <NcButton
+ :loading="loading"
+ type="primary"
+ size="small"
+ :disabled="!hasUnsavedChanges"
+ data-testid="nc-save-webhook"
+ @click.stop="saveHooks"
+ >
{{
showUpgradeModal
? $t('general.upgrade')
diff --git a/packages/nc-gui/utils/commonUtils.ts b/packages/nc-gui/utils/commonUtils.ts
index fe3c4423b1..e399836aae 100644
--- a/packages/nc-gui/utils/commonUtils.ts
+++ b/packages/nc-gui/utils/commonUtils.ts
@@ -205,3 +205,51 @@ export const pollUntil = <T>(conditionFn: () => T | null | undefined | false, in
check()
})
}
+
+/**
+ * Removes all `undefined` values and empty objects (`{}`) from an object.
+ * Can optionally run recursively with the `deep` flag.
+ *
+ * - Works only on plain objects (`ncIsObject`).
+ * - Arrays are preserved as-is (including `undefined` entries).
+ * - Empty objects are always removed if they occur as object values.
+ *
+ * @typeParam T - Type of the input value.
+ * @param obj - The object or value to clean.
+ * @param deep - If `true`, cleans recursively. If `false`, cleans only top-level. Defaults to `true`.
+ * @returns A cleaned copy of `obj`.
+ *
+ * @example
+ * ```ts
+ * const data = {
+ * a: 1,
+ * b: undefined,
+ * c: { d: 2, e: undefined, f: { g: undefined } },
+ * arr: [1, undefined, { x: undefined, y: 5 }]
+ * }
+ *
+ * removeUndefinedFromObj(data)
+ * // β†’ { a: 1, c: { d: 2 }, arr: [1, undefined, { y: 5 }] }
+ *
+ * removeUndefinedFromObj(data, false)
+ * // β†’ { a: 1, c: { d: 2, f: { g: undefined } }, arr: [1, undefined, { x: undefined, y: 5 }] }
+ * ```
+ */
+export const removeUndefinedFromObj = <T>(obj: T, deep = true): T => {
+ if (ncIsObject(obj)) {
+ const cleanedEntries = Object.entries(obj)
+ .map(([k, v]) => {
+ const cleanedValue = deep && (ncIsObject(v) || Array.isArray(v)) ? removeUndefinedFromObj(v, deep) : v
+ return [k, cleanedValue] as const
+ })
+ .filter(([_, v]) => {
+ if (v === undefined) return false
+ if (ncIsObject(v) && !Array.isArray(v) && ncIsEmptyObject(v)) return false
+ return true
+ })
+
+ return Object.fromEntries(cleanedEntries) as T
+ }
+
+ return obj
+}