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 | 111x 111x 111x 111x 111x 111x 111x 111x 19x 19x 18x 18x 18x 18x 111x 1x 111x 14x 14x 14x 111x 11x 11x 11x 11x 11x 11x 11x 10x 10x 10x 1x 11x 111x 3x 3x 3x 2x 2x 2x 1x 3x 111x 111x | import * as React from "react";
import { useRef, useState } from "react";
import { useDispatch } from "react-redux";
import { useSnackbar } from "notistack";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import { Alert, Box, IconButton, Typography } from "@mui/material";
import CloseIcon from "@mui/icons-material/Close";
import { _addClassPhoto, _updateClassPhoto, _deleteClassPhoto } from "@/store/slices/teacher/TeacherClasses";
// Upload/replace/remove dialog for a class's cover photo. Shared by the
// class-list cards (ClassCards.jsx) and the class-details page
// (ClassDetails.jsx). Mirrors the teacher account ChangePhoto.jsx flow —
// same hidden-input + FileReader preview + FormData upload pattern — scoped
// to a single class.
//
// The `_addClassPhoto`/`_updateClassPhoto`/`_deleteClassPhoto` thunks already
// patch the class-list Redux slice (TeacherClasses.classData) in place, but
// the class-details page reads from a separate slice (TeacherSpecificClass).
// `onPhotoUpdated(newClassPhotoUrl)` lets each caller sync whatever local
// state/slice it owns after a successful mutation.
export default function ClassPhotoDialog({ open, onClose, classUUID, currentPhotoUrl, onPhotoUpdated }) {
const dispatch = useDispatch();
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
const inputRef = useRef(null);
const [preview, setPreview] = useState(""); // local FileReader preview
const [selectedFile, setSelectedFile] = useState(null);
const [isSaving, setIsSaving] = useState(false);
const browseImage = () => inputRef.current.click();
const handleFileChange = (event) => {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onloadend = () => setPreview(reader.result);
setSelectedFile(file);
reader.readAsDataURL(file);
};
const snackbarAction = (key) => (
<IconButton size="small" color="inherit" onClick={() => closeSnackbar(key)}>
<CloseIcon fontSize="small" />
</IconButton>
);
const resetAndClose = () => {
setPreview("");
setSelectedFile(null);
onClose();
};
const handleSave = () => {
Iif (!selectedFile) return;
const formData = new FormData();
formData.append("file", selectedFile);
const action = currentPhotoUrl ? _updateClassPhoto : _addClassPhoto;
setIsSaving(true);
dispatch(action({ classUUID, formData }))
.then((response) => {
if (!response.error) {
onPhotoUpdated?.(response.payload?.response?.data?.updated_class?.class_photo ?? null);
resetAndClose();
enqueueSnackbar(
currentPhotoUrl ? "Class photo successfully updated" : "Class photo successfully added",
{ variant: "success", action: snackbarAction }
);
} else {
enqueueSnackbar("Invalid image upload", { variant: "error", action: snackbarAction });
}
})
.finally(() => setIsSaving(false));
};
const handleDelete = () => {
setIsSaving(true);
dispatch(_deleteClassPhoto(classUUID))
.then((response) => {
if (!response.error) {
onPhotoUpdated?.(null);
resetAndClose();
enqueueSnackbar("Class photo successfully deleted", {
variant: "success",
action: snackbarAction,
});
} else {
enqueueSnackbar("Failed to delete class photo", { variant: "error", action: snackbarAction });
}
})
.finally(() => setIsSaving(false));
};
const displayedPhoto = preview || currentPhotoUrl;
return (
<React.Fragment>
<input ref={inputRef} type="file" accept="image/*" hidden onChange={handleFileChange} />
<Dialog open={open} onClose={resetAndClose} fullWidth aria-labelledby="class-photo-dialog-title">
<DialogTitle
id="class-photo-dialog-title"
sx={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}
>
<Typography variant="body1">Class Cover Photo</Typography>
</DialogTitle>
<DialogContent dividers>
<Alert severity="info" sx={{ mb: 2 }}>
JPG or PNG, up to 10MB. The image is resized automatically.
</Alert>
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: 160,
borderRadius: 2,
overflow: "hidden",
bgcolor: "action.hover",
}}
>
{displayedPhoto ? (
<Box
component="img"
src={displayedPhoto}
alt="Class cover"
sx={{ width: "100%", height: "100%", objectFit: "cover" }}
/>
) : (
<Typography variant="body2" color="text.secondary">
No cover photo yet
</Typography>
)}
</Box>
</DialogContent>
<DialogActions sx={{ display: "flex", justifyContent: "space-between" }}>
<Button sx={{ width: "150px" }} variant="inherit" onClick={resetAndClose} disabled={isSaving}>
Cancel
</Button>
<Box sx={{ display: "flex", gap: 1 }}>
{currentPhotoUrl && !selectedFile && (
<Button variant="outlined" color="error" onClick={handleDelete} disabled={isSaving}>
Delete
</Button>
)}
<Button variant="outlined" color="primary" onClick={browseImage} disabled={isSaving}>
{selectedFile ? "Change" : "Browse"}
</Button>
<Button
variant="contained"
color="primary"
onClick={handleSave}
disabled={!selectedFile || isSaving}
>
Save
</Button>
</Box>
</DialogActions>
</Dialog>
</React.Fragment>
);
}
|