Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds README for UPS #302

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/services/user-preferences/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# User Preferences Service

The `UserPreferencesService` class is a TypeScript module designed to manage user preferences within an application. It leverages a composite pattern to handle preferences across different scopes and integrates with browser cookies for persistence.

## Features

- **Initialization of Preferences**: Loads and initializes user preferences from storage.
jared-dickman marked this conversation as resolved.
Show resolved Hide resolved
- **Opt-in Check**: Allows checking if a user has opted into a specific preference.
- **Set Preference**: Enables setting a user's preference, including opt-in/opt-out functionality.
- **Scoped Preferences**: Supports handling preferences within defined scopes to cater to different user segments or application areas.
- **Update Callbacks**: Provides an optional callback mechanism to react to changes in preferences. When preferences are successfully loaded or updated, the `onUpdate` callback is invoked with the resolved preferences as its argument. This allows for reactive updates in the application UI or logic based on the current user preferences.

## Dependencies

This module relies on several custom utilities and services, including:

- `UserPreferences` and `CompositeUserPreferences` for modeling user preferences.
- `UserPreferenceScope` and `UserPreferenceDefinitions` for defining the scope and structure of preferences.
- `CompositeUserPreferencesService` for handling the composite nature of user preferences.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought This concept was difficult to grasp when I first started using the UPS. It took some time to understand what "composite" meant in practice. Maybe some more description here or even an example would help with this?

- `Cookies` utility for getting and setting preferences in browser cookies.

## Usage

To use the `UserPreferencesService`, you need to instantiate it with the necessary dependencies, including preference definitions, a composite service instance, the current scope, and cookie options. After instantiation, call the `init` method to load existing preferences.

### Example

```typescript
import { UserPreferencesService } from './user-preferences.ts'
import { UserPreferenceDefinitions } from 'src/services/user-preferences/models/definitions/user-preference-definitions'
import { CompositeUserPreferencesService } from 'src/services/user-preferences/composite-user-preferences-service'
import { UserPreferenceScope } from 'src/services/user-preferences/models/storage-models/user-preference-scope'
import { CookieOptions } from 'src/utils/Cookies'

// Define your preferences, scope, and cookie options
const definitions = new UserPreferenceDefinitions(/* ... */)
const compositeService = new CompositeUserPreferencesService(/* ... */)
const scope = UserPreferenceScope.Global
const cookieOptions: CookieOptions = { key: 'user_prefs', path: '/', secure: true }

// Instantiate the service
const userPreferencesService = new UserPreferencesService(
definitions,
compositeService,
scope,
cookieOptions,
resolvedPreferences => {
console.log('Preferences updated:', resolvedPreferences)
// Implement your update logic here, e.g., update UI or application state
},
)

// Initialize preferences
userPreferencesService.init().then(() => {
console.log('User preferences initialized.')
})
```

#### Note

This module is designed to be flexible and extensible to accommodate various types of user preferences and application requirements. It is essential to provide the correct dependencies and configurations for it to function correctly.
Loading