-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Facundo De Lorenzo
committed
May 17, 2024
1 parent
d2e4f12
commit 3929638
Showing
18 changed files
with
1,105 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { APIGatewayEvent, Callback, Context } from 'aws-lambda'; | ||
import { Restaurant } from './types'; | ||
|
||
const responseHeaders = { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Credentials': true, | ||
'Access-Control-Allow-Methods': 'OPTIONS,GET, POST', | ||
}; | ||
|
||
exports.get = async ( | ||
event: APIGatewayEvent, | ||
context: Context, | ||
callback: Callback, | ||
) => { | ||
try { | ||
const restaurants: Restaurant[] = [ | ||
{ id: 1, name: 'Big restaurant' }, | ||
{ id: 2, name: 'China here' }, | ||
{ id: 3, name: 'La parri argenta' }, | ||
{ id: 4, name: 'Small restaurant' }, | ||
{ id: 5, name: 'China there' }, | ||
{ id: 6, name: 'La re parri argenta' }, | ||
{ id: 7, name: 'Big restaurant' }, | ||
{ id: 8, name: 'China here' }, | ||
{ id: 9, name: 'La parri argenta' }, | ||
{ id: 10, name: 'Small restaurant' }, | ||
{ id: 11, name: 'China there' }, | ||
{ id: 12, name: 'La re parri argenta' }, | ||
]; | ||
|
||
return { | ||
isBase64Encoded: false, | ||
statusCode: 200, | ||
headers: responseHeaders, | ||
body: JSON.stringify(restaurants), | ||
}; | ||
} catch (error) { | ||
return { | ||
isBase64Encoded: false, | ||
statusCode: 500, | ||
headers: responseHeaders, | ||
body: error.message, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
export interface CreateOrderRequest { | ||
products: Product[]; | ||
} | ||
|
||
export interface Product { | ||
name: string; | ||
price: number; | ||
} | ||
|
||
export interface Order { | ||
products: Product[]; | ||
totalPrice: number; | ||
} | ||
products: Product[]; | ||
} | ||
export interface OrderProduct { | ||
product: Product; | ||
amount: number; | ||
} | ||
|
||
export interface Product { | ||
id: number; | ||
name: string; | ||
price: number; | ||
} | ||
|
||
export interface Order { | ||
products: Product[]; | ||
totalPrice: number; | ||
} | ||
|
||
export interface Restaurant { | ||
id: number; | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
import styled from "styled-components"; | ||
import useGetProducts from "./components/app/hooks/useGetProducts"; | ||
import { | ||
createBrowserRouter, | ||
RouterProvider, | ||
Route, | ||
createRoutesFromElements, | ||
} from "react-router-dom"; | ||
import Products from "./pages/Products"; | ||
import { CurrentOrderProvider } from "./components/app/contexts/useCurrentOrderProvider"; | ||
import Restaurants from "./pages/Restaurant"; | ||
|
||
const App = () => { | ||
const products = useGetProducts(); | ||
const routes = [ | ||
<Route path="/" element={<Restaurants />} />, | ||
<Route path="/:restaurant/products/" element={<Products />} />, | ||
]; | ||
const router2 = createBrowserRouter(createRoutesFromElements(routes)); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
width: "100%", | ||
alignItems: "center", | ||
}} | ||
> | ||
<Title>This is a title</Title> | ||
<div>{products.length > 0 && "hay productos"}</div> | ||
<div>{products.length === 0 && "No hay productos :("}</div> | ||
<img height={600} /> | ||
</div> | ||
<CurrentOrderProvider> | ||
<RouterProvider router={router2} /> | ||
</CurrentOrderProvider> | ||
); | ||
}; | ||
|
||
export default App; | ||
|
||
const Title = styled.a` | ||
font-size: 2rem; | ||
`; |
15 changes: 15 additions & 0 deletions
15
packages/front/src/components/app/contexts/useCurrentOrderContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createContext, useContext } from "react"; | ||
import { OrderProduct } from "../hooks/useCreateOrder"; | ||
|
||
interface CurrentOrderContextType { | ||
orderProducts: OrderProduct[]; | ||
setOrderProducts: React.Dispatch<React.SetStateAction<OrderProduct[]>>; | ||
} | ||
export const CurrentOrderContext = createContext<CurrentOrderContextType>({ | ||
orderProducts: [], | ||
setOrderProducts: () => {}, | ||
} as CurrentOrderContextType); | ||
|
||
export const useCurrentOrderContext = () => { | ||
return useContext(CurrentOrderContext); | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/front/src/components/app/contexts/useCurrentOrderProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ReactNode, useState } from "react"; | ||
import { CurrentOrderContext } from "./useCurrentOrderContext"; | ||
import { OrderProduct } from "../hooks/useCreateOrder"; | ||
|
||
export interface ICurrentOrderProvider { | ||
children: ReactNode; | ||
} | ||
|
||
export const CurrentOrderProvider = ({ children }: ICurrentOrderProvider) => { | ||
const [orderProducts, setOrderProducts] = useState<OrderProduct[]>([]); | ||
|
||
return ( | ||
<CurrentOrderContext.Provider value={{ orderProducts, setOrderProducts }}> | ||
{children} | ||
</CurrentOrderContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Product } from "./useGetProducts"; | ||
import { useCurrentOrderContext } from "../contexts/useCurrentOrderContext"; | ||
|
||
export interface Order { | ||
products: OrderProduct[]; | ||
totalPrice: number; | ||
} | ||
export interface OrderProduct { | ||
product: Product; | ||
amount: number; | ||
} | ||
export interface CreateOrderRequest { | ||
products: OrderProduct[]; | ||
} | ||
|
||
const useCreateOrder = () => { | ||
const { orderProducts, setOrderProducts } = useCurrentOrderContext(); | ||
|
||
const alterProduct = ({ product, amount }: OrderProduct) => { | ||
let productToAdd: OrderProduct | undefined = undefined; | ||
if (amount !== 0) productToAdd = { product, amount }; | ||
setOrderProducts((value) => [ | ||
...value | ||
.filter((x) => x.product.id !== product.id) | ||
.concat(productToAdd ? [productToAdd] : []), | ||
]); | ||
}; | ||
const createOrder = () => { | ||
if (orderProducts.length > 0) { | ||
console.log("order products: ", orderProducts); | ||
//Execute create order | ||
} | ||
}; | ||
const resetFlow = () => { | ||
setOrderProducts([]); | ||
}; | ||
|
||
return { alterProduct, createOrder, orderProducts, resetFlow }; | ||
}; | ||
|
||
export default useCreateOrder; |
Oops, something went wrong.