{"size": 2526, "ext": "js", "max_stars_repo_path": "force-app/main/default/lwc/recordCloneConfigurator/recordCloneConfigurator.js", "max_stars_repo_name": "mhamzas/RecordClone", "max_stars_repo_stars_event_min_datetime": "2020-12-05T02:22:32.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T18:58:35.000Z", "max_issues_repo_path": "force-app/main/default/lwc/recordCloneConfigurator/recordCloneConfigurator.js", "max_issues_repo_name": "mhamzas/RecordClone", "max_issues_repo_issues_event_min_datetime": "2021-05-12T00:10:31.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-17T10:16:00.000Z", "max_forks_repo_path": "force-app/main/default/lwc/recordCloneConfigurator/recordCloneConfigurator.js", "max_forks_repo_name": "mhamzas/RecordClone", "max_forks_repo_forks_event_min_datetime": "2020-12-05T02:15:25.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-11T06:28:58.000Z"}
{"max_stars_count": 8, "max_issues_count": 6, "max_forks_count": 10, "avg_line_length": 26.8723404255, "max_line_length": 111, "alphanum_fraction": 0.6286619161}
/*
 * Copyright (c) 2020, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: BSD-3-Clause
 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
 */
import { LightningElement, api, track, wire } from "lwc";
import showAllChildRelationships from "@salesforce/apex/RecordCloneDebugController.showAllChildRelationships";
import { ShowToastEvent } from "lightning/platformShowToastEvent";

export default class RecordCloneConfigurator extends LightningElement {
  @api
  recordId = "";

  @track
  childrenList = [];

  childrenListStr = "";

  objectName = "";

  @api
  targetSObject = "";

  isInvalidObjectName = false;

  @wire(showAllChildRelationships, {
    recordId: "$recordId",
    objectName: "$targetSObject"
  })
  allChildRelationships({ error, data }) {
    if (!data) {
      if (error) {
        this.handleErrors(error);
        this.isInvalidObjectName = true;
      }
      return;
    }

    const childrenList = [];
    const childrenListStr = [];
    this.objectName = data.objectName;
    for (let key of Object.keys(data.relations)) {
      childrenList.push(key + " : " + data.relations[key]);
      childrenListStr.push(key);
    }
    this.childrenList = childrenList;
    this.childrenListStr = childrenListStr.join(",");
  }

  get hasObjectName() {
    return this.objectName && this.objectName.length > 0;
  }

  buildErrorMessage = (errors) => {
    let errorMessage = "";

    if (Array.isArray(errors)) {
      errors.forEach((err) => {
        errorMessage += `[${err.statusCode}] ${err.message}\n`;
      });
    } else if (errors instanceof Object) {
      for (let key in errors) {
        if (Object.prototype.hasOwnProperty.call(errors, key)) {
          const errs = errors[key];
          for (let err of errs) {
            errorMessage += `${key} - [${err.statusCode}] ${err.message}\n`;
          }
        }
      }
    }
    return errorMessage;
  };

  handleErrors = (errors) => {
    let errorMessage = "Unknown error";
    if (errors && errors.body) {
      errorMessage = "";
      if (errors.body.fieldErrors) {
        errorMessage += this.buildErrorMessage(errors.body.fieldErrors);
      }
      if (errors.body.pageErrors) {
        errorMessage += this.buildErrorMessage(errors.body.pageErrors);
      }
    }
    const errorToast = new ShowToastEvent({
      variant: "error",
      title: "RecordClone Error",
      message: errorMessage
    });
    this.dispatchEvent(errorToast);
  };
}
