Skip to content

Commit

Permalink
fix: barber receiving unapproved message
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed May 30, 2022
1 parent fe0ff00 commit 5570068
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/components/DropdownMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useDropdown } from 'hooks/useDropdown';
import styles from './DropdownMenu.module.scss';

export function DropdownMenu() {
const { user, isAlexander, isBarbeiro, isBarbeiroApproved } = useAuth();
const { user, isAlexander, isBarbeiro } = useAuth();
const { activeMenu, dropdownRef, calcHeight, menuHeight, photo } = useDropdown();

return (
Expand Down Expand Up @@ -40,7 +40,7 @@ export function DropdownMenu() {
</DropdownItem>

{isAlexander && <DropdownItem link="/admin">Painel Admin</DropdownItem>}
{isBarbeiro && isBarbeiroApproved && <DropdownItem link="/horarios">Atualizar horários</DropdownItem>}
{isBarbeiro && <DropdownItem link="/horarios">Atualizar horários</DropdownItem>}

<DropdownItem logout>Sair</DropdownItem>
</div>
Expand Down
39 changes: 0 additions & 39 deletions src/contexts/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useFormik } from 'formik';
import { AuthContextProps } from 'types/IContext';
import { loginSchema } from 'validations/Login';

import { getBarbeiro } from 'services/get/barbeiros';
import { getPhoto } from 'services/get/photo';
import { getUser } from 'services/get/user';
import { signIn } from 'services/post/signIn';
Expand All @@ -20,7 +19,6 @@ export function AuthProvider({ children }: any) {
const [user, setUser] = useState<any>();
const [ocupacao, setOcupacao] = useState('cliente');
const [loading, setLoading] = useState(false);
const [approved, setApproved] = useState('');

function isSigned() {
const storagedUser = localStorage.getItem('supabase.auth.token');
Expand Down Expand Up @@ -56,14 +54,6 @@ export function AuthProvider({ children }: any) {
return false;
}

function isBarbeiroApproved() {
if (approved === '') {
return false;
}

return true;
}

async function checkUser() {
const hash = window.location.href.split('#')[0];
const params = hash.split('/')[3];
Expand Down Expand Up @@ -116,30 +106,6 @@ export function AuthProvider({ children }: any) {
}
}

async function verificarStatusBarbeiro() {
const storagedUser = JSON.parse(localStorage.getItem('supabase.auth.token') || '{}');

const id = storagedUser?.currentSession?.user.id;

const { data, status, error } = await getBarbeiro(id, true);

if (error) {
switch (status) {
default:
return;
}
}

if (!data) return;
if (!data[0].j) return;

if (data[0].j === null) {
return;
}

setApproved(data[0].j[0].admin_confirmed);
}

useEffect(() => {
checkUser();

Expand All @@ -148,10 +114,6 @@ export function AuthProvider({ children }: any) {
});
}, []);

useEffect(() => {
verificarStatusBarbeiro();
}, []);

const formikLogin = useFormik({
initialValues: {
email: '',
Expand Down Expand Up @@ -243,7 +205,6 @@ export function AuthProvider({ children }: any) {
isBarbeiro: isBarbeiro(),
isCliente: isCliente(),
isAlexander: isAlexander(),
isBarbeiroApproved: isBarbeiroApproved(),
}}
>
{children}
Expand Down
51 changes: 34 additions & 17 deletions src/hooks/useBarbeiro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import * as XLSX from 'xlsx';

import { useUser } from 'contexts/User';

import { getBarbeiro } from 'services/get/barbeiros';
import { getClientesMonth } from 'services/get/clientes';

const THIRTYMINUTES = 30 * 60 * 1000;
const ONE_MONTH = 30;
const ONE_DAY = 1;
const pastMonth = new Date();

Expand All @@ -21,6 +21,7 @@ export function useBarbeiro() {
const [visible, setVisible] = useState(true);
const [modalIsOpen, setIsOpen] = useState(false);
const [date, setDate] = useState(new Date());
const [approved, setApproved] = useState('');
const [ultimaAtualizacao, setUltimaAtualizacao] = useState(format(new Date(), 'HH:mm:ss'));
const [dataExport, setDataExport] = useState<ClienteMetadata[]>([]);
const defaultSelected: DateRange = {
Expand All @@ -32,6 +33,28 @@ export function useBarbeiro() {
const dataInicial = format(range?.from as Date, 'yyyy-MM-dd');
const dataFinal = format(range?.to as Date, 'yyyy-MM-dd');

async function verificarStatusBarbeiro() {
console.log('id ->', clientId);

const { data, status, error } = await getBarbeiro(clientId as string, true);

if (error) {
switch (status) {
default:
return;
}
}

if (!data) return;
if (!data[0].j) return;

if (data[0].j === null) {
return;
}

setApproved(data[0].j[0].admin_confirmed);
}

async function buscarDadosParaExcel() {
const { data, error, status } = await getClientesMonth(clientId || '', dataInicial, dataFinal);

Expand Down Expand Up @@ -77,6 +100,14 @@ export function useBarbeiro() {
setIsOpen(false);
}

function isBarbeiroApproved() {
if (approved === 'S') {
return true;
} else {
return false;
}
}

useEffect(() => {
const timerID = setInterval(() => tick(), 1000);

Expand Down Expand Up @@ -128,22 +159,7 @@ export function useBarbeiro() {
}, []);

useEffect(() => {
if (
(Cookies.get('barbeiro_modal') === 'false' && Cookies.get('barbeiro_warning') === 'true') ||
(Cookies.get('barbeiro_modal') === undefined && Cookies.get('barbeiro_warning') === 'true')
) {
Swal.fire({
title: 'Novidade no ar!',
html: 'Agora você pode fazer relatórios do mês. Basta clicar no botão "Download do mês" e selecionar o periodo desejado.',
icon: 'info',
confirmButtonColor: '#ff9000',
background: '#312e38',
color: '#f4ede8',
confirmButtonText: 'Entendi!',
}).then(() => {
Cookies.set('barbeiro_modal', 'true', { expires: ONE_MONTH });
});
}
verificarStatusBarbeiro();
}, []);

const customStyles = {
Expand Down Expand Up @@ -183,5 +199,6 @@ export function useBarbeiro() {
setRange,
exportToExcel,
pastMonth,
isBarbeiroApproved: isBarbeiroApproved(),
};
}
3 changes: 1 addition & 2 deletions src/pages/Barbeiro/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import { getDiaSemana } from 'utils/diaDaSemana';

import { useUser } from 'contexts/User';

import { useAuth } from 'hooks/useAuth';
import { useBarbeiro } from 'hooks/useBarbeiro';

import styles from './Barbeiro.module.scss';

export function Barbeiro() {
const navigate = useNavigate();
const { isBarbeiroApproved } = useAuth();
const {
clientes,
selectDay,
Expand All @@ -47,6 +45,7 @@ export function Barbeiro() {
setRange,
exportToExcel,
pastMonth,
isBarbeiroApproved,
} = useBarbeiro();

return (
Expand Down
1 change: 0 additions & 1 deletion src/types/IContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export interface AuthContextProps {
isBarbeiro: boolean;
isCliente: boolean;
isAlexander: boolean;
isBarbeiroApproved: boolean;
}

export interface FormikProps {
Expand Down

0 comments on commit 5570068

Please sign in to comment.