Overview
TheContextSwitcher component allows users to switch between viewing their own health data and viewing data from other users who have granted them access. This enables family members, caregivers, and healthcare providers to view patient data in read-only mode.
Namespace: App\Livewire\Dashboard\ContextSwitcher
Properties
This component has no public properties - it reads directly from the session and database.Methods
switchTo()
Switches the viewing context to another user or back to the current user.app/Livewire/Dashboard/ContextSwitcher.php:11-22
The ID of the user to view. Pass
null or the current user’s ID to return to own data.auth()->user()->canView($userId) before allowing the switch. If permission is denied, the context remains unchanged.
Session Storage: The selected user ID is stored in session('viewing_user_id') and persists across page loads.
render()
Renders the context switcher dropdown with available users.app/Livewire/Dashboard/ContextSwitcher.php:24-35
currentUser- The user whose data is currently being viewedaccessibleUsers- Collection of users who have shared their data with the logged-in user
Usage Example
The context switcher is typically rendered in the application header/navbar:- Validates the user has
canView()permission - Stores the selected user ID in the session
- Redirects back to the current page
- All subsequent components read
session('viewing_user_id')to display that user’s data
Read-Only Mode
When viewing another user’s data (session('viewing_user_id') is set and differs from auth()->id()):
- All create/edit/delete operations are disabled
- The floating action button (FAB) is hidden or shows a “Viewer Mode” notice
- Components check
$this->isReadOnlybefore allowing modifications
Dependencies
- User Model - Uses the
accessibleUsersrelationship to get the list of users - Session - Stores the viewing context across requests
- Permission System - Relies on the
user_permissionstable
Related Components
ShareData Component
Grant and revoke viewing permissions
User Model
See canView() method and permission relationships
