Show total price.

This commit is contained in:
Dillon Kearns 2022-05-24 20:30:49 -07:00
parent 86f60f23f2
commit a3ab4dec8f

View File

@ -90,7 +90,7 @@ subscriptions maybePageUrl routeParams path sharedModel model =
type alias Data = type alias Data =
{ smoothies : List Smoothie { smoothies : List Smoothie
, cart : Maybe (Dict String Int) , cart : Maybe (Dict String CartEntry)
} }
@ -187,7 +187,11 @@ addItemToCart quantity userId itemId =
SelectionSet.empty SelectionSet.empty
cartSelection : SelectionSet (Maybe (Dict String Int)) RootQuery type alias CartEntry =
{ quantity : Int, pricePerItem : Int }
cartSelection : SelectionSet (Maybe (Dict String CartEntry)) RootQuery
cartSelection = cartSelection =
Api.Query.users_by_pk { id = Uuid "2500fcdc-737b-4126-96c2-b3aae64cb5c4" } Api.Query.users_by_pk { id = Uuid "2500fcdc-737b-4126-96c2-b3aae64cb5c4" }
(Api.Object.Users.orders (Api.Object.Users.orders
@ -213,7 +217,10 @@ cartSelection =
(Api.Object.Order.order_items identity (Api.Object.Order.order_items identity
(SelectionSet.map2 Tuple.pair (SelectionSet.map2 Tuple.pair
(Api.Object.Order_item.product_id |> SelectionSet.map uuidToString) (Api.Object.Order_item.product_id |> SelectionSet.map uuidToString)
Api.Object.Order_item.quantity (SelectionSet.map2 CartEntry
Api.Object.Order_item.quantity
(Api.Object.Order_item.product Api.Object.Products.price)
)
) )
) )
) )
@ -249,7 +256,20 @@ view :
view maybeUrl sharedModel model app = view maybeUrl sharedModel model app =
{ title = "Ctrl-R Smoothies" { title = "Ctrl-R Smoothies"
, body = , body =
[ cartView (app.data.cart |> Maybe.withDefault Dict.empty |> Dict.foldl (\_ quantity soFar -> soFar + quantity) 0) let
totals =
app.data.cart
|> Maybe.withDefault Dict.empty
|> Dict.foldl
(\_ { quantity, pricePerItem } soFar ->
{ soFar
| totalItems = soFar.totalItems + quantity
, totalPrice = soFar.totalPrice + (quantity * pricePerItem)
}
)
{ totalItems = 0, totalPrice = 0 }
in
[ cartView totals
, app.data.smoothies , app.data.smoothies
|> List.map |> List.map
(productView app.data.cart) (productView app.data.cart)
@ -258,11 +278,11 @@ view maybeUrl sharedModel model app =
} }
cartView : Int -> Html msg cartView : { totalItems : Int, totalPrice : Int } -> Html msg
cartView itemsInCart = cartView totals =
Html.button [ Attr.class "checkout" ] Html.button [ Attr.class "checkout" ]
[ Html.span [ Attr.class "icon" ] [ Icon.cart ] [ Html.span [ Attr.class "icon" ] [ Icon.cart ]
, Html.text <| " Checkout (" ++ String.fromInt itemsInCart ++ ")" , Html.text <| " Checkout (" ++ String.fromInt totals.totalItems ++ ") $" ++ String.fromInt totals.totalPrice
] ]
@ -271,7 +291,7 @@ uuidToString (Uuid id) =
id id
productView : Maybe (Dict String Int) -> Smoothie -> Html (Pages.Msg.Msg msg) productView : Maybe (Dict String CartEntry) -> Smoothie -> Html (Pages.Msg.Msg msg)
productView cart item = productView cart item =
let let
quantityInCart : Int quantityInCart : Int
@ -279,12 +299,14 @@ productView cart item =
cart cart
|> Maybe.withDefault Dict.empty |> Maybe.withDefault Dict.empty
|> Dict.get (uuidToString item.id) |> Dict.get (uuidToString item.id)
|> Maybe.map .quantity
|> Maybe.withDefault 0 |> Maybe.withDefault 0
in in
Html.li [ Attr.class "item" ] Html.li [ Attr.class "item" ]
[ Html.div [] [ Html.div []
[ Html.h3 [] [ Html.text item.name ] [ Html.h3 [] [ Html.text item.name ]
, Html.p [] [ Html.text item.description ] , Html.p [] [ Html.text item.description ]
, Html.p [] [ "$" ++ String.fromInt item.price |> Html.text ]
] ]
, Html.form , Html.form
[ Attr.method "POST" [ Attr.method "POST"