-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from MuhammadKhalilzadeh/020-oct-19-refactori…
…ng-components New avatar component refactored and created
- Loading branch information
Showing
4 changed files
with
144 additions
and
27 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
Clients/src/presentation/components/Avatar/VWAvatar/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Avatar as VWAvatar } from "@mui/material"; | ||
|
||
interface User { | ||
firstname: string; | ||
lastname: string; | ||
pathToImage: string; | ||
} | ||
|
||
/** | ||
* Avatar component that displays a user's avatar with customizable size and styles. | ||
* | ||
* @param {Object} props - The properties object. | ||
* @param {User} [props.user] - The user object containing firstname, lastname, and pathToImage. | ||
* @param {"small" | "medium" | "large"} [props.size="small"] - The size of the avatar. | ||
* @param {object} [props.sx] - Additional styles to apply to the avatar. | ||
* | ||
* @returns {JSX.Element} The rendered Avatar component. | ||
*/ | ||
const Avatar = ({ | ||
user = { | ||
firstname: "F", | ||
lastname: "L", | ||
pathToImage: "", | ||
}, | ||
size = "small", | ||
sx, | ||
}: { | ||
user?: User; | ||
size?: "small" | "medium" | "large"; | ||
sx?: object; | ||
}): JSX.Element => { | ||
let dimensions = {}; | ||
if (size === "small") { | ||
dimensions = { width: 32, height: 32, fontSize: 13 }; | ||
} else if (size === "medium") { | ||
dimensions = { width: 64, height: 64, fontSize: 22 }; | ||
} else if (size === "large") { | ||
dimensions = { width: 128, height: 128, fontSize: 44 }; | ||
} | ||
|
||
return ( | ||
<VWAvatar | ||
src={user!.pathToImage} | ||
alt={`${user!.firstname} ${user!.lastname}`} | ||
component={"div"} | ||
variant="circular" // or variant="rounded" or variant="square" | ||
sx={{ | ||
backgroundColor: `#12715B`, | ||
...dimensions, | ||
border: user!.pathToImage ? `1px solid #12715B` : "none", | ||
...sx, | ||
}} | ||
> | ||
{user!.firstname.charAt(0)} | ||
{user!.lastname.charAt(0)} | ||
</VWAvatar> | ||
); | ||
}; | ||
|
||
export default Avatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export function stringToColor(firstName: string, lastName: string) { | ||
// hex numbers are 0 1 2 3 4 5 6 7 8 9 A B C D E F | ||
const lenghtOfFirstName = firstName.length; | ||
const lenghtOfLastName = lastName.length; | ||
|
||
let firstThreeDigits = ""; | ||
|
||
const firstThreeDigitsCounter = lenghtOfFirstName % 10; | ||
for ( | ||
let index = firstThreeDigitsCounter; | ||
index < 10 && firstThreeDigits.length < 3; | ||
index++ | ||
) { | ||
firstThreeDigits = firstThreeDigits.concat(index.toString()); | ||
} | ||
|
||
let secondThreeDigits = ""; | ||
|
||
const secondThreeDigitsCounter = lenghtOfLastName % 6; | ||
for (let index = secondThreeDigitsCounter; index < 6; index++) { | ||
if (index === 6) { | ||
secondThreeDigits = secondThreeDigits.concat("ABC"); | ||
} else { | ||
switch (index) { | ||
case 0: | ||
secondThreeDigits = secondThreeDigits.concat("A"); | ||
break; | ||
case 1: | ||
secondThreeDigits = secondThreeDigits.concat("B"); | ||
break; | ||
case 2: | ||
secondThreeDigits = secondThreeDigits.concat("C"); | ||
break; | ||
case 3: | ||
secondThreeDigits = secondThreeDigits.concat("D"); | ||
break; | ||
case 4: | ||
secondThreeDigits = secondThreeDigits.concat("E"); | ||
break; | ||
case 5: | ||
secondThreeDigits = secondThreeDigits.concat("F"); | ||
break; | ||
default: | ||
secondThreeDigits = secondThreeDigits.concat(index.toString()); | ||
break; | ||
} | ||
} | ||
} | ||
return "#" + firstThreeDigits + secondThreeDigits; | ||
} |