1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-09 07:07:49 +00:00
bramw_baserow/web-frontend/modules/database/components/field/FieldSelectThroughFieldSubForm.vue

126 lines
3.6 KiB
Vue

<template>
<div>
<Alert v-if="linkRowFieldsInThisTable.length === 0" minimal type="error">
{{ $t('fieldSelectThroughFieldSubForm.noTable') }}
</Alert>
<div v-if="linkRowFieldsInThisTable.length > 0" class="control">
<label class="control__label control__label--small">
{{ $t('fieldSelectThroughFieldSubForm.selectThroughFieldLabel') }}
</label>
<div class="control__elements">
<FixedItemsDropdown
v-model="values.through_field_id"
:class="{ 'dropdown--error': $v.values.through_field_id.$error }"
@hide="$v.values.through_field_id.$touch()"
@input="throughFieldChanged($event)"
>
<DropdownItem
v-for="field in linkRowFieldsInThisTable"
:key="field.id"
:disabled="field.disabled"
:name="field.name"
:value="field.id"
:icon="field.icon"
></DropdownItem>
</FixedItemsDropdown>
<div v-if="$v.values.through_field_id.$error" class="error">
{{ $t('error.requiredField') }}
</div>
</div>
</div>
</div>
</template>
<script>
import { required } from 'vuelidate/lib/validators'
import form from '@baserow/modules/core/mixins/form'
import { LinkRowFieldType } from '@baserow/modules/database/fieldTypes'
import { DatabaseApplicationType } from '@baserow/modules/database/applicationTypes'
import FixedItemsDropdown from '@baserow/modules/core/components/FixedItemsDropdown'
export default {
name: 'FieldSelectThroughFieldSubForm',
components: { FixedItemsDropdown },
mixins: [form],
props: {
database: {
type: Object,
required: true,
},
fields: {
type: Array,
required: true,
},
},
data() {
return {
allowedValues: ['through_field_id'],
values: {
through_field_id: null,
},
}
},
computed: {
linkRowFieldsInThisTable() {
return this.fields
.filter((f) => f.type === LinkRowFieldType.getType())
.map((f) => {
const fieldType = this.$registry.get('field', f.type)
f.icon = fieldType.getIconClass()
f.disabled = !this.tableIdsAccessible.includes(f.link_row_table_id)
return f
})
},
allTables() {
const databaseType = DatabaseApplicationType.getType()
return this.$store.getters['application/getAll'].reduce(
(tables, application) => {
if (application.type === databaseType) {
const tablesWithCreateFieldAccess = (
application.tables || []
).filter((table) =>
this.$hasPermission(
'database.table.create_field',
table,
this.database.workspace.id
)
)
return tables.concat(tablesWithCreateFieldAccess)
}
return tables
},
[]
)
},
tableIdsAccessible() {
return this.allTables.map((table) => table.id)
},
},
watch: {
'defaultValues.through_field_id'(value) {
this.throughFieldChanged(value)
},
},
created() {
this.throughFieldChanged(this.defaultValues.through_field_id)
},
validations: {
values: {
through_field_id: { required },
},
},
methods: {
isFormValid() {
return (
form.methods.isFormValid.call(this) &&
this.linkRowFieldsInThisTable.length > 0
)
},
throughFieldChanged(fieldId) {
const field = this.linkRowFieldsInThisTable.find((f) => f.id === fieldId)
this.$emit('input', field)
},
},
}
</script>