Repository: hashicorp/vault
File: ui/app/components/database-role-setting-form.js (JavaScript)

Code before:
 *
 * @example
 * ```js
 * <DatabaseRoleSettingForm @requiredParam={requiredParam} @optionalParam={optionalParam} @param1={{param1}}/>
 * ```
 * @param {Array<object>} attrs - all available attrs from the model to iterate over
 * @param {object} model - ember data model which should be updated on change
 * @param {string} [roleType] - role type controls which attributes are shown
 * @param {string} [mode=create] - mode of the form (eg. create or edit)
 * @param {object} dbParams - holds database config values, (plugin_name [eg 'mongodb-database-plugin'], skip_static_role_rotation_import)
 */

export default class DatabaseRoleSettingForm extends Component {
  get dbConfig() {
    return this.args.dbParams;
  }

  get settingFields() {
    const dbValues = this.args.dbParams;
    if (!this.args.roleType) return null;
    const dbValidFields = getRoleFields(this.args.roleType);
    return this.args.attrs.filter((a) => {
      // Sets default value for skip_import_rotation based on parent db config value
      if (a.name === 'skip_import_rotation' && this.args.mode === 'create') {
        a.options.defaultValue = dbValues?.skip_static_role_rotation_import;
      }
      return dbValidFields.includes(a.name);
    });
  }

  get statementFields() {
    const type = this.args.roleType;
    if (!type) return null;
    const dbValidFields = getStatementFields(type, this.dbConfig ? this.dbConfig.plugin_name : null);
    return this.args.attrs.filter((a) => {
      return dbValidFields.includes(a.name);
    });
  }

  get isOverridden() {
    if (this.args.mode !== 'create' || !this.dbConfig) return null;

    const dbSkip = this.dbConfig.skip_static_role_rotation_import;
    const staticVal = this.args.model.get('skip_import_rotation');
    return this.args.mode === 'create' && dbSkip !== staticVal;
  }
}


Review comment (suggestion):
A more resilient way to do this would probably be setting these on the actual model (as model values, not in the attr options block) in the constructor. It feels a bit strange to set these as the `defaultValue` option on the `attrs` arg and rely on FormField to update the model, especially in a getter that's intended to just return pertinent statement fields. 🤔 
```js
this.args.model.skip_import_rotation = this.args.dbParams?.skip_static_role_rotation_import

Code after:
 *
 * @example
 * ```js
 * <DatabaseRoleSettingForm @requiredParam={requiredParam} @optionalParam={optionalParam} @param1={{param1}}/>
 * ```
 * @param {Array<object>} attrs - all available attrs from the model to iterate over
 * @param {object} model - ember data model which should be updated on change
 * @param {string} [roleType] - role type controls which attributes are shown
 * @param {string} [mode=create] - mode of the form (eg. create or edit)
 * @param {object} dbParams - holds database config values, { plugin_name: string [eg 'mongodb-database-plugin'], skip_static_role_rotation_import: boolean }
 */

export default class DatabaseRoleSettingForm extends Component {
  get dbConfig() {
    return this.args.dbParams;
  }

  get settingFields() {
    const dbValues = this.args.dbParams;
    if (!this.args.roleType) return null;
    const dbValidFields = getRoleFields(this.args.roleType);
    return this.args.attrs.filter((a) => {
      // Sets default value for skip_import_rotation based on parent db config value
      if (a.name === 'skip_import_rotation' && this.args.mode === 'create') {
        a.options.defaultValue = dbValues?.skip_static_role_rotation_import;
      }
      return dbValidFields.includes(a.name);
    });
  }

  get statementFields() {
    const type = this.args.roleType;
    if (!type) return null;
    const dbValidFields = getStatementFields(type, this.dbConfig ? this.dbConfig.plugin_name : null);
    return this.args.attrs.filter((a) => {
      return dbValidFields.includes(a.name);
    });
  }

  get isOverridden() {
    if (this.args.mode !== 'create' || !this.dbConfig) return null;

    const dbSkip = this.dbConfig.skip_static_role_rotation_import;
    const staticVal = this.args.model.get('skip_import_rotation');
    return this.args.mode === 'create' && dbSkip !== staticVal;
  }
}