0.2.2 • Published 11 months ago

@blocdigital/usescorm v0.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

usescorm

React hook for communicating with the SCORM API.

I would recommend reading the the SCORM documentation to familiarise yourself with the syntax.

Install

npm install --save @blocdigital/usescorm

Usage

import { useState, useEffect } from 'react';

import useScorm from '@blocdigital/usescorm';

const Example = () => {
  // initiate the scorm session
  const scorm = useScorm({ version: '1.2' });

  const [state, setState] = useState(scorm?.storage || null);

  /**
   * toggle the pass status of a module
   *
   * @param {string} module which module to update
   * @param {number} item index of item
   */
  const handleToggle = (module, item) => {
    scorm.store((state) => {
      const newObj = structuredClone(state);

      newObj[module][item] = newObj[module][item] > 0 ? -1 : 1;

      return newObj;
    });
  };

  // set up listeners to keep state in sync with suspend data
  useEffect(() => {
    const onStateChange = (data) => setState(data);

    scorm.on('storage', onStateChange);

    // remember to tidy up your event listeners
    return () => scorm.off('storage', onStateChange);
  }, [scorm]);

  // initialise the suspend data
  useEffect(() => {
    scorm.store((state) => ({ module1: [0, 0, 0], module2: [0, 0, 0], ...state }));
  }, [scorm]);

  return (
    <>
      <h2>Module 1</h2>
      {state &&
        state.module1.map((pass, index) => (
          <p key={index}>
            Module 1, item {index + 1} is {pass === -1 ? 'failed' : pass === 1 ? 'passed' : 'not started'}
            <button onClick={() => handleToggle('module1', index)}>toggle</button>
          </p>
        ))}
      <h2>Module 2</h2>
      {state &&
        state.module2.map((pass, index) => (
          <p key={index}>
            Module 2, item {index + 1} is {pass === -1 ? 'failed' : pass === 1 ? 'passed' : 'not started'}
            <button onClick={() => handleToggle('module2', index)}>toggle</button>
          </p>
        ))}
    </>
  );
};

Properties

NameTypeDefaultDescription
versionstring'auto'Version of scorm used.
debugbooleanfalseWhether or not debug mode is enabled.
handleCompletionStatusbooleantrueWhether or not the wrapper should automatically handle the initial completion status.
handleExitModebooleantrueWhether or not the wrapper should automatically handle the exit mode.
autoCommitbooleantrueWhether or not the each action automatically saves.

API

NameTypeDescription
versionstringVersion of scorm used
handleCompletionStatusbooleanWhether or not the wrapper should automatically handle the initial completion status.
handleExitModebooleanWhether or not the wrapper should automatically handle the exit mode.
autoCommitbooleanWhether or not the each action automatically saves.
storageunknownLive version of data that is in suspend_data.
API.handlenull | IScorm1_2 | IScorm2004Stored version of SCORM API.
API.isFoundbooleanWhether the SCORM API has been detected or not.
API.find(win: Window) => null | IScorm1_2 | IScorm2004Find a copy of SCORM.
API.get() => null | IScorm1_2 | IScorm2004Find a copy of SCORM (using current window).
API.getHandle() => null | IScorm1_2 | IScorm2004Find a copy of SCORM (using current window + update handle).
connection.isActivebooleanWhether the connection to the SCORM API is active.
data.completionStatuslesson_statusCurrent Course completion status.
data.exitStatusbooleanCourse Exit status.
debug.isActivebooleanWhether or not debug mode is enabled.
debug.getCode() => numberReturns the error code that resulted from the last API call.
debug.getInfo(error: number) => string | undefinedReturns a short string describing the specified error code.
debug.getDiagnosticInfo(error: number) => string | undefinedReturns detailed information about the last error that occurred.
init() => booleanBegins a communication session with the LMS.
terminate() => booleanEnds a communication session with the LMS.
get(parameter: CMIElement) => stringRetrieves a value from the LMS.
set(parameter: CMIElement, value: string) => booleanSaves a value to the LMS
store(value: unknown | ((value: unknown) => unknown)) => booleanStore data in suspense data
save() => booleanSaves a value to the LMS.
status(action: 'get' | 'set', status?: lesson_status) => boolean | lesson_statusUpdate the lesson status.
on(event: 'init' | 'set' | 'storage', callback: (data: unknown) => void) => voidAdd event listener for when this component is used.
onAny(callback: (data: unknown) => void) => voidAdd event listener, for all events, for when this component is used.
off(event: 'init' | 'set' | 'storage', callback: (data: unknown) => void) => voidIf you exactly match an on event you can remove it.
offAny(callback: (data: unknown) => void) => voidIf you exactly match an onAny function you can remove it.
0.2.1

11 months ago

0.2.0

12 months ago

0.2.2

11 months ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago