2022-02-08 13:36:32 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-04-23 13:49:42 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import OutsideClickHandler from 'react-outside-click-handler';
|
|
|
|
|
|
|
|
import { Avatar, Button } from '@material-ui/core';
|
|
|
|
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
|
|
|
|
import { useStyles } from './UserProfile.styles';
|
|
|
|
import { useCommonStyles } from '../../../common.styles';
|
|
|
|
import UserProfileContent from './UserProfileContent/UserProfileContent';
|
2022-02-08 13:36:32 +01:00
|
|
|
import { IUser } from "../../../interfaces/user";
|
|
|
|
import { ILocationSettings } from "../../../hooks/useLocationSettings";
|
2022-02-04 12:41:59 +01:00
|
|
|
|
|
|
|
interface IUserProfileProps {
|
2022-02-08 13:36:32 +01:00
|
|
|
profile: IUser
|
|
|
|
locationSettings: ILocationSettings
|
|
|
|
setLocationSettings: React.Dispatch<React.SetStateAction<ILocationSettings>>
|
2022-02-04 12:41:59 +01:00
|
|
|
}
|
2021-04-23 13:49:42 +02:00
|
|
|
|
|
|
|
const UserProfile = ({
|
|
|
|
profile,
|
2022-02-08 13:36:32 +01:00
|
|
|
locationSettings,
|
|
|
|
setLocationSettings,
|
2022-02-04 12:41:59 +01:00
|
|
|
}: IUserProfileProps) => {
|
2021-04-23 13:49:42 +02:00
|
|
|
const [showProfile, setShowProfile] = useState(false);
|
2022-02-04 12:41:59 +01:00
|
|
|
const [currentLocale, setCurrentLocale] = useState<string>();
|
2021-04-23 13:49:42 +02:00
|
|
|
|
|
|
|
const styles = useStyles();
|
|
|
|
const commonStyles = useCommonStyles();
|
|
|
|
|
|
|
|
const [possibleLocales, setPossibleLocales] = useState([
|
2021-04-28 11:57:45 +02:00
|
|
|
'en-US',
|
|
|
|
'en-GB',
|
|
|
|
'nb-NO',
|
|
|
|
'sv-SE',
|
|
|
|
'da-DK',
|
|
|
|
'en-IN',
|
|
|
|
'de',
|
|
|
|
'cs',
|
|
|
|
'pt-BR',
|
|
|
|
'fr-FR',
|
2021-04-23 13:49:42 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-28 11:57:45 +02:00
|
|
|
let found = possibleLocales.find(l =>
|
2022-02-08 13:36:32 +01:00
|
|
|
l.toLowerCase().includes(locationSettings.locale.toLowerCase())
|
2021-04-28 11:57:45 +02:00
|
|
|
);
|
|
|
|
setCurrentLocale(found);
|
2021-04-23 13:49:42 +02:00
|
|
|
if (!found) {
|
2022-02-08 13:36:32 +01:00
|
|
|
setPossibleLocales(prev => [...prev, locationSettings.locale]);
|
2021-04-23 13:49:42 +02:00
|
|
|
}
|
|
|
|
/* eslint-disable-next-line*/
|
2022-02-08 13:36:32 +01:00
|
|
|
}, [locationSettings]);
|
2021-04-23 13:49:42 +02:00
|
|
|
|
|
|
|
const email = profile ? profile.email : '';
|
|
|
|
const imageUrl = email ? profile.imageUrl : 'unknown-user.png';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<OutsideClickHandler onOutsideClick={() => setShowProfile(false)}>
|
|
|
|
<div className={styles.profileContainer}>
|
|
|
|
<Button
|
|
|
|
className={classnames(
|
|
|
|
commonStyles.flexRow,
|
|
|
|
commonStyles.itemsCenter,
|
|
|
|
styles.button
|
|
|
|
)}
|
|
|
|
onClick={() => setShowProfile(prev => !prev)}
|
|
|
|
role="button"
|
|
|
|
disableRipple
|
|
|
|
>
|
|
|
|
<Avatar alt="user image" src={imageUrl} />
|
|
|
|
<KeyboardArrowDownIcon />
|
|
|
|
</Button>
|
|
|
|
<UserProfileContent
|
|
|
|
showProfile={showProfile}
|
|
|
|
imageUrl={imageUrl}
|
|
|
|
profile={profile}
|
2022-02-08 13:36:32 +01:00
|
|
|
setLocationSettings={setLocationSettings}
|
2021-04-23 13:49:42 +02:00
|
|
|
possibleLocales={possibleLocales}
|
2021-04-28 11:57:45 +02:00
|
|
|
setCurrentLocale={setCurrentLocale}
|
|
|
|
currentLocale={currentLocale}
|
2021-04-23 13:49:42 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</OutsideClickHandler>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UserProfile;
|