All files / src/mixins sourceable.js

80.65% Statements 25/31
80% Branches 12/15
85.71% Functions 12/14
80.65% Lines 25/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                      3x             3x 3x 1x     2x     1x     1x     2x         1x             1x                     1x                   2x 2x         1x           1x 1x   1x 1x         1x       1x 1x         1x 1x         2x 2x                                
import {
  assign,
  isNil,
  isString,
  merge,
} from 'lodash';
 
import { mapping } from '../utility';
 
export default {
  data() {
    return {
      dataSourceParams: {},
      loadingDataSource: false,
    };
  },
  computed: {
    dataSource() {
      const source = this.config.dataSource;
      if (isString(source)) {
        return this.getBindingValue(source);
      }
 
      return source;
    },
    dataConnector() {
      return this.dataSource.connector;
    },
    isDataSourceRemoteValid() {
      return !isNil(this.dataSource) && !isNil(this.dataConnector);
    },
    isDataSourceLocal() {
      return isNil(this.dataSource) ? true : this.dataSource.local === true;
    },
  },
  methods: {
    getMergedConnector() {
      return assign(
        {},
        this.dataConnector,
        this.options.connectors[this.dataConnector.id],
      );
    },
    getMergedSource(connector) {
      return merge(
        {},
        connector.sources[this.dataSource.id],
        {
          schema: this.dataSource.schema,
          filters: this.dataSource.filters,
          meta: this.dataSource.meta,
        },
      );
    },
    getMergedDataSourceParams() {
      return {
        params: merge(this.dataSourceParams, this.dataSource.params),
        seed: this.registry.isPreviewMode,
        context: {
          registry: this.registry,
          parent: this.$parent,
        },
      };
    },
    loadConnectorData() {
      return new Promise((resolve) => {
        if (
          this.isDataSourceLocal
          || !this.options.connectors
          || !this.isDataSourceRemoteValid
        ) {
          return resolve({
            items: isNil(this.dataSource) ? null : this.mapDataSourceItems(this.dataSource.items),
            pagination: {},
          });
        }
 
        const connector = this.getMergedConnector();
        const source = this.getMergedSource(connector);
 
        this.loadingDataSource = true;
        return this.options.connector.getSourceData(
          connector,
          source,
          this.getMergedDataSourceParams(),
        ).then((result) => {
          this.dataSourceParams = {
            pagination: result.pagination,
          };
 
          Eif (result.items) {
            assign(result, {
              items: this.mapDataSourceItems(result.items),
            });
          }
 
          this.loadingDataSource = false;
          resolve(result);
        });
      });
    },
    mapDataSourceItems(items) {
      const { schema } = this.dataSource;
      return mapping.mapWithSchema(schema, items);
    },
    saveConnectorData(data) {
      const connector = this.getMergedConnector();
      const source = this.getMergedSource(connector);
      const params = this.getMergedDataSourceParams();
      params.payload = data;
 
      return this.options.connector.changeSourceData(
        connector,
        source,
        params,
      ).then(response => response);
    },
  },
};