-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (currently uses cookies). | ||
- **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. | ||
- `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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?