Settings Function - april1703/SpotiReels GitHub Wiki
Settings
The settings component manages user preferences, provides functionality for updating the app's theme (dark/light mode), changing the user's password, and logging out and clearing authentication data.
The React component handles both UI and backend logic for user perferences and password management.
Key Features
- Password Management
- Allows users to change their current password.
- Validates password inputs i.e. matching confirmation, non-empty fields
- Sends a
POSTresquest to/changePassword
- Theme Management
- Toggles between dark and light themes.
- Sends a
POSTrequest to/changeLightingModeto sync preference with the backend
- Logout
- Provides a logout button
- Calls
logout()to clear tokens and reset the app state.
- Following Management
- Allows users to see and unfollow the other users they are following.
Default Settings
const defaultSettings = {
theme: "dark",
allowExplicit: true,
autoplayPreview: true,
};
Dependencies
- React-Hooks:
useState,useEffect, anduseMemo - React-Bootstrap - for layout and UI components
- Custom Components:
ThemeSwitch- handles dark/light mode togglingFollowingPopup- manages the user's following list
- Backend API endpoints:
POST /changePasswordPOST /changeLightingModePOST /logout
Functions
readSettings()- Reads and parses stored settings fromlocalStorage.- If no settings exists or parsing fail, it goes back to default.
writeSettings(data) - Writes user settings tolocalStoragelogout()- sends a
POSTrequest to the backend (/logout) - clearing
localStorageand user data - redirects to login page
- sends a
Event Handlers
handleSubmit(event)
- Prevents form default submission
- Validates user input for the password fields.
- Sends
POSTrequest tohtttp://localhost:3001/changePassword - Handles response codes (200, 403, 404, 500)
onChange(event)
- Updates the
formDatavariable as user types in the input fields.
handleThemeChange(event)
- Updates the app theme based on user selection
useEffect
- Updates user's lighting mode in the backend whenever the theme changes.
onSave(event)
- Writes updated settings to
localStorageand provides confirmation feeback.
onReset()
- Restores all settings to their default values.
| Variables | Description |
|---|---|
settings |
Stores the current data for settings |
saved |
Boolean to show save confirmation feedback |
formData |
Holds input data for the password change form |
msg |
Displays messages for password change results |
theme |
Tracks the theme mode (dark/light |
FollowingPopup.jsx
The FollowingPopup component displays a modal-style popup that shows the list of users the current user is following. It allows the user to view, manage, and unfollow accounts directly within the interface. This component retrieves the current user's identity from a cookie-based token and communicates with the backend endpoints to load and update following data.
Key Features
- Following list
- Retrieves the current user's following list from the backend via
POST /getFollowing - Displays the list in a scrollable popup window.
- Unfollow Functionality
- Allows the user to remove accounts they follow.
- Sends a
POST /removeFollowingrequest to update the backend - Updates the frontend list immediately after a successful response
- Token-Based User Identification
- Extracts a JWT
tokenfrom browser cookies - Decodes the token to obtain the logged-in username for authentication backend requests.
Functions
getCookie(name)
Purpose: Retrieve the value of a specific cookie from document.cookie
getCurrentUser()
Purpose: Extracts and decodes the JWT token stored as a cookie named token
Returns: The username encoded in the token payload or null if no token is found.
unfollowServer(followingUsername)
Purpose: Removes a user from the current user's following list both on the frontend and backend.
Render Logic
- When
isOpenisfalse, the component returns null - When
isOpenistrue, it renders:- A popup card containing:
- Header with "Following" title and a close ("x") button.
- Loading or error messages from
status - A scrollable list of usernames with "Unfollow" buttons.
- A popup card containing: