JavaScript API

Install

  yarn add @resys/dialob-fill-api

Create session

The default export exposes a single newSession() method:

  import DialobFill from '@resys/dialob-fill-api';
  const session = DialobFill.newSession(sessionId, config);

For example, to open a session to demo.dialob.io over REST:

  DialobFill.newSession('e9fe4cdad3119b02aaac0f2ef431a2dc', {
    endpoint: 'https://demo.dialob.io/session/dialob',
    transport: {
      mode: 'rest'
    }
  });

Use the session

  session.pull();
This will pull the session state from the server.

You should set up a listener, which will trigger every time the state updates:

  session.on('update', () => {
    const questionnaire = session.getItem('questionnaire');
    questionnaire.items.forEach(itemId => {
      const item = session.getItem(itemId);
      console.log(item);
    });
  });

The above code will get the questionnaire in the state and log all its items’ data.

Live example

TODO: display some session with a basic vanilla JS impl.

Syncing

Syncing of the session state can happen intermittently. Session always updates the local state immediately, but updates to the back-end might not be sent right away and could be batched together in certain situations - this is why Session is mostly event-based. It also tries to automatically handle network errors and has a progressive back-off functionality, however if no updates can be reliably synced, a sync error will be emitted.

API reference

newSession(sessionId, SessionConfig)

Takes in a string sessionId and SessionConfig and returns a Session.

SessionConfig

Option Description
endpoint

The URL at which requests will be made

transport
optional

TransportConfig, defines how to make requests to the endpoint

TransportConfig

Option Description
mode
optional

rest = requests will be made through REST API

credentials
optional

See RequestCredentials

headers
optional

An object where key is the header name and value is the header value

Session

pull()

Pulls the session state from the back-end. This can be used to initiate or reset the local state.

getItem(id)

Returns the SessionItem with the given id from the state or undefined if no item with such id exists.

getItemErrors(id)

Returns a SessionError array of the item with the given id or undefined if no item with such id exists. If the item has no errors, an empty list is returned.

getValueSet(id)

Returns the SessionValueSet with the given id or undefined if no value set with such id exists.

getLocale()

Returns the locale of the session.

isComplete()

Returns true if session is marked as completed, otherwise returns false.

setAnswer(id, answer)

Updates the answer of the item with the given id. answer should have a type that is acceptable to the back-end, see SessionItem.type.

addRowToGroup(rowGroupId)

Adds a row to a row group with the given rowGroupId. This action depends on the back-end and undefined is always returned from this method - if you need to get the row that was added, use the on('update') event listener.

deleteRow(rowId)

Deletes the row with the given rowId.

complete()

Marks the session as completed.

next()

Updates the session to the next page.

previous()

Updates the session to the previous page.

on(eventType, handler)

handler will be called whenever an event with the given eventType is emitted.

Event types:

  • on('update', () => void) will be triggered whenever session state is updated (this can be local state update as well as an update after a sync)
  • on('sync', (status) => void) will trigger the handler during a sync. status will be either INPROGRESS or DONE.
  • on('error', (type, error) => void) will trigger the handler whenever an error happens. type is either CLIENT or SYNC

removeListener(eventType, handler)

Removes the handler from eventType listeners.

SessionItem

Option Description
id

Unique identifier of the item

type

Indicates the data type of the item. One of:

  • questionnaire
  • group
  • text
  • number
  • boolean
  • decimal
  • date
  • time
  • list
  • multichoice
  • surveygroup
  • survey
  • rowgroup
  • row
  • note
view
optional

Indicates which UI element to show

label
optional

Descriptive label for the item

description
optional

More in-depth description for the item

disabled
optional

true if item is disabled, false otherwise

required
optional

true if item is required, false otherwise

className
optional

A list of strings of the HTML classes that should be applied to the element

value
optional

If item is answerable, the value property stands for the answer value.

items
optional

A list of item ids that are the children of this item

activeItem
optional

Only used on questionnaire item. It holds the id of the item that is currently visible under the questionnaire (effectively the active page).

availableItems
optional

Only used on questionnaire item. Contains item ids that are available.

allowedActions
optional

One of ANSWER, NEXT, PREVIOUS, COMPLETE.

answered
optional

true if all answerable items under this item have been answered, false otherwise

valueSetId
optional

If item has a SessionValueSet tied to it (for example, as is the case with surveys), this holds the id of that value set

props
optional

Custom properties given to the item. Is a simple key-value object.

SessionError

Option Description
id

Id of the item to which the error belongs

code

Unique string which identifies this error

description

Description of the error

SessionValueSet

Option Description
id

Unique identifier of the value set

entries

An array of objects in the format of {key: string, value: string}. Each object is an entry in the value set, where key is the unique identifier for the entry and value is the descriptor for it.