Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 17 additions & 31 deletions src/components/AuthMiddleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { AppDispatch, RootState } from '../redux/store';
import { getUserData } from '../redux/thunks/userThunks';
import { logout } from '../redux/slices/userSlice';

// Simple debounce utility
const useDebounce = (callback: Function, delay: number) => {
Expand All @@ -28,7 +29,7 @@ const AuthMiddleware: React.FC<AuthMiddlewareProps> = ({ children }) => {
const { token, role, loading, error } = user;
const [hasAttempted, setHasAttempted] = useState(false);
const attemptCountRef = useRef(0);
const MAX_ATTEMPTS = 3;
const MAX_ATTEMPTS = 1;

// Debug log whenever user state changes
useEffect(() => {
Expand All @@ -47,11 +48,24 @@ const AuthMiddleware: React.FC<AuthMiddlewareProps> = ({ children }) => {
attemptCountRef.current += 1;
const response = await dispatch(getUserData());
console.log('Fetch response:', response);
const role = useSelector((state: RootState) => state.user.role);
console.log('Fetched role:', role);
// Don't use hooks here - removed the invalid hook call
}
}, 1000);

// Effect to check for max attempts and clear token if needed
useEffect(() => {
if (
attemptCountRef.current >= MAX_ATTEMPTS &&
error &&
token &&
error.includes("404")
) {
console.log('Maximum attempts reached with 404 errors. Clearing invalid token.');
dispatch(logout());
// No need for window.location.reload() as the state change will trigger a re-render
}
}, [attemptCountRef.current, error, token, dispatch]);

useEffect(() => {
// If we have a token but no role and haven't tried fetching yet
if (token && !role && !loading && !hasAttempted) {
Expand All @@ -74,34 +88,6 @@ const AuthMiddleware: React.FC<AuthMiddlewareProps> = ({ children }) => {
}
}, [token, role, loading, hasAttempted, debouncedFetchUserData]);

// For extreme debugging, force-update the role from localStorage if available
useEffect(() => {
// This is a workaround if the reducer isn't working properly
const tryRestoreRoleFromLocalStorage = () => {
try {
// Only do this if we've exhausted our API attempts
if (attemptCountRef.current >= MAX_ATTEMPTS && token && !role) {
const savedUserData = localStorage.getItem('userData');
if (savedUserData) {
const userData = JSON.parse(savedUserData);
if (userData.role) {
console.log('Restoring role from localStorage:', userData.role);
// You would need to add a setRole action to your userSlice
// dispatch(setRole(userData.role));
window.location.reload(); // Extreme measure, reload the page
}
}
}
} catch (e) {
console.error('Error restoring role from localStorage:', e);
}
};

if (attemptCountRef.current >= MAX_ATTEMPTS) {
tryRestoreRoleFromLocalStorage();
}
}, [attemptCountRef.current, token, role, dispatch]);

return <>{children}</>;
};

Expand Down

This file was deleted.

Empty file.
237 changes: 237 additions & 0 deletions src/feature/AccountSettings/AccountSettingsModal.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
.modalOverlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
backdrop-filter: blur(4px);
}

.modalContent {
background: white;
border-radius: 16px;
width: 90%;
max-width: 450px;
max-height: 90vh;
overflow-y: auto;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
animation: slideIn 0.3s ease;
}

@keyframes slideIn {
from {
transform: translateY(20px);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}

.modalHeader {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 24px;
border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.modalHeader h2 {
margin: 0;
font-size: 20px;
font-weight: 700;
color: #111827;
}

.closeButton {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #6b7280;
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
transition: all 0.2s ease;
}

.closeButton:hover {
background-color: #f3f4f6;
color: #111827;
}

.modalBody {
padding: 24px;
}

.loadingSpinner {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 30px 0;
}

.spinner {
border: 4px solid rgba(0, 0, 0, 0.05);
border-radius: 50%;
border-top: 4px solid #b0f484;
width: 36px;
height: 36px;
animation: spin 1s linear infinite;
margin-bottom: 12px;
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}

.profileSection {
display: flex;
align-items: center;
margin-bottom: 24px;
}

.avatarContainer {
width: 64px;
height: 64px;
border-radius: 50%;
background: linear-gradient(135deg, #50703C 0%, #7fa25c 100%);
display: flex;
justify-content: center;
align-items: center;
margin-right: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.avatarInitials {
color: white;
font-size: 24px;
font-weight: 600;
}

.profileInfo {
flex: 1;
}

.userName {
margin: 0;
font-size: 20px;
font-weight: 600;
color: #111827;
}

.infoSection {
background-color: #f9fafb;
border-radius: 12px;
padding: 16px;
margin-bottom: 24px;
}

.infoItem {
margin-bottom: 16px;
}

.infoItem:last-child {
margin-bottom: 0;
}

.infoLabel {
font-size: 12px;
color: #6b7280;
margin-bottom: 4px;
}

.infoValue {
font-size: 16px;
color: #111827;
font-weight: 500;
}

.inputField {
width: 100%;
padding: 10px 12px;
border: 1px solid #e5e7eb;
border-radius: 8px;
font-size: 16px;
transition: all 0.2s;
background-color: white;
}

.inputField:focus {
border-color: #b0f484;
outline: none;
box-shadow: 0 0 0 3px rgba(176, 244, 132, 0.15);
}

.actionsSection {
display: flex;
flex-direction: column;
gap: 12px;
}

.actionButton {
padding: 12px 16px;
border-radius: 8px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
border: none;
background-color: #f3f4f6;
color: #111827;
transition: all 0.2s ease;
text-align: center;
}

.actionButton:hover {
background-color: #e5e7eb;
transform: translateY(-1px);
}

.actionButton:active {
transform: translateY(0);
}

.saveButton {
background-color: #b0f484;
color: #111827;
}

.saveButton:hover {
background-color: #86efac;
}

.logoutButton {
background-color: #fee2e2;
color: #b91c1c;
}

.logoutButton:hover {
background-color: #fecaca;
}

@media (max-width: 640px) {
.modalContent {
width: 95%;
border-radius: 16px;
max-height: 80vh;
}

.modalHeader {
padding: 16px 20px;
}

.modalBody {
padding: 20px;
}
}
Loading
Loading