All files / src/app/student-page/page/dashboard/components SearchTeacherWidget.jsx

100% Statements 49/49
90.24% Branches 37/41
100% Functions 18/18
100% Lines 48/48

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                                                                      94x 94x 94x 94x 94x 94x   94x 28x 27x 27x 27x     25x 28x 14x 14x   11x 11x 11x     2x 2x   27x       94x 2x 2x 2x     94x 1x 1x     94x 2x 2x     94x       94x       94x                 94x               94x                                                   30x 2x                                                                                                   17x 17x   2x                                                                                 13x                                                                                                                       26x                                                                                                                     4x         4x     4x                                           1x 1x                              
import { useState } from "react";
import { alpha } from "@mui/material/styles";
import {
    Avatar,
    Box,
    Button,
    Card,
    CardContent,
    Chip,
    CircularProgress,
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
    Divider,
    List,
    ListItem,
    ListItemAvatar,
    ListItemButton,
    ListItemText,
    Stack,
    TextField,
    Typography,
} from "@mui/material";
import ManageSearchIcon from "@mui/icons-material/ManageSearch";
import SearchIcon from "@mui/icons-material/Search";
import PersonIcon from "@mui/icons-material/Person";
import EmailIcon from "@mui/icons-material/Email";
import SchoolIcon from "@mui/icons-material/School";
import ClassIcon from "@mui/icons-material/Class";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import axios from "axios";
import { API_ENDPOINTS } from "@/api/endpoints";
 
export default function SearchTeacherWidget() {
    const [search, setSearch] = useState("");
    const [results, setResults] = useState([]);
    const [selectedProfile, setSelectedProfile] = useState(null);
    const [resultsOpen, setResultsOpen] = useState(false);
    const [profileOpen, setProfileOpen] = useState(false);
    const [loading, setLoading] = useState(false);
 
    const handleSearch = async () => {
        if (!search.trim()) return;
        setLoading(true);
        try {
            const response = await axios.get(API_ENDPOINTS.student.account.teacherFind, {
                params: { search: search.trim(), page: 1, page_size: 10 },
            });
            const data = response.data.data ?? response.data;
            if (Array.isArray(data)) {
                setResults(data);
                setResultsOpen(true);
            } else {
                setResults([]);
                setSelectedProfile(data);
                setProfileOpen(true);
            }
        } catch {
            setResults([]);
            setResultsOpen(true);
        } finally {
            setLoading(false);
        }
    };
 
    const openProfile = (profile) => {
        setResultsOpen(false);
        setSelectedProfile(profile);
        setProfileOpen(true);
    };
 
    const closeResults = () => {
        setResultsOpen(false);
        setResults([]);
    };
 
    const closeProfile = () => {
        setProfileOpen(false);
        setSelectedProfile(null);
    };
 
    const profileFullName = selectedProfile
        ? [selectedProfile.first_name, selectedProfile.middle_name, selectedProfile.last_name].filter(Boolean).join(" ")
        : "";
 
    return (
        <>
            <Card
                elevation={0}
                sx={{ border: (t) => `1px solid ${t.palette.divider}`, borderRadius: 3, overflow: "hidden" }}
            >
                <Box
                    sx={{
                        px: 2.5,
                        py: 1.75,
                        display: "flex",
                        alignItems: "center",
                        gap: 1.5,
                        borderBottom: (t) => `1px solid ${t.palette.divider}`,
                    }}
                >
                    <Box
                        sx={{
                            width: 34,
                            height: 34,
                            borderRadius: 2,
                            bgcolor: (t) => alpha(t.palette.primary.main, 0.1),
                            display: "flex",
                            alignItems: "center",
                            justifyContent: "center",
                            flexShrink: 0,
                        }}
                    >
                        <ManageSearchIcon sx={{ color: "primary.main", fontSize: 18 }} />
                    </Box>
                    <Box>
                        <Typography variant="subtitle2" fontWeight={700} color="text.primary" sx={{ lineHeight: 1.2 }}>
                            Search Teacher
                        </Typography>
                        <Typography variant="caption" color="text.secondary">
                            Find a teacher by name
                        </Typography>
                    </Box>
                </Box>
 
                <CardContent sx={{ p: 2 }}>
                    <Stack spacing={1.5}>
                        <TextField
                            label="Name or email"
                            size="small"
                            fullWidth
                            value={search}
                            onChange={(e) => setSearch(e.target.value)}
                            onKeyDown={(e) => e.key === "Enter" && !loading && handleSearch()}
                            slotProps={{
                                input: {
                                    startAdornment: (
                                        <SearchIcon sx={{ color: "text.disabled", mr: 0.75, fontSize: 18 }} />
                                    ),
                                },
                            }}
                        />
                        <Button
                            variant="contained"
                            onClick={handleSearch}
                            disabled={loading || !search.trim()}
                            fullWidth
                            startIcon={loading ? <CircularProgress size={14} color="inherit" /> : <SearchIcon />}
                            sx={{ textTransform: "none", fontWeight: 700, borderRadius: 2 }}
                        >
                            {loading ? "Searching…" : "Search"}
                        </Button>
                    </Stack>
                </CardContent>
            </Card>
 
            {/* Results Dialog */}
            <Dialog open={resultsOpen} onClose={closeResults} fullWidth maxWidth="sm">
                <DialogTitle sx={{ pb: 1 }}>
                    <Typography variant="h6" fontWeight={700}>
                        Search Results
                    </Typography>
                    <Typography variant="body2" color="text.secondary">
                        {results.length > 0
                            ? `${results.length} teacher${results.length !== 1 ? "s" : ""} found`
                            : "No results found"}
                    </Typography>
                </DialogTitle>
                <Divider />
                <DialogContent sx={{ p: 0 }}>
                    {results.length === 0 ? (
                        <Box sx={{ py: 6, textAlign: "center" }}>
                            <PersonIcon sx={{ fontSize: 48, color: "text.disabled", mb: 1 }} />
                            <Typography variant="body1" color="text.secondary">
                                No teachers found matching your search.
                            </Typography>
                            <Typography variant="body2" color="text.disabled" sx={{ mt: 0.5 }}>
                                Try a different name.
                            </Typography>
                        </Box>
                    ) : (
                        <List disablePadding>
                            {results.map((profile, i) => {
                                const fullName = `${profile.first_name || ""} ${profile.last_name || ""}`.trim();
                                return (
                                    <ListItem key={i} disablePadding divider={i < results.length - 1}>
                                        <ListItemButton onClick={() => openProfile(profile)} sx={{ px: 3, py: 1.5 }}>
                                            <ListItemAvatar>
                                                <Avatar src={profile.profile_picture} sx={{ width: 40, height: 40 }}>
                                                    {profile.first_name?.[0]}
                                                </Avatar>
                                            </ListItemAvatar>
                                            <ListItemText
                                                primary={
                                                    <Typography variant="body1" fontWeight={600}>
                                                        {fullName || profile.email}
                                                    </Typography>
                                                }
                                                secondary={profile.email}
                                            />
                                            <Chip
                                                label="Teacher"
                                                size="small"
                                                color="primary"
                                                sx={{ fontSize: "0.65rem", fontWeight: 700, flexShrink: 0, ml: 1 }}
                                            />
                                        </ListItemButton>
                                    </ListItem>
                                );
                            })}
                        </List>
                    )}
                </DialogContent>
                <DialogActions sx={{ px: 3, py: 2 }}>
                    <Button onClick={closeResults} sx={{ textTransform: "none", fontWeight: 600 }}>
                        Close
                    </Button>
                </DialogActions>
            </Dialog>
 
            {/* Profile Dialog */}
            <Dialog open={profileOpen} onClose={closeProfile} fullWidth maxWidth="sm">
                {selectedProfile && (
                    <>
                        <Box
                            sx={{
                                background: (t) =>
                                    `linear-gradient(135deg, ${t.palette.primary.dark ?? t.palette.primary.main}, ${t.palette.primary.main})`,
                                px: 3,
                                pt: 3,
                                pb: 8,
                                textAlign: "center",
                            }}
                        >
                            <Typography variant="h6" fontWeight={700} sx={{ color: "#fff" }}>
                                Teacher Profile
                            </Typography>
                        </Box>
                        <Box
                            sx={{
                                display: "flex",
                                justifyContent: "center",
                                mt: -7,
                                mb: 1,
                                position: "relative",
                                zIndex: 1,
                            }}
                        >
                            <Avatar
                                src={selectedProfile?.profile_picture}
                                alt={profileFullName}
                                sx={{
                                    width: 100,
                                    height: 100,
                                    border: "4px solid",
                                    borderColor: "background.paper",
                                    boxShadow: "0 4px 20px rgba(0,0,0,0.18)",
                                    fontSize: 42,
                                }}
                            >
                                <PersonIcon sx={{ fontSize: 52 }} />
                            </Avatar>
                        </Box>
                        <DialogContent sx={{ pt: 0, px: 3 }}>
                            <Box textAlign="center" sx={{ mb: 2.5 }}>
                                <Typography variant="h5" fontWeight={800} color="text.primary">
                                    {profileFullName}
                                </Typography>
                                <Chip label="Teacher" color="primary" size="small" sx={{ mt: 0.75, fontWeight: 700 }} />
                            </Box>
                            <Divider sx={{ mb: 2.5 }} />
                            <Stack spacing={2}>
                                <Card variant="outlined" sx={{ borderRadius: 2 }}>
                                    <CardContent sx={{ p: "16px !important" }}>
                                        <Stack spacing={1}>
                                            {[
                                                {
                                                    icon: <EmailIcon fontSize="small" />,
                                                    label: "Email",
                                                    value: selectedProfile?.email,
                                                },
                                                {
                                                    icon: <SchoolIcon fontSize="small" />,
                                                    label: "School",
                                                    value: selectedProfile?.school || "Not specified",
                                                },
                                            ].map((row) => (
                                                <Box
                                                    key={row.label}
                                                    sx={{
                                                        display: "flex",
                                                        alignItems: "center",
                                                        gap: 1.5,
                                                        p: 1.25,
                                                        borderRadius: 1.5,
                                                        bgcolor: "action.hover",
                                                    }}
                                                >
                                                    <Box sx={{ color: "text.secondary", display: "flex" }}>
                                                        {row.icon}
                                                    </Box>
                                                    <Box>
                                                        <Typography
                                                            variant="caption"
                                                            color="text.secondary"
                                                            sx={{
                                                                display: "block",
                                                                fontWeight: 700,
                                                                textTransform: "uppercase",
                                                                fontSize: "0.65rem",
                                                                letterSpacing: "0.06em",
                                                            }}
                                                        >
                                                            {row.label}
                                                        </Typography>
                                                        <Typography
                                                            variant="body2"
                                                            fontWeight={500}
                                                            color="text.primary"
                                                        >
                                                            {row.value}
                                                        </Typography>
                                                    </Box>
                                                </Box>
                                            ))}
                                        </Stack>
                                    </CardContent>
                                </Card>
                                <Card variant="outlined" sx={{ borderRadius: 2 }}>
                                    <CardContent sx={{ p: "16px !important" }}>
                                        <Box sx={{ display: "flex", alignItems: "center", gap: 1, mb: 1.5 }}>
                                            <ClassIcon color="primary" fontSize="small" />
                                            <Typography variant="subtitle2" fontWeight={700}>
                                                Subjects
                                                {selectedProfile.classes?.length > 0 && (
                                                    <Chip
                                                        label={selectedProfile.classes.length}
                                                        size="small"
                                                        sx={{ ml: 1, height: 18, fontSize: "0.7rem", fontWeight: 700 }}
                                                    />
                                                )}
                                            </Typography>
                                        </Box>
                                        {selectedProfile.classes?.length > 0 ? (
                                            <Box sx={{ display: "flex", flexWrap: "wrap", gap: 0.75 }}>
                                                {selectedProfile.classes.map((item, i) => (
                                                    <Chip
                                                        key={i}
                                                        label={item.title}
                                                        size="small"
                                                        sx={{
                                                            bgcolor: (t) => alpha(t.palette.primary.main, 0.12),
                                                            color: "text.primary",
                                                            border: (t) =>
                                                                `1px solid ${alpha(t.palette.primary.main, 0.35)}`,
                                                            fontWeight: 500,
                                                        }}
                                                    />
                                                ))}
                                            </Box>
                                        ) : (
                                            <Typography variant="body2" color="text.secondary" fontStyle="italic">
                                                No subjects assigned
                                            </Typography>
                                        )}
                                    </CardContent>
                                </Card>
                            </Stack>
                        </DialogContent>
                    </>
                )}
                <DialogActions sx={{ px: 3, py: 2, gap: 1 }}>
                    {results.length > 0 && (
                        <Button
                            startIcon={<ArrowBackIcon />}
                            onClick={() => {
                                closeProfile();
                                setResultsOpen(true);
                            }}
                            sx={{ textTransform: "none", fontWeight: 600, mr: "auto" }}
                        >
                            Back to results
                        </Button>
                    )}
                    <Button variant="contained" onClick={closeProfile} sx={{ textTransform: "none", fontWeight: 600 }}>
                        Close
                    </Button>
                </DialogActions>
            </Dialog>
        </>
    );
}