Skip to content

Commit

Permalink
Merge pull request #61 from penguinsufcg/fix/user-close-table
Browse files Browse the repository at this point in the history
Fix problem when user close the bill
  • Loading branch information
ArthurFerrao authored Dec 23, 2021
2 parents 0d0c1b8 + 3130fde commit ed945ef
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/components/Session/SessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function SessionProvider({ children }: SessionContextProps): JSX.Element {
setSessionId(undefined)
setSession(null)
setSessionRef(null)

router.push('/logout')
}

Expand Down Expand Up @@ -106,7 +106,7 @@ function SessionProvider({ children }: SessionContextProps): JSX.Element {
}, [session])

useEffect(() => {
if(session?.status === 'FINISHED') {
if (session?.status === 'FINISHED') {
closeSession()
}
}, [session])
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/components/client/Navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Flex justifyContent="space-around">
<NavbarOption isSelected icon={<BiHome size={24} />} href="/" />
<NavbarOption icon={<BiCartAlt size={24} />} href="/cart" />
<NavbarOption icon={<BiReceipt size={24} />} href="/receipt" />
<NavbarOption icon={<BiReceipt size={24} />} href={underPayment ? "/bill" : "/receipt"} />
</Flex>
)
}
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ type SessionContextProps = {
session?: EntityWithID<Session> | null
sessionRef?: Reference<Session> | null
isLogged: boolean
createNewSession?: (params: {table: string, client: string }) => Promise<string>
underPayment: boolean
createNewSession?: (params: { table: string, client: string }) => Promise<string>
joinSession?: (params: { sessionId: string }) => void
}

Expand All @@ -14,16 +15,17 @@ export const SessionContext: Context<SessionContextProps> =
session: null,
sessionRef: null,
isLogged: false,
underPayment: false,
isLoading: false,
})

const useSession = (): SessionContextProps => {
const useSession = (): SessionContextProps => {
const context = useContext<SessionContextProps>(SessionContext)

if (!context) {
throw new Error('Do not use Session outside of context')
}

return context
}

Expand Down
12 changes: 6 additions & 6 deletions src/pages/bill/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -26,15 +26,15 @@ const BillPage = () => {
newStatus: 'PAYMENT',
})

await updateSessionStatus(session.id, 'PAYMENT')

toast({
title: 'Pagamento solicitado!',
description: 'Aguarde pelo garçom :)',
status: 'success',
duration: 5000,
isClosable: true,
})

setDisableButton(true)
}

return (
Expand All @@ -45,11 +45,11 @@ const BillPage = () => {
buttonProps: {
children: 'Solicitar pagamento',
onClick: handleClick,
disabled: disableButton
disabled: underPayment
},
}}>
<Container px={10}>
<BillDetails items={receiptItems}/>
<BillDetails items={receiptItems} />
</Container>
</Layout>
)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/dish-details/[dishId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DishPage = () => {
dishId,
])
const { addItem } = useMinicart()
const { isLogged } = useSession()
const { isLogged, underPayment } = useSession()

const toast = useToast()

Expand Down Expand Up @@ -59,7 +59,7 @@ const DishPage = () => {
<ChevronLeftIcon />
</Button>
{dishData && <DishDetails dish={dishData} />}
{isLogged && <AddDishForm onSubmit={handleSubmit} />}
{isLogged && !underPayment && <AddDishForm onSubmit={handleSubmit} />}
</Flex>
)
}
Expand Down

0 comments on commit ed945ef

Please sign in to comment.