All files / src/utils/components/dialogs ViewProfile.jsx

80% Statements 16/20
55.88% Branches 19/34
20% Functions 1/5
80% Lines 16/20

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                                                                        4x                                                                         4x 60x 60x 60x 60x   60x     60x   60x 60x     60x 60x       60x 60x   60x   60x                                                                                                                                                                                                                                                      
import React from "react";
import {
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
    Box,
    Typography,
    Button,
    Avatar,
    IconButton,
    Stack,
    Divider,
} from "@mui/material";
import { alpha, useTheme } from "@mui/material/styles";
import CloseIcon from "@mui/icons-material/Close";
import SchoolOutlinedIcon from "@mui/icons-material/SchoolOutlined";
import LocationCityOutlinedIcon from "@mui/icons-material/LocationCityOutlined";
import AccessTimeIcon from "@mui/icons-material/AccessTime";
import { getTimeZoneName } from "@/utils/helpers/getTimeZoneName";
import { getPortalRole } from "@/authentication/auth0";
import useOrganization from "@/utils/hooks/useOrganization";
import {
    formatDistrictAddress,
    formatProfileDisplayName,
    formatSchoolAddress,
    formatSchoolLocation,
    getOrganizationDistrictName,
    getOrganizationPlanLabel,
    getOrganizationPlanSlug,
    getOrganizationSchoolName,
} from "@/utils/helpers/organizationDisplay";
import SoftChip from "@/utils/components/feature-access/SoftChip";
import PlanChip from "@/utils/components/feature-access/PlanChip";
import { SHARED_IDS } from "@/utils/helpers/testIds";
 
const ProfileField = ({ icon, label, value, secondary }) => (
    <Stack direction="row" spacing={1.5} alignItems="flex-start" sx={{ py: 1.25 }}>
        <Box
            sx={{
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                width: 36,
                height: 36,
                borderRadius: 1,
                bgcolor: "action.hover",
                flexShrink: 0,
                mt: 0.25,
            }}
        >
            {icon}
        </Box>
        <Box sx={{ minWidth: 0, flex: 1 }}>
            <Typography variant="caption" color="text.secondary" sx={{ display: "block", lineHeight: 1.2, mb: 0.25 }}>
                {label}
            </Typography>
            <Typography variant="body2" sx={{ fontWeight: 600, wordBreak: "break-word", lineHeight: 1.45 }}>
                {value || "—"}
            </Typography>
            {secondary ? (
                <Typography
                    variant="caption"
                    color="text.secondary"
                    sx={{ display: "block", mt: 0.35, lineHeight: 1.45 }}
                >
                    {secondary}
                </Typography>
            ) : null}
        </Box>
    </Stack>
);
 
const ViewProfile = ({ open, setShowProfile, userData, subscriptionPlan = null }) => {
    const theme = useTheme();
    const portalRole = getPortalRole();
    const userType = userData?.role === "student" || portalRole === "student" ? "student" : "teacher";
    const { organization, loading: organizationLoading } = useOrganization(userType);
 
    const fullName = userData
        ? [userData.first_name, userData.middle_name, userData.last_name].filter(Boolean).join(" ")
        : "";
    const displayName = formatProfileDisplayName(fullName);
 
    const resolvedPlan = subscriptionPlan || getOrganizationPlanSlug(organization);
    const resolvedPlanName = getOrganizationPlanLabel(organization);
 
    const schoolName =
        organizationLoading && !organization ? "Loading…" : getOrganizationSchoolName(organization, userData, "—");
    const schoolSecondary =
        formatSchoolLocation(organization?.school) || formatSchoolAddress(organization?.school) || null;
 
    const districtName =
        organizationLoading && !organization ? "Loading…" : getOrganizationDistrictName(organization, "—");
    const districtSecondary = formatDistrictAddress(organization?.district);
 
    const roleLabel = userData?.role ? userData.role.charAt(0).toUpperCase() + userData.role.slice(1) : null;
 
    return (
        <Dialog
            id={SHARED_IDS.viewProfileDialog}
            open={open}
            onClose={() => setShowProfile(false)}
            maxWidth="xs"
            fullWidth
        >
            <IconButton
                id={SHARED_IDS.viewProfileClose}
                aria-label="Close"
                onClick={() => setShowProfile(false)}
                size="small"
                sx={{
                    position: "absolute",
                    right: 12,
                    top: 12,
                    zIndex: 1,
                    width: 32,
                    height: 32,
                    bgcolor: alpha(theme.palette.grey[500], 0.16),
                    color: theme.palette.text.secondary,
                    "&:hover": { bgcolor: alpha(theme.palette.grey[500], 0.32) },
                }}
            >
                <CloseIcon sx={{ fontSize: 18 }} />
            </IconButton>
 
            {userData ? (
                <>
                    <DialogTitle component="div" sx={{ textAlign: "center", pt: 4, pb: 2, px: 3 }}>
                        <Avatar
                            src={userData.profile_picture}
                            alt={displayName}
                            sx={{
                                width: 80,
                                height: 80,
                                fontSize: 28,
                                bgcolor: "primary.main",
                                color: "primary.contrastText",
                                mx: "auto",
                                mb: 1.5,
                            }}
                        >
                            {userData.first_name?.[0]}
                            {userData.last_name?.[0]}
                        </Avatar>
                        <Typography
                            variant="h6"
                            component="h2"
                            sx={{
                                fontWeight: 600,
                                lineHeight: 1.35,
                                wordBreak: "break-word",
                                px: 1,
                            }}
                        >
                            {displayName}
                        </Typography>
                        <Typography variant="body2" color="text.secondary" sx={{ mt: 0.5 }}>
                            {userData.email}
                        </Typography>
                        {roleLabel ? (
                            <Stack
                                direction="row"
                                spacing={0.75}
                                justifyContent="center"
                                flexWrap="wrap"
                                useFlexGap
                                sx={{ mt: 1.25 }}
                            >
                                <SoftChip
                                    id={SHARED_IDS.viewProfileRoleChip}
                                    label={roleLabel}
                                    color="accent"
                                    size="sm"
                                />
                                {resolvedPlan || resolvedPlanName ? (
                                    <PlanChip
                                        id={SHARED_IDS.viewProfilePlanChip}
                                        plan={resolvedPlan}
                                        planName={resolvedPlanName}
                                        size="sm"
                                    />
                                ) : null}
                            </Stack>
                        ) : null}
                    </DialogTitle>
 
                    <Divider />
 
                    <DialogContent sx={{ pt: 1.5, pb: 1 }}>
                        <ProfileField
                            icon={<SchoolOutlinedIcon sx={{ fontSize: 20, color: "text.secondary" }} />}
                            label="School"
                            value={schoolName}
                            secondary={schoolSecondary}
                        />
                        <ProfileField
                            icon={<LocationCityOutlinedIcon sx={{ fontSize: 20, color: "text.secondary" }} />}
                            label="District"
                            value={districtName}
                            secondary={districtSecondary}
                        />
                        <ProfileField
                            icon={<AccessTimeIcon sx={{ fontSize: 20, color: "text.secondary" }} />}
                            label="Time zone"
                            value={getTimeZoneName()}
                        />
                    </DialogContent>
 
                    <DialogActions sx={{ px: 3, pb: 3, pt: 0 }}>
                        <Button variant="contained" fullWidth disableElevation onClick={() => setShowProfile(false)}>
                            Close
                        </Button>
                    </DialogActions>
                </>
            ) : null}
        </Dialog>
    );
};
 
export default ViewProfile;