{"size": 2055, "ext": "js", "max_stars_repo_path": "src/stores/runtime/credential.js", "max_stars_repo_name": "openpitrix/dashboard", "max_stars_repo_stars_event_min_datetime": "2017-12-28T12:27:36.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-27T13:10:03.000Z", "max_issues_repo_path": "src/stores/runtime/credential.js", "max_issues_repo_name": "openpitrix/dashboard", "max_issues_repo_issues_event_min_datetime": "2018-01-04T09:15:55.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:39:18.000Z", "max_forks_repo_path": "src/stores/runtime/credential.js", "max_forks_repo_name": "openpitrix/dashboard", "max_forks_repo_forks_event_min_datetime": "2017-12-28T09:53:18.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-02T01:23:56.000Z"}
{"max_stars_count": 36, "max_issues_count": 922, "max_forks_count": 28, "avg_line_length": 23.6206896552, "max_line_length": 73, "alphanum_fraction": 0.6194647202}
import { action } from 'mobx';
import _ from 'lodash';

import Store from '../Store';

export default class RuntimeCredentialStore extends Store {
  constructor(...args) {
    super(...args);

    this.defaultStatus = ['active'];

    this.defineObservables(function () {
      this.isLoading = false;

      this.credentials = [];

      // last created resource
      this.credential = {};

      this.runtimeZones = [];

      this.credentialCount = 0;
    });
  }

  get actionName() {
    return this.getUser().isUserPortal
      ? 'runtimes/credentials'
      : 'debug_runtimes/credentials';
  }

  @action
  fetchAll = async params => {
    params = this.normalizeParams(params);

    this.isLoading = true;
    const res = await this.request.get(this.actionName, params);
    this.credentials = _.get(res, 'runtime_credential_set', []);
    this.credentialCount = _.get(res, 'total_count', 0);
    this.isLoading = false;
  };

  @action
  validate = async (params = {}) => {
    this.isLoading = true;
    const res = await this.request.post(
      'runtimes/credentials:validate',
      params
    );
    this.isLoading = false;
    return res;
  };

  @action
  create = async (params = {}) => {
    this.isLoading = true;
    const res = await this.request.post(this.actionName, params);
    if (_.isObject(res) && res.runtime_credential_id) {
      this.credential = { ...res };
    }
    this.isLoading = false;
    return res;
  };

  @action
  modify = async (params = {}) => {
    this.isLoading = true;
    const res = await this.request.patch('runtimes/credentials', params);
    if (_.isObject(res) && res.runtime_credential_id) {
      this.credential = { ...res };
    }
    this.isLoading = false;
    return res;
  };

  @action
  fetchZonesByCredential = async credential_id => {
    this.isLoading = true;
    const res = await this.request.get('runtimes/zones', {
      runtime_credential_id: credential_id
    });
    this.runtimeZones = _.get(res, 'zone', []);
    this.isLoading = false;
    return this.runtimeZones;
  };
}
