Skip to content

Commit f3283f2

Browse files
committed
fix: broken UI breaking e2e tests
1 parent b2f7933 commit f3283f2

4 files changed

Lines changed: 47 additions & 38 deletions

File tree

e2e/fixtures/post-login-validation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export async function validateLoggedInState(page: Page): Promise<ValidationResul
6363
expect(userName.length).toBeGreaterThan(0);
6464
console.log(`[validateLoggedInState] User name: ${userName}`);
6565

66-
// 3. Verify "Sticker Collector" text is visible (sidebar user info)
67-
const stickerCollectorText = page.getByText('Sticker Collector');
68-
await expect(stickerCollectorText).toBeVisible({ timeout: 10000 });
66+
// 3. Verify user role text is visible in sidebar (e.g. "user", "admin")
67+
const userRoleText = page.locator('.text-sm.text-gray-600').filter({ hasNotText: '...' });
68+
await expect(userRoleText).toBeVisible({ timeout: 10000 });
6969

7070
// 4. Verify Sign Out is visible (indicates authenticated state)
7171
const signOut = page.getByText('Sign Out');

web-frontend/src/App.jsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { datadogRum } from '@datadog/browser-rum';
77
import "./App.css";
88

99
function App() {
10-
const { isAuthenticated, isLoading, user } = useAuth();
10+
const { isAuthenticated, user } = useAuth();
1111
const navigate = useNavigate();
1212

1313
if (isAuthenticated) {
@@ -18,18 +18,10 @@ function App() {
1818
}
1919

2020
useEffect(() => {
21-
if (!isLoading && isAuthenticated) {
21+
if (isAuthenticated) {
2222
navigate("/dashboard");
2323
}
24-
}, [isAuthenticated, isLoading, navigate]);
25-
26-
if (isLoading) {
27-
return (
28-
<div style={{ textAlign: "center", padding: "50px" }}>
29-
<h2>Loading...</h2>
30-
</div>
31-
);
32-
}
24+
}, [isAuthenticated, navigate]);
3325

3426
return (
3527
<div className="isolate flex flex-auto flex-col bg-[--root-bg]">

web-frontend/src/components/StickerDetail.jsx

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,20 @@ import LocalPrintshopOutlinedIcon from "@mui/icons-material/LocalPrintshopOutlin
44
import HeaderBar from "./HeaderBar";
55
import Sidebar from "./Sidebar";
66
import { API_BASE_URL } from "../config";
7-
import AuthService from "../services/AuthService";
87
import { useAuth } from "../context/AuthContext";
98
import { authFetch } from "../utils/authFetch";
109
import { getLastActiveEvent } from "../services/eventStorage";
1110

1211
function StickerDetail() {
1312
const { id } = useParams();
1413
const navigate = useNavigate();
15-
const { user, isLoading: authLoading } = useAuth();
14+
const { user } = useAuth();
1615
const [sticker, setSticker] = useState(null);
1716
const [loading, setLoading] = useState(true);
1817
const [error, setError] = useState(null);
1918
const [isOwned, setIsOwned] = useState(false);
2019

2120
useEffect(() => {
22-
const userId = user?.sub || user?.email;
23-
24-
// Validate userId before fetching
25-
if (!userId) {
26-
if (!authLoading) {
27-
setError('Unable to identify user. Please log in again.');
28-
setLoading(false);
29-
}
30-
return;
31-
}
32-
3321
const controller = new AbortController();
3422

3523
const fetchSticker = async () => {
@@ -47,20 +35,14 @@ function StickerDetail() {
4735
}
4836

4937
const data = await response.json();
50-
setSticker(data);
51-
52-
// Check if user owns this sticker
53-
const awardsResponse = await authFetch(
54-
`${API_BASE_URL}/api/awards/v1/assignments/${encodeURIComponent(userId)}`
55-
);
56-
if (awardsResponse.ok) {
57-
const awardsData = await awardsResponse.json();
58-
const assignments = awardsData.stickers || [];
59-
setIsOwned(assignments.some((a) => a.stickerId === id));
38+
if (!controller.signal.aborted) {
39+
setSticker(data);
6040
}
6141
} catch (err) {
6242
console.error("Error fetching sticker:", err);
63-
setError(err.message);
43+
if (!controller.signal.aborted) {
44+
setError(err.message);
45+
}
6446
} finally {
6547
if (!controller.signal.aborted) {
6648
setLoading(false);
@@ -69,8 +51,35 @@ function StickerDetail() {
6951
};
7052

7153
fetchSticker();
54+
return () => controller.abort();
7255
}, [id]);
7356

57+
// Check ownership when user context is available
58+
useEffect(() => {
59+
const userId = user?.sub || user?.email;
60+
if (!userId || !sticker) return;
61+
62+
const controller = new AbortController();
63+
64+
const checkOwnership = async () => {
65+
try {
66+
const awardsResponse = await authFetch(
67+
`${API_BASE_URL}/api/awards/v1/assignments/${encodeURIComponent(userId)}`
68+
);
69+
if (awardsResponse.ok && !controller.signal.aborted) {
70+
const awardsData = await awardsResponse.json();
71+
const assignments = awardsData.stickers || [];
72+
setIsOwned(assignments.some((a) => a.stickerId === id));
73+
}
74+
} catch (err) {
75+
console.error("Error checking ownership:", err);
76+
}
77+
};
78+
79+
checkOwnership();
80+
return () => controller.abort();
81+
}, [id, user, sticker]);
82+
7483
const formatDate = (dateString) => {
7584
if (!dateString) return "Unknown";
7685
return new Date(dateString).toLocaleDateString("en-US", {

web-frontend/src/context/AuthContext.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ export const AuthProvider = ({ children }) => {
119119
clearLoginError
120120
}
121121

122+
if (isLoading) {
123+
return (
124+
<div style={{ textAlign: 'center', padding: '50px' }}>
125+
<h2>Loading...</h2>
126+
</div>
127+
)
128+
}
129+
122130
return (
123131
<AuthContext.Provider value={value}>
124132
{children}

0 commit comments

Comments
 (0)