eLabSDK2.System.User

Hierarchy

Methods

formatDateTime

Static formatDateTime(datetime, timezone, format?): string

Format a datetime string according to a specific timezone and format.

This utility function formats a datetime string according to the specified timezone and format pattern. It handles timezone conversion and displays the time offset. If no format is provided, it uses a default format. This is particularly useful for displaying timestamps from the server in the user's local timezone.

Parameters

NameTypeDescription
datetimestringThe datetime string to format (ISO 8601 format recommended, e.g., '2024-11-27T14:30:00Z').
timezonestringAn IANA timezone identifier (e.g., 'Europe/Amsterdam', 'America/New_York', 'Asia/Tokyo').
format?stringOptional MomentJS-compatible datetime format string. See docs for format options.

Returns

string

A formatted datetime string including the time offset (e.g., '2024-11-27 15:30:00 +01:00').

Example

// Format a UTC datetime to Amsterdam timezone
const formatted = eLabSDK2.System.User.formatDateTime(
  '2024-11-27T14:30:00Z',
  'Europe/Amsterdam',
  'YYYY-MM-DD HH:mm:ss'
);
console.log(formatted); // Output: '2024-11-27 15:30:00 +01:00'

Example

// Format using user's timezone and format preferences
const userTimezone = eLabSDK2.System.User.getTimeZone();
const userFormat = eLabSDK2.System.User.getDateTimeFormat();
const formatted = eLabSDK2.System.User.formatDateTime(
  serverTimestamp,
  userTimezone,
  userFormat
);

getDateFormat

Static getDateFormat(): string

Get the user's configured date format preference.

This retrieves the date format setting from the user's preferences, which determines how dates are displayed throughout the application. Common formats include 'YYYY-MM-DD', 'DD/MM/YYYY', 'MM/DD/YYYY', etc.

Returns

string

The user's date format string (e.g., 'YYYY-MM-DD', 'DD/MM/YYYY') or undefined if not set.

Example

// Get the current user's date format
const dateFormat = eLabSDK2.System.User.getDateFormat();
console.log(dateFormat); // Output: 'YYYY-MM-DD'

Example

// Use it to format a date with MomentJS
const dateFormat = eLabSDK2.System.User.getDateFormat();
const formatted = moment(new Date()).format(dateFormat);
console.log(formatted); // Output: '2024-11-27'

getDateTimeFormat

Static getDateTimeFormat(): string

Get the user's combined date and time format.

This combines the user's date format and time format preferences into a single format string, useful for displaying complete datetime values. The formats are concatenated with a space separator.

Returns

string

A string containing the combined date and time format (e.g., 'YYYY-MM-DD HH:mm:ss').

Example

// Get the combined datetime format
const datetimeFormat = eLabSDK2.System.User.getDateTimeFormat();
console.log(datetimeFormat); // Output: 'YYYY-MM-DD HH:mm:ss'

Example

// Use it to format a datetime with MomentJS
const datetimeFormat = eLabSDK2.System.User.getDateTimeFormat();
const formatted = moment(new Date()).format(datetimeFormat);
console.log(formatted); // Output: '2024-11-27 14:30:45'

getSetting

Static getSetting(settingsKey): string

Get a specific user setting value by its key.

This retrieves a single user setting value by looking up the setting key. The key comparison is case-insensitive. Common setting keys include 'dateformat', 'timeformat', 'timezone', and other user preferences.

Parameters

NameTypeDescription
settingsKeystringThe setting key to look up (case-insensitive, e.g., 'dateformat', 'timeformat').

Returns

string

The setting value as a string, or undefined if the setting is not found.

Example

// Get a specific setting by key
const dateFormat = eLabSDK2.System.User.getSetting('dateformat');
console.log(dateFormat); // Output: 'YYYY-MM-DD'

Example

// Check if a setting exists before using it
const timezone = eLabSDK2.System.User.getSetting('timezone');
if (timezone) {
  console.log(`User timezone: ${timezone}`);
} else {
  console.log('Timezone not configured');
}

getTimeFormat

Static getTimeFormat(): string

Get the user's configured time format preference.

This retrieves the time format setting from the user's preferences, which determines how times are displayed throughout the application. Common formats include 'HH:mm:ss' (24-hour) and 'hh:mm:ss A' (12-hour with AM/PM).

Returns

string

The user's time format string (e.g., 'HH:mm:ss', 'hh:mm A') or undefined if not set.

Example

// Get the current user's time format
const timeFormat = eLabSDK2.System.User.getTimeFormat();
console.log(timeFormat); // Output: 'HH:mm:ss'

Example

// Use it to format a time with MomentJS
const timeFormat = eLabSDK2.System.User.getTimeFormat();
const formatted = moment(new Date()).format(timeFormat);
console.log(formatted); // Output: '14:30:45'

getTimeZone

Static getTimeZone(): string

Get the user's configured timezone.

This retrieves the timezone setting for the currently logged-in user. Timezones are returned as IANA timezone identifiers (e.g., 'Europe/Amsterdam', 'America/New_York'). This timezone is used throughout the application to display dates and times in the user's local time.

Returns

string

An IANA timezone identifier string (e.g., 'Europe/Amsterdam', 'America/New_York', 'Asia/Tokyo').

Example

// Get the current user's timezone
const timezone = eLabSDK2.System.User.getTimeZone();
console.log(timezone); // Output: 'Europe/Amsterdam'

Example

// Use timezone to convert a UTC time to local time
const timezone = eLabSDK2.System.User.getTimeZone();
const utcTime = '2024-11-27T14:30:00Z';
const localTime = moment.tz(utcTime, timezone).format('YYYY-MM-DD HH:mm:ss');
console.log(localTime); // Output: '2024-11-27 15:30:00' (if timezone is +01:00)

getUserId

Static getUserId(): number

Get the user ID of the currently logged-in user.

This retrieves the unique identifier for the user who is currently authenticated in the application. The user ID is useful for making API calls, filtering data, or performing actions on behalf of the current user.

Returns

number

The numeric user ID of the currently logged-in user.

Example

// Get the current user's ID
const userId = eLabSDK2.System.User.getUserId();
console.log(`Current user ID: ${userId}`); // Output: Current user ID: 12345

Example

// Use the user ID in an API call
const userId = eLabSDK2.System.User.getUserId();
fetch(`/api/users/${userId}/preferences`)
  .then(response => response.json())
  .then(preferences => console.log(preferences));

Example

// Check if current user owns a resource
const userId = eLabSDK2.System.User.getUserId();
const isOwner = sample.createdBy === userId;

getUserSettings

Static getUserSettings(): UserSetting[]

Get all user settings for the currently logged-in user.

This retrieves all user-specific configuration settings stored in the system, including preferences for date format, time format, timezone, and other customizable options. Settings are returned as key-value pairs.

Returns

UserSetting[]

An array of UserSetting objects, each containing a key and value property.

Example

// Get all user settings
const settings = eLabSDK2.System.User.getUserSettings();
settings.forEach(setting => {
  console.log(`${setting.key}: ${setting.value}`);
});

Example

// Find a specific setting
const settings = eLabSDK2.System.User.getUserSettings();
const dateFormat = settings.find(s => s.key === 'dateformat');
console.log(dateFormat?.value); // Output: 'YYYY-MM-DD'

hasPermissions

Static hasPermissions(scope, permissions): boolean

Check if the current user has specific permissions within a given scope.

This verifies whether the currently logged-in user has all of the specified permissions within a particular permission scope (e.g., 'inventory', 'journal', 'admin'). All provided permissions must be present for the function to return true. If the permission scope doesn't exist, an error is logged to the console and the function returns undefined.

Parameters

NameTypeDescription
scopePermissionScopeThe permission scope to check (e.g., 'inventory', 'journal', 'admin', 'samples').
permissionsstring[]An array of permission strings to verify within the scope.

Returns

boolean

True if the user has all specified permissions in the scope, false if any permission is missing, or undefined if the scope doesn't exist.

Example

// Check if user can create samples in inventory
const canCreateSamples = eLabSDK2.System.User.hasPermissions(
  'inventory',
  ['CREATE_SAMPLE']
);
if (canCreateSamples) {
  console.log('User can create samples');
}

Example

// Check multiple permissions for a feature
const canManageExperiments = eLabSDK2.System.User.hasPermissions(
  'journal',
  ['CREATE_EXPERIMENT', 'EDIT_EXPERIMENT', 'DELETE_EXPERIMENT']
);
if (canManageExperiments) {
  // Show full experiment management UI
} else {
  // Show read-only view
}

Example

// Use in conditional rendering
const hasAdminAccess = eLabSDK2.System.User.hasPermissions(
  'admin',
  ['MANAGE_USERS', 'MANAGE_GROUPS']
);
if (hasAdminAccess) {
  // Render admin panel
}

readClientSideSetting

Static readClientSideSetting<T>(settingsKey, defaultValue?): T

Read a setting from the user's local browser storage.

This retrieves a setting stored locally in the browser's localStorage for the current user and group combination. Client-side settings are useful for storing UI preferences that don't need to be synced across devices (e.g., sidebar collapsed state, table column widths, filter preferences). The setting key is automatically namespaced by user ID and group ID to prevent conflicts.

Type parameters

Name
T

Parameters

NameTypeDescription
settingsKeyClientSideSettingsThe client-side setting key to retrieve.
defaultValue?TOptional default value to return if the setting is not found.

Returns

T

The setting value (parsed from JSON), the default value if provided, or undefined if not found and no default provided.

Example

// Read a client-side setting with a default value
const sidebarCollapsed = eLabSDK2.System.User.readClientSideSetting<boolean>(
  'sidebarCollapsed',
  false
);
console.log(sidebarCollapsed); // Output: true or false

Example

// Read a complex object from client-side storage
interface FilterState {
  searchTerm: string;
  selectedTags: string[];
}
const filterState = eLabSDK2.System.User.readClientSideSetting<FilterState>(
  'inventoryFilters'
);
if (filterState) {
  console.log(`Stored search: ${filterState.searchTerm}`);
}

removeClientSideSetting

Static removeClientSideSetting(settingsKey): void

Remove a setting from the user's local browser storage.

This deletes a setting that was previously stored in the browser's localStorage for the current user and group combination. Use this to clean up settings that are no longer needed or to reset a preference to its default state.

Parameters

NameTypeDescription
settingsKeyClientSideSettingsThe client-side setting key to remove.

Returns

void

Example

// Remove a stored preference
eLabSDK2.System.User.removeClientSideSetting('sidebarCollapsed');

Example

// Clear filter state when user clicks 'Reset Filters'
const handleResetFilters = () => {
  eLabSDK2.System.User.removeClientSideSetting('inventoryFilters');
  // Filters will now use default values
};

Example

// Remove all saved state for a feature
eLabSDK2.System.User.removeClientSideSetting('experimentViewSettings');
eLabSDK2.System.User.removeClientSideSetting('experimentTableColumns');

writeClientSideSetting

Static writeClientSideSetting(settingsKey, value): void

Write a setting to the user's local browser storage.

This stores a setting locally in the browser's localStorage for the current user and group combination. The value is automatically serialized to JSON before storage. Client-side settings persist across browser sessions but are specific to the current browser. The setting key is automatically namespaced by user ID and group ID.

Parameters

NameTypeDescription
settingsKeyClientSideSettingsThe client-side setting key to store.
valueunknownThe value to store. Can be any JSON-serializable value (string, number, boolean, object, array).

Returns

void

Example

// Store a simple boolean preference
eLabSDK2.System.User.writeClientSideSetting('sidebarCollapsed', true);

Example

// Store a complex object
const filterState = {
  searchTerm: 'antibody',
  selectedTags: ['reagent', 'active'],
  dateRange: { start: '2024-01-01', end: '2024-12-31' }
};
eLabSDK2.System.User.writeClientSideSetting('inventoryFilters', filterState);

Example

// Store user's last selected tab
eLabSDK2.System.User.writeClientSideSetting('lastActiveTab', 'experiments');

© 2023 eLabNext