Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | 4x 28x 28x 28x 45x 45x 28x 28x 28x 28x 15x 13x 3x 10x 28x 28x 28x 28x 28x 28x 28x 9x 9x 1x 1x 8x 8x 8x 8x 5x 5x 5x 3x 3x 3x 2x 1x 3x 3x 3x 28x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 22x 22x 1x 1x 189x 132x 9x | /**
* Modified by Allan Ninal — 2026-08-12: dropped an unused `useEffect` import
* (theme fetching moved to the page level and left it behind).
*/
import React, { useState } from "react";
import { useThemeToggle } from "@/theme/ThemeContext";
import {
Fade,
IconButton,
Stack,
Typography,
Snackbar,
Alert,
CircularProgress,
Paper,
Box,
Tooltip,
Zoom,
Divider,
Button,
Dialog,
DialogTitle,
DialogContent,
DialogContentText,
DialogActions,
} from "@mui/material";
import { alpha } from "@mui/material/styles";
import CheckIcon from "@mui/icons-material/Check";
import LightModeOutlinedIcon from "@mui/icons-material/LightModeOutlined";
import DarkModeOutlinedIcon from "@mui/icons-material/DarkModeOutlined";
import PaletteIcon from "@mui/icons-material/Palette";
import RestartAltIcon from "@mui/icons-material/RestartAlt";
import { colorPresets } from "@/theme/ThemePresets";
import { useTheme } from "@mui/material/styles";
import { getPortalRole } from "@/authentication/auth0";
import {
_fetchTeacherTheme,
_applyTeacherTheme,
_updateTeacherTheme,
_deleteTeacherTheme,
} from "@/store/slices/teacher/TeacherThemes";
import { _fetchStudentTheme, _applyStudentTheme, _updateStudentTheme } from "@/store/slices/student/StudentThemes";
import { useDispatch } from "react-redux";
import { useSelector } from "react-redux";
import { enqueueSnackbar } from "notistack";
import { themeIDs } from "@/theme/CustomColors";
const ThemeSets = ({ showTheme = true }) => {
const theme = useTheme();
const dispatch = useDispatch();
const { themeMode, toggleThemeMode, themePreset, changeThemePreset } = useThemeToggle();
const teacherThemeState = useSelector((state) => state?.TeacherThemes || null);
const studentThemeState = useSelector((state) => state?.StudentThemes || null);
const portalRole = getPortalRole();
const fallbackThemeState = {
selectedTheme: null,
colorMode: null,
isLoading: false,
isApplyingTheme: false,
isUpdatingTheme: false,
isDeletingTheme: false,
};
let themeState = {};
if (portalRole === "teacher") {
themeState = teacherThemeState || fallbackThemeState;
} else if (portalRole === "student") {
themeState = studentThemeState || fallbackThemeState;
} else {
themeState = fallbackThemeState;
}
const {
selectedTheme,
colorMode,
isLoading,
isApplyingTheme,
isUpdatingTheme,
isDeletingTheme,
hasErrors,
applyThemeError,
updateThemeError,
} = themeState;
const [loadingTheme, setLoadingTheme] = useState(null);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const openDeleteDialog = () => setDeleteDialogOpen(true);
const closeDeleteDialog = () => setDeleteDialogOpen(false);
const handleResetTheme = async () => {
if (portalRole !== "teacher") {
closeDeleteDialog();
return;
}
try {
await dispatch(_deleteTeacherTheme()).unwrap();
changeThemePreset("default");
enqueueSnackbar("Theme reverted to default", { variant: "success" });
} catch (error) {
console.error("Error deleting theme:", error);
enqueueSnackbar("Error reverting theme", { variant: "error" });
} finally {
closeDeleteDialog();
}
};
// Note: Theme fetching is now handled at the main page level (TeacherMainPage.jsx)
// to ensure themes are loaded immediately after login
const handleThemeChange = async (newPreset) => {
const role = getPortalRole();
// Check if theme is already selected
if (themePreset === newPreset) {
console.log("Theme already selected:", newPreset);
return;
}
setLoadingTheme(newPreset);
const themeId = themeIDs[newPreset];
Iif (!themeId) {
console.error("Invalid theme preset:", newPreset);
enqueueSnackbar("Invalid theme preset", { variant: "error" });
setLoadingTheme(null);
return;
}
if (role !== "teacher" && role !== "student") {
enqueueSnackbar("Unable to save theme. Please sign in again.", { variant: "error" });
setLoadingTheme(null);
return;
}
// Prepare theme data with both theme_id and current color_mode
const themeData = {
theme_id: themeId,
color_mode: themeMode, // Get current mode from ThemeContext
};
try {
if (role === "teacher") {
await dispatch(_applyTeacherTheme(themeData)).unwrap();
} else {
await dispatch(_applyStudentTheme(themeData)).unwrap();
}
// Note: Not saving to localStorage per requirements - using Redux state only
changeThemePreset(newPreset);
enqueueSnackbar("Theme updated successfully", { variant: "success" });
} catch (error) {
console.error("Error applying theme:", error);
enqueueSnackbar("Error updating theme", { variant: "error" });
} finally {
setLoadingTheme(null);
}
};
const handleModeChange = async (newMode) => {
const role = getPortalRole();
// Check if mode is already selected
if (theme.palette.mode === newMode) {
console.log("Mode already selected:", newMode);
return;
}
try {
// Update local theme mode first
Iif (newMode === "light" && theme.palette.mode !== "light") {
toggleThemeMode();
E} else if (newMode === "dark" && theme.palette.mode === "light") {
toggleThemeMode();
}
// Also save the mode change to the database
const currentThemeId = themeIDs[themePreset];
Eif (currentThemeId) {
const themeData = {
theme_id: currentThemeId,
color_mode: newMode,
};
if (role === "teacher") {
await dispatch(_applyTeacherTheme(themeData)).unwrap();
E} else if (role === "student") {
await dispatch(_applyStudentTheme(themeData)).unwrap();
}
}
enqueueSnackbar("Theme updated successfully", { variant: "success" });
} catch (error) {
console.error("Error changing theme mode:", error);
enqueueSnackbar("Error changing theme mode", { variant: "error" });
}
};
return (
<React.Fragment>
{showTheme && (
<Box sx={{ maxWidth: 820, mx: "auto", mt: 4, position: "relative" }}>
<Paper
elevation={0}
sx={{
p: { xs: 2.5, sm: 4 },
borderRadius: "20px",
border: (t) => `1px solid ${alpha(t.palette.grey[500], 0.14)}`,
position: "relative",
overflow: "hidden",
}}
>
{/* Loading overlay for initial fetch */}
<Fade in={isLoading} unmountOnExit>
<Box
sx={{
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: "100%",
bgcolor:
theme.palette.mode === "dark"
? "rgba(24,24,32,0.92)"
: "rgba(255,255,255,0.85)",
zIndex: 10,
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
backdropFilter: "blur(2px)",
transition: "opacity 0.3s",
}}
>
<CircularProgress
size={48}
thickness={4}
sx={{
color: theme.palette.primary.main,
mb: 2,
animation: "spin 1.5s linear infinite",
}}
/>
<Typography sx={{ fontWeight: 500 }} color="text.secondary">
Loading theme settings…
</Typography>
</Box>
</Fade>
<Box sx={{ display: "flex", alignItems: "center", mb: 2.5, gap: 1.25 }}>
<Box
sx={{
display: "flex",
alignItems: "center",
justifyContent: "center",
width: 44,
height: 44,
borderRadius: "12px",
bgcolor: (t) => alpha(t.palette.primary.main, 0.12),
}}
>
<PaletteIcon color="primary" sx={{ fontSize: 24 }} />
</Box>
<Box>
<Typography variant="h6" sx={{ fontWeight: 700 }} color="text.primary">
Theme & Color Settings
</Typography>
<Typography variant="body2" color="text.secondary">
Choose how the app looks for you.
</Typography>
</Box>
</Box>
<Divider sx={{ mb: 3 }} />
<Typography variant="subtitle1" sx={{ mb: 1.5, fontWeight: 600 }}>
Color Mode
</Typography>
<Box
sx={{
display: "grid",
gridTemplateColumns: "1fr 1fr",
gap: 2,
mb: 3.5,
}}
>
<Zoom in>
<IconButton
sx={{
border: `2px solid ${theme.palette.mode === "light" ? theme.palette.primary.main : theme.palette.divider}`,
borderRadius: "12px",
width: "100%",
height: 60,
justifyContent: "center",
gap: 1,
transition: "border-color 0.2s, box-shadow 0.2s",
boxShadow:
theme.palette.mode === "light" ? theme.customShadows?.primary : "none",
bgcolor: "#f5f5f5",
"&:hover": { borderColor: theme.palette.primary.main, bgcolor: "#f5f5f5" },
}}
onClick={() => handleModeChange("light")}
disabled={isApplyingTheme || isUpdatingTheme}
>
<LightModeOutlinedIcon sx={{ fill: "#FFC107" }} />
<Typography fontWeight={600} sx={{ color: "#333" }}>
Light
</Typography>
</IconButton>
</Zoom>
<Zoom in>
<IconButton
sx={{
border: `2px solid ${theme.palette.mode === "dark" ? theme.palette.primary.main : theme.palette.divider}`,
borderRadius: "12px",
width: "100%",
height: 60,
justifyContent: "center",
gap: 1,
transition: "border-color 0.2s, box-shadow 0.2s",
boxShadow:
theme.palette.mode === "dark" ? theme.customShadows?.primary : "none",
bgcolor: "#1e1e1e",
"&:hover": { borderColor: theme.palette.primary.main, bgcolor: "#1e1e1e" },
}}
onClick={() => handleModeChange("dark")}
disabled={isApplyingTheme || isUpdatingTheme}
>
<DarkModeOutlinedIcon sx={{ color: "#FFC107" }} />
<Typography fontWeight={600} sx={{ color: "#fff" }}>
Dark
</Typography>
</IconButton>
</Zoom>
</Box>
<Typography variant="subtitle1" sx={{ mb: 1.5, fontWeight: 600 }}>
Color Themes
</Typography>
<Box
sx={{
display: "grid",
gridTemplateColumns: "repeat(auto-fill, minmax(96px, 1fr))",
gap: 2,
mb: 1,
}}
>
{Object.entries(themeIDs).map(([preset]) => (
<Tooltip
key={preset}
title={colorPresets[preset].label}
placement="top"
arrow
TransitionComponent={Fade}
TransitionProps={{ timeout: 400 }}
>
<Box sx={{ display: "flex", flexDirection: "column", alignItems: "center" }}>
<IconButton
id={`theme-preset-${preset}`}
sx={{
color: "#fff",
background: colorPresets[preset].gradient,
borderRadius: "16px",
width: 72,
height: 72,
border:
themePreset === preset
? `3px solid ${theme.palette.primary.main}`
: (t) => `3px solid ${alpha(t.palette.grey[500], 0.4)}`,
position: "relative",
transition: "border-color 0.2s, box-shadow 0.2s, transform 0.2s",
boxShadow:
themePreset === preset ? theme.customShadows?.primary : "none",
"&:hover": {
transform: "scale(1.06)",
boxShadow: theme.customShadows?.z8,
},
}}
variant="contained"
onClick={() => handleThemeChange(preset)}
disabled={isApplyingTheme || isUpdatingTheme}
>
{(isApplyingTheme || isUpdatingTheme) && loadingTheme === preset ? (
<Zoom in>
<CircularProgress size={28} sx={{ color: "#fff" }} />
</Zoom>
) : (
<Fade in={themePreset === preset}>
<CheckIcon
sx={{
fontSize: 32,
color: "#fff",
filter: "drop-shadow(0 2px 8px #0003)",
}}
/>
</Fade>
)}
</IconButton>
<Typography
variant="caption"
color="text.primary"
sx={{ mt: 1, fontWeight: 500 }}
>
{colorPresets[preset].label}
</Typography>
</Box>
</Tooltip>
))}
</Box>
{portalRole === "teacher" && (
<React.Fragment>
<Divider sx={{ my: 3 }} />
<Stack
direction="row"
alignItems="center"
justifyContent="space-between"
spacing={2}
flexWrap="wrap"
useFlexGap
>
<Box>
<Typography variant="subtitle1" sx={{ fontWeight: 600 }}>
Reset Theme
</Typography>
<Typography variant="body2" color="text.secondary">
Remove your saved theme and revert to the default appearance.
</Typography>
</Box>
<Button
id="delete-theme-button"
variant="outlined"
color="error"
startIcon={<RestartAltIcon />}
onClick={openDeleteDialog}
disabled={isDeletingTheme || isApplyingTheme || isUpdatingTheme}
>
{isDeletingTheme ? "Reverting…" : "Reset to default"}
</Button>
</Stack>
</React.Fragment>
)}
</Paper>
</Box>
)}
<Dialog
open={deleteDialogOpen}
onClose={closeDeleteDialog}
aria-labelledby="delete-theme-dialog-title"
aria-describedby="delete-theme-dialog-description"
>
<DialogTitle id="delete-theme-dialog-title">Reset theme to default?</DialogTitle>
<DialogContent>
<DialogContentText id="delete-theme-dialog-description">
Your saved theme will be removed and the app will revert to the default appearance. You can
apply a theme again at any time.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button id="delete-theme-cancel-button" onClick={closeDeleteDialog} color="inherit">
Cancel
</Button>
<Button
id="delete-theme-confirm-button"
onClick={handleResetTheme}
color="error"
variant="contained"
disabled={isDeletingTheme}
autoFocus
>
{isDeletingTheme ? "Reverting…" : "Reset"}
</Button>
</DialogActions>
</Dialog>
</React.Fragment>
);
};
export default ThemeSets;
|