All files / src/display SkillsLevelJS.js

85.36% Statements 35/41
75% Branches 12/16
90.9% Functions 10/11
84.61% Lines 33/39

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                                          3x       4x         4x       5x       5x 5x 5x 4x 4x   5x 2x     3x     3x   3x 3x 3x   3x 3x 3x 3x   3x   3x 3x 3x           3x 7x 3x 3x   3x 3x       6x 6x               4x      
/*
 * Copyright 2025 SkillTree
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import axios from 'axios';
import log from 'js-logger';
 
import SkillsConfiguration from '../config/SkillsConfiguration';
import { SkillsReporter } from '../reporter/SkillsReporter';
 
const emptyArrayIfNull = (value) => (value || []);
 
export default class SkillsLevelJS {
  constructor(projectId) {
    Iif (!SkillsConfiguration.wasConfigureCalled()) {
      const errorMessage = 'SkillsConfiguration.configure must be called before invoking SkillsLevelJS constructor.';
      log.error(`SkillsClient::SkillsLevelJS::${errorMessage}`);
      throw new Error(errorMessage);
    }
    this._projectId = projectId;
  }
 
  attachTo(selectorOrElement) {
    Iif (SkillsConfiguration.isDisabled()) {
      log.info('SkillsLevelJS.js::attachTo: SkillsConfiguration is disabled Level will not be initialized');
      return;
    }
    log.info(`SkillsClient::SkillsLevelJS::attaching to [${selectorOrElement}]`);
    let skillLevelElement = selectorOrElement;
    if (typeof selectorOrElement === 'string') {
      skillLevelElement = document.querySelector(selectorOrElement);
      log.debug(`SkillsClient::SkillsLevelJS::document.querySelector returned [${skillLevelElement}]`);
    }
    if (!skillLevelElement) {
      throw new Error(`Can't find element with selector='${selectorOrElement}'`);
    }
 
    this._skillLevelElement = skillLevelElement;
 
    // load initial skill level
    SkillsConfiguration.afterConfigure()
      .then(() => {
        Eif (!this._projectId) {
          this._projectId = SkillsConfiguration.getProjectId();
          log.debug(`SkillsClient::SkillsLevelJS::getting projectId from SkillsConfiguration: [${this._projectId}]`);
        }
        const requestConfig = { withCredentials: true };
        Eif (!SkillsConfiguration.isPKIMode()) {
          const authToken = SkillsConfiguration.getAuthToken();
          requestConfig.headers = { Authorization: `Bearer ${authToken}` };
        }
        axios.get(`${SkillsConfiguration.getServiceUrl()}/api/projects/${this._projectId}/level`, requestConfig)
          .then((result) => {
            this.setSkillLevel(result.data);
            SkillsReporter.addSuccessHandler(this.update.bind(this));
            log.info(`SkillsClient::SkillsLevelJS::received initial skill level [${result.data}]`);
          });
      });
  }
 
  update(details) {
    const completed = emptyArrayIfNull(details.completed);
    this._skillLevel = completed.filter((message) => message.id === 'OVERALL').reduce((maxLevel, currentLevelUpdateObject) => {
      const levelUpdate = currentLevelUpdateObject.level;
      return maxLevel < levelUpdate ? levelUpdate : maxLevel;
    }, this._skillLevel);
    this.setSkillLevel(this._skillLevel);
    log.info(`SkillsClient::SkillsLevelJS::updated skill level [${this._skillLevel}]`);
  }
 
  setSkillLevel(skillLevel) {
    this._skillLevel = skillLevel;
    this._skillLevelElement.innerText = `Level ${skillLevel}`;
  }
 
  get projectId() {
    return this._projectId;
  }
 
  get skillLevel() {
    return this._skillLevel;
  }
}