From 3130fde83b0f48f619cbea6df3c7ffda60452001 Mon Sep 17 00:00:00 2001 From: arthurferrao Date: Wed, 22 Dec 2021 21:53:06 -0300 Subject: [PATCH] Fix problem when user close the bill --- src/components/Session/SessionProvider.tsx | 5 +++-- src/components/client/Navbar/index.tsx | 4 +++- src/hooks/useSession.ts | 8 +++++--- src/pages/bill/index.tsx | 12 ++++++------ src/pages/dish-details/[dishId].tsx | 4 ++-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/components/Session/SessionProvider.tsx b/src/components/Session/SessionProvider.tsx index 46b4c00..c5b3c95 100644 --- a/src/components/Session/SessionProvider.tsx +++ b/src/components/Session/SessionProvider.tsx @@ -26,7 +26,7 @@ function SessionProvider({ children }: SessionContextProps): JSX.Element { setSessionId(undefined) setSession(null) setSessionRef(null) - + router.push('/logout') } @@ -106,7 +106,7 @@ function SessionProvider({ children }: SessionContextProps): JSX.Element { }, [session]) useEffect(() => { - if(session?.status === 'FINISHED') { + if (session?.status === 'FINISHED') { closeSession() } }, [session]) @@ -116,6 +116,7 @@ function SessionProvider({ children }: SessionContextProps): JSX.Element { session, sessionRef, isLogged: sessionId && sessionId !== 'undefined' ? true : false, + underPayment: session?.status == 'PAYMENT', isLoading, createNewSession, joinSession, diff --git a/src/components/client/Navbar/index.tsx b/src/components/client/Navbar/index.tsx index fed532a..0dc4049 100644 --- a/src/components/client/Navbar/index.tsx +++ b/src/components/client/Navbar/index.tsx @@ -1,14 +1,16 @@ +import useSession from "@/hooks/useSession" import { Flex } from "@chakra-ui/react" import React from "react" import { BiHome, BiCartAlt, BiReceipt } from "react-icons/bi" import NavbarOption from "./components/NavbarOption" const Navbar = () => { + const { underPayment } = useSession() return ( } href="/" /> } href="/cart" /> - } href="/receipt" /> + } href={underPayment ? "/bill" : "/receipt"} /> ) } diff --git a/src/hooks/useSession.ts b/src/hooks/useSession.ts index e0066c7..522a351 100644 --- a/src/hooks/useSession.ts +++ b/src/hooks/useSession.ts @@ -5,7 +5,8 @@ type SessionContextProps = { session?: EntityWithID | null sessionRef?: Reference | null isLogged: boolean - createNewSession?: (params: {table: string, client: string }) => Promise + underPayment: boolean + createNewSession?: (params: { table: string, client: string }) => Promise joinSession?: (params: { sessionId: string }) => void } @@ -14,16 +15,17 @@ export const SessionContext: Context = session: null, sessionRef: null, isLogged: false, + underPayment: false, isLoading: false, }) -const useSession = (): SessionContextProps => { +const useSession = (): SessionContextProps => { const context = useContext(SessionContext) if (!context) { throw new Error('Do not use Session outside of context') } - + return context } diff --git a/src/pages/bill/index.tsx b/src/pages/bill/index.tsx index 6c1de66..f7fcd10 100644 --- a/src/pages/bill/index.tsx +++ b/src/pages/bill/index.tsx @@ -5,12 +5,12 @@ import useSessionReceipt from '@/hooks/useSessionReceipt' import { useToast } from '@chakra-ui/react' import { Container } from '@chakra-ui/layout' import { updateTableStatus } from '@/api/tables' +import { updateSessionStatus } from '@/api/session' import Layout from '@/components/client/Layout' import BillDetails from '@/components/BillDetails' const BillPage = () => { - const [ disableButton, setDisableButton ] = useState(false) - const { session, sessionRef } = useSession() + const { session, sessionRef, underPayment } = useSession() const { items: receiptItems, total: billTotal } = useSessionReceipt({ sessionRef, }) @@ -26,6 +26,8 @@ const BillPage = () => { newStatus: 'PAYMENT', }) + await updateSessionStatus(session.id, 'PAYMENT') + toast({ title: 'Pagamento solicitado!', description: 'Aguarde pelo garçom :)', @@ -33,8 +35,6 @@ const BillPage = () => { duration: 5000, isClosable: true, }) - - setDisableButton(true) } return ( @@ -45,11 +45,11 @@ const BillPage = () => { buttonProps: { children: 'Solicitar pagamento', onClick: handleClick, - disabled: disableButton + disabled: underPayment }, }}> - + ) diff --git a/src/pages/dish-details/[dishId].tsx b/src/pages/dish-details/[dishId].tsx index 9f64858..bd640e4 100644 --- a/src/pages/dish-details/[dishId].tsx +++ b/src/pages/dish-details/[dishId].tsx @@ -16,7 +16,7 @@ const DishPage = () => { dishId, ]) const { addItem } = useMinicart() - const { isLogged } = useSession() + const { isLogged, underPayment } = useSession() const toast = useToast() @@ -59,7 +59,7 @@ const DishPage = () => { {dishData && } - {isLogged && } + {isLogged && !underPayment && } ) }