Skip to content

Commit

Permalink
Merge pull request #72 from AJ-Quran/71-overloading-avatar
Browse files Browse the repository at this point in the history
Overloading Avatar
  • Loading branch information
akbarjorayev authored Apr 26, 2024
2 parents 2037f36 + abb7c09 commit 742d0ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { msgData } from '../../../../../../js/utils/message'
import { elText } from '../../../../../../js/utils/copy'
import { load } from '../../../../../../js/db/db'
import { getImgBlob } from '../../../../../../js/utils/img'
import { avatars } from '../utils/getAvatar'

import './AccountData.css'

Expand Down Expand Up @@ -77,6 +78,8 @@ export default function AccountData() {
const userNewData = { ...formData, img: { img: profileImg } }
const editedData = await editUser(username, userNewData)

avatars.get(username).img = profileImg

if (!editedData.ok) {
setMessage({ msg: editedData.msg, type: editedData.msgType, show: true })
setTimeout(
Expand Down
16 changes: 2 additions & 14 deletions src/pages/Main/components/Account/components/utils/Avatar.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { useEffect, useState } from 'react'

import { load } from '../../../../../../js/db/db'
import { loadLocal } from '../../../../../../js/db/localStorage'
import { getAvatar } from './getAvatar'

import './Avatar.css'

async function getUserAvatar(username) {
username = username ? username : loadLocal('quran').accounts.active

const img = await load(`accounts/${username}/user/img/img`)
const name = await load(`accounts/${username}/user/name`)

return { name: name[0], img }
}

export default function Avatar({ style, username, children, ...props }) {
const [data, setData] = useState({ name: '', img: '' })

useEffect(() => {
async function loadData() {
const userAvatar = username
? await getUserAvatar(username)
: await getUserAvatar()
const userAvatar = await getAvatar(username)
setData(userAvatar)
}
loadData()
Expand Down
18 changes: 18 additions & 0 deletions src/pages/Main/components/Account/components/utils/getAvatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { load } from '../../../../../../js/db/db'
import { loadLocal } from '../../../../../../js/db/localStorage'

const avatars = new Map()
export { avatars }

export async function getAvatar(username) {
username = username ? username : loadLocal('quran').accounts.active
if (avatars.get(username)) return avatars.get(username)

const img = await load(`accounts/${username}/user/img/img`)
const name = await load(`accounts/${username}/user/name`)

const avatar = { name: name[0], img }

avatars.set(username, avatar)
return avatar
}

0 comments on commit 742d0ac

Please sign in to comment.