|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + Box, |
| 4 | + Flex, |
| 5 | + Typography, |
| 6 | + Avatar, |
| 7 | + Loader, |
| 8 | + Badge, |
| 9 | + Grid, |
| 10 | + Divider, |
| 11 | +} from '@strapi/design-system'; |
| 12 | +import { useAuth, useFetchClient } from '@strapi/admin/strapi-admin'; |
| 13 | +import styled from 'styled-components'; |
| 14 | +import { User, Book, Calendar, Phone, Information } from '@strapi/icons'; |
| 15 | + |
| 16 | +const StyledContainer = styled(Box)` |
| 17 | + background: ${({ theme }) => theme.colors.neutral0}; |
| 18 | + border-radius: 16px; |
| 19 | + border: 1px solid ${({ theme }) => theme.colors.neutral150}; |
| 20 | + box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08); |
| 21 | + overflow: hidden; |
| 22 | + height: 100%; |
| 23 | + transition: transform 0.3s ease, box-shadow 0.3s ease; |
| 24 | + |
| 25 | + &:hover { |
| 26 | + transform: translateY(-4px); |
| 27 | + box-shadow: 0 12px 24px rgba(0, 0, 0, 0.12); |
| 28 | + } |
| 29 | +`; |
| 30 | + |
| 31 | +const HeaderBox = styled(Box)` |
| 32 | + background: linear-gradient(135deg, #1e1e2f 0%, #4a4a7d 100%); |
| 33 | + padding: ${({ theme }) => theme.spaces[7]}; |
| 34 | + color: white; |
| 35 | + position: relative; |
| 36 | +`; |
| 37 | + |
| 38 | +const AvatarWrapper = styled(Flex)` |
| 39 | + position: absolute; |
| 40 | + bottom: -40px; |
| 41 | + left: 32px; |
| 42 | + border: 5px solid white; |
| 43 | + border-radius: 50%; |
| 44 | + background: white; |
| 45 | + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); |
| 46 | + z-index: 2; |
| 47 | +`; |
| 48 | + |
| 49 | +const ContentBox = styled(Box)` |
| 50 | + padding: ${({ theme }) => theme.spaces[12]} ${({ theme }) => theme.spaces[8]} ${({ theme }) => theme.spaces[7]} ${({ theme }) => theme.spaces[8]}; |
| 51 | +`; |
| 52 | + |
| 53 | +const InfoItem = ({ icon: Icon, label, value }: { icon: any, label: string, value: string }) => ( |
| 54 | + <Flex gap={3} alignItems="center" marginBottom={5}> |
| 55 | + <Box padding={3} background="primary100" borderRadius="12px"> |
| 56 | + <Icon width="18px" height="18px" fill="#4945ff" /> |
| 57 | + </Box> |
| 58 | + <Box> |
| 59 | + <Typography variant="sigma" textColor="neutral500" fontWeight="bold" display="block" style={{ letterSpacing: '0.05em' }}> |
| 60 | + {label} |
| 61 | + </Typography> |
| 62 | + <Typography variant="omega" fontWeight="bold" textColor="neutral800"> |
| 63 | + {value || 'Not set'} |
| 64 | + </Typography> |
| 65 | + </Box> |
| 66 | + </Flex> |
| 67 | +); |
| 68 | + |
| 69 | +interface ProfileData { |
| 70 | + id: number; |
| 71 | + university: string; |
| 72 | + birth: string; |
| 73 | + phone: string; |
| 74 | + identifier: string; |
| 75 | + avatar: any; |
| 76 | +} |
| 77 | + |
| 78 | +export default function ProfileWidget() { |
| 79 | + const user = useAuth('ProfileWidget', (state: any) => state.user); |
| 80 | + const { get } = useFetchClient(); |
| 81 | + const [profile, setProfile] = useState<ProfileData | null>(null); |
| 82 | + const [loading, setLoading] = useState(true); |
| 83 | + const [error, setError] = useState(false); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + const fetchProfile = async () => { |
| 87 | + if (!user?.id) return; |
| 88 | + try { |
| 89 | + setLoading(true); |
| 90 | + // Fetch profiles populated with user and avatar, filtered by the authenticated user's ID |
| 91 | + const response = await get(`/api/profiles?filters[user][id][$eq]=${user.id}&populate=*`); |
| 92 | + |
| 93 | + if (response.data && response.data.data && response.data.data.length > 0) { |
| 94 | + const item = response.data.data[0]; |
| 95 | + setProfile({ |
| 96 | + id: item.id, |
| 97 | + university: item.university, |
| 98 | + birth: item.birth, |
| 99 | + phone: item.phone, |
| 100 | + identifier: item.identifier, |
| 101 | + avatar: item.avatar, |
| 102 | + }); |
| 103 | + } |
| 104 | + } catch (err) { |
| 105 | + console.error('Error fetching profile:', err); |
| 106 | + setError(true); |
| 107 | + } finally { |
| 108 | + setLoading(false); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + fetchProfile(); |
| 113 | + }, [user, get]); |
| 114 | + |
| 115 | + if (loading) { |
| 116 | + return ( |
| 117 | + <StyledContainer> |
| 118 | + <Flex justifyContent="center" alignItems="center" height="300px"> |
| 119 | + <Loader>Syncing your profile...</Loader> |
| 120 | + </Flex> |
| 121 | + </StyledContainer> |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if (error || !user) { |
| 126 | + return ( |
| 127 | + <StyledContainer> |
| 128 | + <Flex justifyContent="center" alignItems="center" height="300px" padding={6} direction="column" gap={4}> |
| 129 | + <Typography variant="beta" textColor="danger600">Profile Not Found</Typography> |
| 130 | + <Typography textColor="neutral600" textAlign="center"> |
| 131 | + We couldn't retrieve your additional profile details. Please contact the administrator. |
| 132 | + </Typography> |
| 133 | + </Flex> |
| 134 | + </StyledContainer> |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + const fullName = `${user.firstname || ''} ${user.lastname || ''}`.trim() || user.username || 'Nuclear Member'; |
| 139 | + const avatarUrl = profile?.avatar?.url ? (profile.avatar.url.startsWith('http') ? profile.avatar.url : `${window.location.origin}${profile.avatar.url}`) : null; |
| 140 | + const initials = fullName.split(' ').map(n => n[0]).join('').toUpperCase().substring(0, 2); |
| 141 | + |
| 142 | + return ( |
| 143 | + <StyledContainer> |
| 144 | + <HeaderBox> |
| 145 | + <Flex justifyContent="space-between" alignItems="flex-start"> |
| 146 | + <Typography variant="beta" fontWeight="bold" textColor="neutral0"> |
| 147 | + Account Overview |
| 148 | + </Typography> |
| 149 | + <Badge variant="success">Verified</Badge> |
| 150 | + </Flex> |
| 151 | + <AvatarWrapper> |
| 152 | + <Avatar |
| 153 | + src={avatarUrl} |
| 154 | + alt={fullName} |
| 155 | + fallback={initials} |
| 156 | + width="90px" |
| 157 | + height="90px" |
| 158 | + /> |
| 159 | + </AvatarWrapper> |
| 160 | + </HeaderBox> |
| 161 | + |
| 162 | + <ContentBox> |
| 163 | + <Box marginBottom={8}> |
| 164 | + <Typography variant="alpha" fontWeight="bold" textColor="neutral800" display="block"> |
| 165 | + {fullName} |
| 166 | + </Typography> |
| 167 | + <Typography variant="epsilon" textColor="neutral500"> |
| 168 | + Member of Nuclear Community |
| 169 | + </Typography> |
| 170 | + </Box> |
| 171 | + |
| 172 | + <Grid.Root gap={4}> |
| 173 | + <Grid.Item col={6}> |
| 174 | + <InfoItem |
| 175 | + icon={Book} |
| 176 | + label="INSTITUTION" |
| 177 | + value={profile?.university} |
| 178 | + /> |
| 179 | + </Grid.Item> |
| 180 | + <Grid.Item col={6}> |
| 181 | + <InfoItem |
| 182 | + icon={Information} |
| 183 | + label="MEMBER ID" |
| 184 | + value={profile?.identifier} |
| 185 | + /> |
| 186 | + </Grid.Item> |
| 187 | + <Grid.Item col={6}> |
| 188 | + <InfoItem |
| 189 | + icon={Calendar} |
| 190 | + label="DATE OF BIRTH" |
| 191 | + value={profile?.birth ? new Date(profile.birth).toLocaleDateString(undefined, { dateStyle: 'long' }) : 'Not specified'} |
| 192 | + /> |
| 193 | + </Grid.Item> |
| 194 | + <Grid.Item col={6}> |
| 195 | + <InfoItem |
| 196 | + icon={Phone} |
| 197 | + label="CONTACT" |
| 198 | + value={profile?.phone} |
| 199 | + /> |
| 200 | + </Grid.Item> |
| 201 | + </Grid.Root> |
| 202 | + </ContentBox> |
| 203 | + </StyledContainer> |
| 204 | + ); |
| 205 | +} |
0 commit comments