Skip to content

Commit

Permalink
fix: get photo dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 6, 2022
1 parent afcdea3 commit c193d3e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/hooks/useDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { useEffect, useRef, useState } from 'react';

import { getPhoto } from 'services/get/photo';

import { useAuth } from './useAuth';

export function useDropdown() {
const { user } = useAuth();
const [activeMenu, setActiveMenu] = useState('main');
const [menuHeight, setMenuHeight] = useState<any>(null);
const [photo, setPhoto] = useState('');
const [name, setName] = useState('');
const dropdownRef = useRef<any>(null);

function calcHeight(el: { offsetHeight: any }) {
Expand All @@ -14,12 +21,34 @@ export function useDropdown() {
setMenuHeight(dropdownRef.current?.firstChild.offsetHeight + 30);
}, []);

async function getPhotoUser(id: string) {
const { data, error, status } = await getPhoto(id);

if (error) {
switch (status) {
default:
throw new Error('Erro ao buscar informações do usuário');
}
}

if (!data) return;

setPhoto(data[0].j[0].src);
setName(data[0].j[0].name);
}

useEffect(() => {
getPhotoUser(user?.id || '');
}, []);

return {
activeMenu,
setActiveMenu,
menuHeight,
setMenuHeight,
dropdownRef,
calcHeight,
photo,
name,
};
}

0 comments on commit c193d3e

Please sign in to comment.