Skip to content

Commit

Permalink
feat: add function isbarbeiro and iscliente
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Apr 5, 2022
1 parent b97d00f commit 0e89d9d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/contexts/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ export function AuthProvider({ children }: any) {
return false;
}

function isBarbeiro() {
if (user?.user_metadata?.ocupacao === 'barbeiro') {
return true;
}

return false;
}

function isCliente() {
if (user?.user_metadata?.ocupacao === 'cliente') {
return true;
}

return false;
}

async function checkUser() {
const hash = window.location.href.split('#')[0];
const params = hash.split('/')[3];
Expand Down Expand Up @@ -173,6 +189,8 @@ export function AuthProvider({ children }: any) {
ocupacao,
setOcupacao,
loading,
isBarbeiro: isBarbeiro(),
isCliente: isCliente(),
}}
>
{children}
Expand Down
25 changes: 15 additions & 10 deletions src/contexts/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function UserProvider({ children }: any) {

const { pathname } = window.location;

const { user } = useAuth();
const { user, isCliente, isBarbeiro } = useAuth();
const [barbeiro, setBarbeiro] = useState<UserMetadata>();
const [barbeiros, setBarbeiros] = useState<UserMetadata[]>([]);
const [clientes, setClientes] = useState<ClienteMetadata[]>([]);
Expand All @@ -29,7 +29,6 @@ export function UserProvider({ children }: any) {
const clientId = user?.id;
const barberId = barbeiro?.id;

const ocupacao = user?.user_metadata.ocupacao;
const selectDayFormatted = format(selectDay, 'yyyy-MM-dd');
const atualDayFormatted = format(new Date(), 'yyyy-MM-dd');
const selectDayFormattedBR = format(selectDay, 'dd/MM/yyyy');
Expand Down Expand Up @@ -169,8 +168,8 @@ export function UserProvider({ children }: any) {
async function buscarClientes() {
if (!clientId) return;

if (ocupacao === 'barbeiro') {
const { data, error, status } = await getClientes(clientId || '', selectDayFormatted);
if (isBarbeiro) {
const { data, error, status } = await getClientes(clientId, selectDayFormatted);

if (error) {
switch (status) {
Expand All @@ -189,7 +188,7 @@ export function UserProvider({ children }: any) {
setClientes(data[0].j);
}

if (ocupacao === 'cliente' && barberId) {
if (isCliente && barberId) {
const { data, error, status } = await getClientes(barberId || '', selectDayFormatted);

if (error) {
Expand Down Expand Up @@ -273,16 +272,22 @@ export function UserProvider({ children }: any) {
}

useEffect(() => {
buscarBarbeiros();
}, []);
if (isCliente) {
buscarBarbeiros();
}
}, [isCliente]);

useEffect(() => {
buscarAgendamentos();
if (isCliente) {
buscarAgendamentos();
}
}, [clientId]);

useEffect(() => {
buscarClientes();
}, [barberId, selectDay]);
if (isBarbeiro) {
buscarClientes();
}
}, [barberId, selectDay, isBarbeiro]);

useEffect(() => {
const params = pathname.split('/')[1];
Expand Down

0 comments on commit 0e89d9d

Please sign in to comment.