-
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
1 parent
76ee9d7
commit 4c5c4b4
Showing
15 changed files
with
309 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import LoadMoreProducts from "./hooks/loadMoreProducts" | ||
import CartSession from "./hooks/cartSession" | ||
|
||
let Hooks = { | ||
LoadMoreProducts: LoadMoreProducts | ||
LoadMoreProducts: LoadMoreProducts, | ||
CartSession: CartSession | ||
} | ||
|
||
export default Hooks |
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,10 @@ | ||
const CartSession = { | ||
mounted(){ | ||
this.handleEvent("create_cart_session_id", map => { | ||
var {cart_id: cart_id} = map; | ||
sessionStorage.setItem("cart_id", cart_id); | ||
}) | ||
} | ||
} | ||
|
||
export default CartSession; |
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
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,48 @@ | ||
defmodule FoodOrderWeb.Middlewares.CartSession do | ||
import Phoenix.LiveView, only: [get_connect_params: 1, push_event: 3] | ||
import Phoenix.Component | ||
alias FoodOrder.Accounts | ||
alias FoodOrder.Carts | ||
|
||
def on_mount(:default, _, params, socket) do | ||
cart_id = get_connect_params(socket)["cart_id"] | ||
|
||
socket = | ||
socket | ||
|> assign_user(params["user_token"]) | ||
|> create_cart(cart_id) | ||
|
||
{:cont, socket} | ||
end | ||
|
||
defp assign_user(socket, nil), do: assign(socket, :current_user, nil) | ||
|
||
defp assign_user(socket, user_token) do | ||
assign_new(socket, :current_user, fn -> Accounts.get_user_by_session_token(user_token) end) | ||
end | ||
|
||
defp create_cart(socket, cart_id) do | ||
current_user = socket.assigns.current_user | ||
cart_id = build_cart(current_user, cart_id) | ||
|
||
socket | ||
|> assign(cart_id: cart_id) | ||
|> push_event("create_cart_session_id", %{cart_id: cart_id}) | ||
end | ||
|
||
defp build_cart(nil, nil) do | ||
cart_id = Ecto.UUID.generate() | ||
Carts.create(cart_id) | ||
cart_id | ||
end | ||
|
||
defp build_cart(nil, cart_id) do | ||
Carts.create(cart_id) | ||
cart_id | ||
end | ||
|
||
defp build_cart(%{id: cart_id}, _) do | ||
Carts.create(cart_id) | ||
cart_id | ||
end | ||
end |
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,26 +1,28 @@ | ||
<section class="py-16" data-role="hero"> | ||
<div class="container mx-auto flex flex-col md:flex-row items-center justify-between"> | ||
<div data-role="hero-cta"> | ||
<h6 class="text-lg">Make your order</h6> | ||
<h1 class="text-3xl font-bold">Right now!</h1> | ||
<button class="px-6 bg-fuchsia-500 rounded-full py-2 text-white mt-5 fond-bold">Order now</button> | ||
<div id="cart-session" phx-hook="CartSession"> | ||
<section class="py-16" data-role="hero"> | ||
<div class="container mx-auto flex flex-col md:flex-row items-center justify-between"> | ||
<div data-role="hero-cta"> | ||
<h6 class="text-lg">Make your order</h6> | ||
<h1 class="text-3xl font-bold">Right now!</h1> | ||
<button class="px-6 bg-fuchsia-500 rounded-full py-2 text-white mt-5 fond-bold">Order now</button> | ||
</div> | ||
<img src={~p"/images/hamburger.svg"} alt="" data-role="hero-img"> | ||
</div> | ||
<img src={~p"/images/hamburger.svg"} alt="" data-role="hero-img"> | ||
</div> | ||
</section> | ||
</section> | ||
|
||
<section class="container mx-auto py-8" data-role="products-section"> | ||
<h1 class="text-lg font-bold mb-8">All foods</h1> | ||
<section class="container mx-auto py-8" data-role="products-section"> | ||
<h1 class="text-lg font-bold mb-8">All foods</h1> | ||
|
||
<div | ||
class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8" | ||
data-role="products-list" | ||
id="products-list" | ||
phx-update="append" | ||
> | ||
<.live_component :for={product <- @products} module={Item}, id={product.id} product={product} /> | ||
</div> | ||
<div | ||
class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8" | ||
data-role="products-list" | ||
id="products-list" | ||
phx-update="append" | ||
> | ||
<.live_component :for={product <- @products} module={Item} id={product.id} product={product} cart_id={@cart_id} /> | ||
</div> | ||
|
||
</section> | ||
</section> | ||
|
||
<div id="load_more_products" phx-hook="LoadMoreProducts"></div> | ||
<div id="load_more_products" phx-hook="LoadMoreProducts"></div> | ||
</div> |
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
Oops, something went wrong.