aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/Cart.elm
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-26 05:21:46 +0000
committerAkshay <[email protected]>2020-12-26 05:21:46 +0000
commit8014def1a8da3397d78d1162f9e1b8c3f22d0322 (patch)
tree346e1de0ac6aa4ca973c1b3e5897c2c44948e5a8 /frontend/src/Cart.elm
parent7c6006e1abc6094b5922ab69ccfa5449b8dbbc99 (diff)
add transactions and quantities
- backend exposes endpoints to perform transactions - frontend introduces a new page to "checkout" cart
Diffstat (limited to 'frontend/src/Cart.elm')
-rw-r--r--frontend/src/Cart.elm76
1 files changed, 63 insertions, 13 deletions
diff --git a/frontend/src/Cart.elm b/frontend/src/Cart.elm
index a1750f6..58fb72e 100644
--- a/frontend/src/Cart.elm
+++ b/frontend/src/Cart.elm
@@ -21,9 +21,15 @@ type alias Product =
21 } 21 }
22 22
23 23
24type alias CartListing =
25 { productItem : Product
26 , quantity : Int
27 }
28
29
24type alias Model = 30type alias Model =
25 { pageStatus : Status 31 { pageStatus : Status
26 , products : List Product 32 , products : List CartListing
27 } 33 }
28 34
29 35
@@ -34,10 +40,12 @@ type Status
34 40
35 41
36type Msg 42type Msg
37 = CartLoaded (Result Http.Error (List Product)) 43 = CartLoaded (Result Http.Error (List CartListing))
38 | FetchCartItems 44 | FetchCartItems
39 | RemoveFromCart Int 45 | RemoveFromCart Int
40 | CartItemRemoved (Result Http.Error ()) 46 | CartItemRemoved (Result Http.Error ())
47 | AddToCartSuccess (Result Http.Error ())
48 | AddToCartPressed Int
41 49
42 50
43init : Model 51init : Model
@@ -69,6 +77,12 @@ update msg model =
69 FetchCartItems -> 77 FetchCartItems ->
70 ( { model | pageStatus = Loading }, fetchCartItems ) 78 ( { model | pageStatus = Loading }, fetchCartItems )
71 79
80 AddToCartPressed id ->
81 ( model, addToCart id )
82
83 AddToCartSuccess _ ->
84 ( { model | pageStatus = Loading }, fetchCartItems )
85
72 86
73decodeProduct : D.Decoder Product 87decodeProduct : D.Decoder Product
74decodeProduct = 88decodeProduct =
@@ -80,9 +94,13 @@ decodeProduct =
80 (D.field "description" (D.nullable D.string)) 94 (D.field "description" (D.nullable D.string))
81 95
82 96
83decodeResponse : D.Decoder (List Product) 97decodeResponse : D.Decoder (List CartListing)
84decodeResponse = 98decodeResponse =
85 D.list decodeProduct 99 D.list
100 (D.map2 CartListing
101 (D.field "product_item" decodeProduct)
102 (D.field "quantity" D.int)
103 )
86 104
87 105
88removeProduct : Int -> Cmd Msg 106removeProduct : Int -> Cmd Msg
@@ -132,15 +150,45 @@ viewStatus s =
132 "Not loaded ..." 150 "Not loaded ..."
133 151
134 152
135viewProduct : Product -> Html Msg 153addToCart : Int -> Cmd Msg
136viewProduct p = 154addToCart id =
155 let
156 _ =
157 Debug.log "err" <| "adding to cart: " ++ String.fromInt id
158 in
159 Http.riskyRequest
160 { method = "POST"
161 , headers = []
162 , url = "http://127.0.0.1:7878/cart/add"
163 , body = Http.stringBody "applcation/json" <| String.fromInt <| id
164 , expect = Http.expectWhatever AddToCartSuccess
165 , timeout = Nothing
166 , tracker = Nothing
167 }
168
169
170calculateTotal : Model -> Float
171calculateTotal model =
172 let
173 items =
174 model.products
175 in
176 items
177 |> List.map (\i -> toFloat i.quantity * i.productItem.price)
178 |> List.foldl (+) 0
179
180
181viewCartItemListing : CartListing -> Html Msg
182viewCartItemListing listing =
137 div [] 183 div []
138 [ text p.name 184 [ text listing.productItem.name
139 , div [] [ text <| Maybe.withDefault "" p.kind ] 185 , div [] [ text <| Maybe.withDefault "" listing.productItem.kind ]
140 , div [] [ text <| Maybe.withDefault "" p.description ] 186 , div [] [ text <| Maybe.withDefault "" listing.productItem.description ]
141 , div [] [ text <| String.fromFloat p.price ] 187 , div [] [ text <| String.fromFloat listing.productItem.price ]
142 , div [] [ button [ onClick (RemoveFromCart p.id) ] [ text "Remove" ] ] 188 , div [] [ text <| String.fromInt listing.quantity ]
143 , div [] [ a [ href ("/product/" ++ String.fromInt p.id) ] [ text "View Product" ] ] 189 , div [] [ button [ onClick (AddToCartPressed listing.productItem.id) ] [ text "Add" ] ]
190 , div [] [ button [ onClick (RemoveFromCart listing.productItem.id) ] [ text "Remove" ] ]
191 , div [] [ a [ href ("/product/" ++ String.fromInt listing.productItem.id) ] [ text "View Product" ] ]
144 ] 192 ]
145 193
146 194
@@ -154,11 +202,13 @@ view model =
154 div [] 202 div []
155 [ let 203 [ let
156 cart = 204 cart =
157 List.map viewProduct model.products 205 List.map viewCartItemListing model.products
158 in 206 in
159 if List.isEmpty cart then 207 if List.isEmpty cart then
160 text "No items in cart" 208 text "No items in cart"
161 209
162 else 210 else
163 ul [] cart 211 ul [] cart
212 , calculateTotal model |> String.fromFloat |> text
213 , a [ href "/checkout" ] [ text "Checkout" ]
164 ] 214 ]