Files
Peter EvansandJeremie Pardou e4b9a2615b feat: add a shared base for integrations that connect an account through OAuth (#6810)
* feat: add a shared base for integrations that connect an account through OAuth

* fix: keep OAuth account scopes current and bound provider answers

* fix: share parent registration between form APIs

* fix: preserve refreshed OAuth tokens when actions fail

* refactor: use absolute imports for parent OAuth modules

* refactor: decouple OAuth cleanup from application publication models

* refactor: use common import hooks for OAuth connections

* fix: keep OAuth account integrations out of the workspace

---------

Co-authored-by: Jeremie Pardou <571533+jrmi@users.noreply.github.com>
2026-11-30 08:55:52 +00:00

344 lines
10 KiB
JavaScript

import { clone } from '@baserow/modules/core/utils/object'
import { formParentKey } from '@baserow/modules/core/composables/useForm'
/**
* This mixin introduces some helper functions for form components where the
* whole component existence is based on being a form.
*/
export default {
emits: ['submitted', 'values-changed'],
props: {
defaultValues: {
type: Object,
required: false,
default: () => {
return {}
},
},
disabled: {
type: Boolean,
required: false,
default: false,
},
// The names of the form fields the host of the form doesn't offer.
hiddenFormFields: {
type: Array,
required: false,
default: () => [],
},
},
provide() {
return {
[formParentKey]: this,
}
},
inject: {
parentForm: {
from: formParentKey,
default: null,
},
},
beforeUnmount() {
if (
this.parentForm &&
typeof this.parentForm.unregisterChildForm === 'function'
) {
this.parentForm.unregisterChildForm(this)
}
},
data() {
return {
// A list of values that the form allows. If null all values are allowed.
allowedValues: null,
// Setting to false make it possible to temporarily
// prevent emitting values when they change.
// Use setEmitValues(value) method to include children
// forms.
emitValues: true,
// Setting to true makes it possible to not
// emit values the first time values are set in
// the form.
skipFirstValuesEmit: false,
// Array to store registered child forms
registeredChildForms: [],
}
},
created() {
for (const [key, value] of Object.entries(this.getDefaultValues())) {
this.values[key] = value
}
for (const name of this.hiddenFormFields) {
delete this.values[name]
}
if (
this.parentForm &&
typeof this.parentForm.registerChildForm === 'function'
) {
this.parentForm.registerChildForm(this)
}
},
watch: {
values: {
handler(newValues) {
if (this.skipFirstValuesEmit) {
this.skipFirstValuesEmit = false
return
}
if (this.emitValues) {
this.emitChange(newValues)
}
},
deep: true,
},
},
methods: {
// Method to register a child form
registerChildForm(childForm) {
if (!this.registeredChildForms.includes(childForm)) {
this.registeredChildForms.push(childForm)
}
},
// Method to unregister a child form
unregisterChildForm(childForm) {
const index = this.registeredChildForms.indexOf(childForm)
if (index !== -1) {
this.registeredChildForms.splice(index, 1)
}
},
/**
* Returns whether a key of the given defaultValue should be handled by this
* form component. This is useful when the defaultValues also contain other
* values which must not be used when submitting. By default this implementation
* is filtered by the list of `allowedValues`.
*/
isAllowedKey(key) {
if (this.allowedValues !== null) {
return this.allowedValues.includes(key)
}
return true
},
/**
* Returns whether the host of the form doesn't offer the form field of the given
* name. A hidden form field isn't shown or validated, and it never is part of
* the values of the form.
*/
isFormFieldHidden(name) {
return this.hiddenFormFields.includes(name)
},
/**
* Returns the given object, keyed by form field name, without the hidden form
* fields. Useful for the validation rules of a form.
*/
omitHiddenFormFields(byName) {
return Object.fromEntries(
Object.entries(byName).filter(([name]) => !this.isFormFieldHidden(name))
)
},
/**
* Returns all the provided default values filtered by the `isAllowedKey` method,
* without the ones of the hidden form fields.
*/
getDefaultValues() {
return Object.keys(this.defaultValues).reduce((result, key) => {
if (this.isAllowedKey(key) && !this.isFormFieldHidden(key)) {
let value = this.defaultValues[key]
// If the value is an array or object, it could be that it contains
// references and we actually need a copy of the value here so that we don't
// directly change existing variables when editing form values.
if (
Array.isArray(value) ||
(typeof value === 'object' && value !== null)
) {
value = clone(value)
}
result[key] = value
}
return result
}, {})
},
/**
* Combined with the FormElement component, this method make sure
* to scroll to the first error field after a fail form submit.
* It is particularly useful for small screen devices or for long
* forms, helping the user to see the error message even if the
* it is outside of the current viewport.
*/
focusOnFirstError() {
const firstError = this.$el.querySelector('[data-form-error]')
if (firstError) {
firstError.scrollIntoView({ behavior: 'smooth' })
}
},
/**
* Select all children that match the given predicate.
* @param {Function} predicate a function that receive the current child as parameter and
* should return true if the child should be accepted.
* @param {Boolean} deep true if you want to deeply search for child
* @returns
*/
getChildForms(predicate = (child) => 'isFormValid' in child, deep = false) {
const children = []
const processChildren = (forms, depth = 0) => {
for (const form of forms) {
if (predicate(form)) {
children.push(form)
}
if (deep && depth < 10 && form.registeredChildForms) {
// Limit depth to avoid infinite recursion
processChildren(form.registeredChildForms, depth + 1)
}
}
}
processChildren(this.registeredChildForms)
return children
},
touch(deep = false) {
if ('v$' in this) {
this.v$.$touch()
}
// Also touch all the child forms so that all the error messages are going to
// be displayed.
for (const child of this.getChildForms(
(child) => 'touch' in child,
deep
)) {
child.touch(deep)
}
},
submit(deep = false) {
if (this.selectedFieldIsDeactivated) {
return
}
this.touch(deep)
if (this.isFormValid(deep)) {
this.$emit('submitted', this.getFormValues(deep))
} else {
this.$nextTick(() => this.focusOnFirstError())
}
},
/**
* Returns true if the field value has no errors
*/
fieldHasErrors(fieldName) {
// a field can be without any validators
return this.v$.values?.[fieldName]?.$error || false
},
/**
* Return the first validaten error message for the given field
* @param {str} fieldName
* @returns the error message or undefined if none.
*/
getFirstErrorMessage(fieldName) {
return this.v$.values?.[fieldName]?.$errors?.[0]?.$message
},
/**
* Returns true is everything is valid.
*
* `deep` parameter allow to deeply search the form elements and not staying at the
* first level of children.
*/
isFormValid(deep = false) {
// Some forms might not do any validation themselves. If they don't, then they
// are by definition valid if their children are valid.
const thisFormInvalid = 'v$' in this && this.v$.$invalid
return !thisFormInvalid && this.areChildFormsValid(deep)
},
/**
* Returns true if all the child form components are valid.
*/
areChildFormsValid(deep = false) {
return this.getChildForms((child) => 'isFormValid' in child, deep).every(
(child) => child.isFormValid()
)
},
/**
* A method that can be overridden to do some mutations on the values before
* calling the submitted event.
*/
getFormValues(deep = false) {
return Object.assign({}, this.values, this.getChildFormsValues(deep))
},
/**
* Returns an object containing the values of the child forms.
*/
getChildFormsValues(deep = false) {
const children = this.getChildForms(
(child) => 'getChildFormsValues' in child,
deep
)
return Object.assign(
{},
...children.map((child) => {
return child.getFormValues(deep)
})
)
},
isDirty() {
for (const [key, value] of Object.entries(this.getDefaultValues())) {
if (this.values[key] !== value) return true
}
return false
},
/**
* Resets the form and the child forms to its original state.
*
* `deep` parameter allow to deeply search the form elements and not staying at the
* first level of children.
*/
async reset(deep = false) {
for (const [key, value] of Object.entries(this.getDefaultValues())) {
this.values[key] = value
}
if ('v$' in this) {
this.v$.$reset()
}
await this.$nextTick()
// Also reset the child forms after a tick to allow default values to be updated.
this.getChildForms((child) => 'reset' in child, deep).forEach((child) =>
child.reset()
)
},
/**
* Sets emitValues property also to child forms.
*/
setEmitValues(value) {
this.emitValues = value
this.getChildForms((child) => 'setEmitValues' in child, true).forEach(
(child) => child.setEmitValues(value)
)
},
/**
* Returns if a child form has indicated it handled the error, false otherwise.
*/
handleErrorByForm(error, deep = false) {
let childHandledIt = false
const children = this.getChildForms(
(child) => 'handleErrorByForm' in child,
deep
)
for (const child of children) {
if (child.handleErrorByForm(error)) {
childHandledIt = true
}
}
return childHandledIt
},
emitChange(newValues) {
this.$emit('values-changed', newValues)
},
},
}