getSelectDescription function

dynamic getSelectDescription({
  1. required Map<String, dynamic> currentObj,
  2. required Map<String, dynamic> dbRow,
  3. required Map<String, dynamic> constants,
  4. required Map<String, dynamic> selectFieldsOptionsPromises,
})

Returns the description for a selected value in a CRUD editor

Implementation

dynamic getSelectDescription({
  required Map<String, dynamic> currentObj,
  required Map<String, dynamic> dbRow,
  required Map<String, dynamic> constants,
  required Map<String, dynamic> selectFieldsOptionsPromises,
}) {
  if (gceSelDebug) {
    logDebug("getSelectDescription - currentObj: $currentObj, dbRow: $dbRow");
  }

  String fieldName = currentObj['name'];
  dynamic value = dbRow[fieldName];

  // Related table select (1-1 relationship)
  if (currentObj['type'] == 'select_table') {
    final descAttr = '${fieldName}_description';
    if (dbRow[descAttr] != null) {
      return dbRow[descAttr];
    }
    if (value == null) {
      return null;
    }
    final Map<String, dynamic>? optionsMap =
        selectFieldsOptionsPromises[fieldName]?['promiseResult'];
    return optionsMap?[value.toString()];
  }

  // Generic select (from constants)
  if (currentObj['type'] == 'select') {
    Map<String, dynamic> selectElements = Map<String, dynamic>.from(
      constants[currentObj['select_elements']] ?? {},
    );
    return getSelectOptionLabel(selectElements, value?.toString() ?? '');
  }

  // Component select (with specific data populator result)
  if (currentObj['type'] == 'select_component') {
    Map<String, dynamic>? selectElements =
        selectFieldsOptionsPromises[currentObj['component']]?['promiseResult'];
    if (selectElements != null) {
      return getSelectOptionLabel(selectElements, value?.toString() ?? '');
    }
  }

  // Verify if the attribute (field) exists
  if (value == null) {
    return null;
  }

  // Show specific component (if defined)
  if (currentObj['type'] == 'component' || currentObj['component'] != null) {
    return value;
  }

  // Returns plain value
  return value;
}