import * as R from 'ramda'; import { createSelector, ParametricSelector, Selector } from 'reselect'; import * as tablesListSelectors from './tablesListSelectors'; import * as tableSelectors from './tableSelectors'; import { FIELD_TYPE, FIELD_KINDS } from '../constants'; import { FieldSchema, TableSchema, FieldKind } from '../types'; export const getTableField = (tableField: FieldSchema | void): FieldSchema => tableField as any; export const getFieldType: any = createSelector( getTableField, R.propOr('', 'fieldType'), ); export const getFieldTypesAttributes = createSelector( getTableField, R.propOr(null, 'fieldTypeAttributes'), ); export const isRelationField = createSelector( getFieldType, R.equals(FIELD_TYPE.RELATION), ); export const isMissingRelationField = createSelector( getFieldType, R.equals(FIELD_TYPE.MISSING_RELATION), ); export const isOneWayRelationField = createSelector( getFieldType, R.equals(FIELD_TYPE.ONE_WAY_RELATION), ); export const isFileField = createSelector( getFieldType, R.equals(FIELD_TYPE.FILE), ); export const isSmartField = createSelector( getFieldType, R.equals(FIELD_TYPE.SMART), ); export const isIdField = createSelector( getFieldType, R.equals(FIELD_TYPE.ID), ); export const isGeoField = createSelector( getFieldType, R.equals(FIELD_TYPE.GEO), ); export const isMetaField = createSelector( getTableField, R.propEq('isMeta', true), ); export const isListField = createSelector( getTableField, R.propEq('isList', true), ); export const isSystemField = createSelector( getTableField, R.propEq('isSystem', true), ); export const getFieldId = createSelector( getTableField, R.propOr('', 'id'), ); export const getFieldName = createSelector( getTableField, fieldSchema => (fieldSchema ? fieldSchema.name : ''), ); export const getFieldDisplayName = createSelector( getTableField, R.propOr('', 'displayName'), ); export const getTableId = createSelector( getTableField, R.path(['table', 'id']), ); export const getTableName = createSelector( getTableField, R.path(['table', 'name']), ); export const getTableDisplayName = createSelector( getTableField, R.path(['table', 'displayName']), ); export const getRelationTableId: ParametricSelector = createSelector( getTableField, R.path(['relation', 'refTable', 'id']), ); export const getRelationTableName: ParametricSelector = createSelector( getTableField, R.path(['relation', 'refTable', 'name']), ); export const getRelationTableDisplayName: ParametricSelector = createSelector( getTableField, R.path(['relation', 'refTable', 'displayName']), ); export const getSchemaFeatures = createSelector( getTableField, R.prop('schemaFeatures'), ); export const getDataFeatures = createSelector( getTableField, R.prop('dataFeatures'), ); export const getFieldKind = createSelector( getTableId, isSystemField, getRelationTableId, (_, tablesSchema: TableSchema[]): TableSchema[] => tablesSchema, (tableId: string, isSystem: boolean, relationTableId: string, tablesSchema: TableSchema[]) => { let kind = FIELD_KINDS.USER; const table = tablesListSelectors.getTableById(tablesSchema, tableId); if (isSystem) { kind = FIELD_KINDS.SYSTEM; } else if (tableSelectors.isIntegrationTable(table)) { kind = FIELD_KINDS.EXTERNAL; } else if (relationTableId) { const refTable = tablesListSelectors.getTableById(tablesSchema, relationTableId); if (tableSelectors.isSystemTable(refTable)) { kind = FIELD_KINDS.SYSTEM; } else if (tableSelectors.isIntegrationTable(refTable)) { kind = FIELD_KINDS.EXTERNAL; } } return kind; }, );