aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/Main.elm
blob: 09a851d4f8c7d2e3f8d40cbca1681b5e01b8d99e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
module Main exposing (Model, Msg(..), init, main, subscriptions, update, view, viewLink)

import Browser
import Browser.Navigation as Nav
import Cart
import Catalog
import Checkout
import Css exposing (..)
import Html
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (..)
import Html.Styled.Events exposing (..)
import Http
import Json.Encode as Encode
import Login
import Product
import Profile
import Signup
import Styles exposing (..)
import Url
import Url.Parser as P exposing ((</>), Parser, int, oneOf, s, string)



-- MAIN


main : Program () Model Msg
main =
    Browser.application
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        , onUrlChange = UrlChanged
        , onUrlRequest = LinkClicked
        }



-- MODEL


type Route
    = LoginPage
    | SignupPage
    | HomePage
    | CatalogPage
    | CartPage
    | ProductPage Int
    | CheckoutPage
    | ProfilePage
    | NotFoundPage


parseRoute : Parser (Route -> a) a
parseRoute =
    oneOf
        [ P.map LoginPage (P.s "login")
        , P.map HomePage P.top
        , P.map CatalogPage (P.s "catalog")
        , P.map CartPage (P.s "cart")
        , P.map SignupPage (P.s "signup")
        , P.map CheckoutPage (P.s "checkout")
        , P.map ProductPage (P.s "product" </> P.int)
        , P.map ProfilePage (P.s "profile")
        ]


type alias Model =
    { key : Nav.Key
    , url : Url.Url
    , location : Route
    , loginModel : Login.Model
    , catalogModel : Catalog.Model
    , productModel : Product.Model
    , signupModel : Signup.Model
    , cartModel : Cart.Model
    , checkoutModel : Checkout.Model
    , profileModel : Profile.Model
    }


init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
    let
        start =
            HomePage

        login =
            Login.init

        catalog =
            Catalog.init

        product =
            Product.init

        signup =
            Signup.init

        cart =
            Cart.init

        checkout =
            Checkout.init

        profile =
            Profile.init
    in
    ( Model key url start login catalog product signup cart checkout profile, Cmd.none )



-- UPDATE


type Msg
    = LinkClicked Browser.UrlRequest
    | UrlChanged Url.Url
    | LoginMessage Login.Msg
    | CatalogMessage Catalog.Msg
    | ProductMessage Product.Msg
    | SignupMessage Signup.Msg
    | CartMessage Cart.Msg
    | CheckoutMessage Checkout.Msg
    | ProfileMessage Profile.Msg
    | LogoutPressed
    | LogoutSuccess (Result Http.Error ())


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        LinkClicked urlRequest ->
            case urlRequest of
                Browser.Internal url ->
                    ( model, Nav.pushUrl model.key (Url.toString url) )

                Browser.External href ->
                    ( model, Nav.load href )

        LogoutPressed ->
            ( model, tryLogout )

        LogoutSuccess _ ->
            ( { model | loginModel = Login.init }, Nav.replaceUrl model.key "/login" )

        UrlChanged url ->
            let
                parsedUrl =
                    P.parse parseRoute url
            in
            case parsedUrl of
                Just CatalogPage ->
                    ( { model | location = CatalogPage }, Cmd.map CatalogMessage Catalog.fetchProducts )

                Just (ProductPage id) ->
                    let
                        cmds =
                            List.map (Cmd.map ProductMessage)
                                [ Product.fetchListing id
                                , Product.fetchRatings id
                                ]
                    in
                    ( { model | location = ProductPage id }, Cmd.batch cmds )

                Just CartPage ->
                    let
                        cmd =
                            Cmd.map CartMessage Cart.fetchCartItems
                    in
                    ( { model | location = CartPage }, cmd )

                Just CheckoutPage ->
                    let
                        _ =
                            Debug.log "err" "loading checkout page ..."

                        cmd =
                            Cmd.map CheckoutMessage Checkout.fetchAmount
                    in
                    ( { model | location = CheckoutPage }, cmd )

                Just ProfilePage ->
                    let
                        cmd =
                            Cmd.map ProfileMessage Profile.tryFetchProfile
                    in
                    ( { model | location = ProfilePage }, cmd )

                Just p ->
                    ( { model | location = p }, Cmd.none )

                Nothing ->
                    ( { model | location = NotFoundPage }, Cmd.none )

        LoginMessage lm ->
            let
                ( lmn, cmd ) =
                    Login.update lm model.loginModel

                redir =
                    case lmn.loginStatus of
                        Login.LoggedIn ->
                            Nav.replaceUrl model.key "/catalog"

                        _ ->
                            Cmd.none
            in
            ( { model | loginModel = lmn }, Cmd.batch [ Cmd.map LoginMessage cmd, redir ] )

        SignupMessage sm ->
            let
                ( smn, cmd ) =
                    Signup.update sm model.signupModel

                redir =
                    case smn.status of
                        Signup.CreatedSuccessfully ->
                            Nav.replaceUrl model.key "/login"

                        _ ->
                            Cmd.none
            in
            ( { model | signupModel = smn }, Cmd.batch [ Cmd.map SignupMessage cmd, redir ] )

        CatalogMessage cm ->
            let
                ( cmn, cmd ) =
                    Catalog.update cm model.catalogModel
            in
            ( { model | catalogModel = cmn }, Cmd.map CatalogMessage cmd )

        CartMessage cm ->
            let
                ( cmn, cmd ) =
                    Cart.update cm model.cartModel
            in
            ( { model | cartModel = cmn }, Cmd.map CartMessage cmd )

        CheckoutMessage cm ->
            let
                ( cmn, cmd ) =
                    Checkout.update cm model.checkoutModel

                redir =
                    case cmn.pageStatus of
                        Checkout.CheckedOut ->
                            Nav.replaceUrl model.key "/profile"

                        _ ->
                            Cmd.none

                _ =
                    Debug.log "err" "received checkout message ..."
            in
            ( { model | checkoutModel = cmn }, Cmd.batch [ Cmd.map CheckoutMessage cmd, redir ] )

        ProfileMessage pm ->
            let
                ( pmn, cmd ) =
                    Profile.update pm model.profileModel

                _ =
                    Debug.log "err" "recieved profile message"
            in
            ( { model | profileModel = pmn }, Cmd.map ProfileMessage cmd )

        ProductMessage pm ->
            let
                ( pmn, cmd ) =
                    Product.update pm model.productModel

                redir =
                    case pm of
                        Product.AddToCartSuccess _ ->
                            Nav.replaceUrl model.key "/cart"

                        _ ->
                            Cmd.none
            in
            ( { model | productModel = pmn }, Cmd.batch [ Cmd.map ProductMessage cmd, redir ] )


tryLogout : Cmd Msg
tryLogout =
    Http.riskyRequest
        { method = "POST"
        , headers = []
        , url = "http://127.0.0.1:7878/user/logout"
        , body = Http.emptyBody
        , expect = Http.expectWhatever LogoutSuccess
        , timeout = Nothing
        , tracker = Nothing
        }



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions _ =
    Sub.none



-- VIEW


view : Model -> Browser.Document Msg
view model =
    case model.location of
        HomePage ->
            { title = "Login"
            , body =
                -- model.loginModel
                --     |> Login.view
                --     |> Html.Styled.map LoginMessage
                --     |> toUnstyled
                --     |> List.singleton
                div []
                    [ ul []
                        (List.map
                            (\l ->
                                li []
                                    [ a [ href l ] [ text l ] ]
                            )
                            [ "/login", "/catalog", "/cart" ]
                        )
                    ]
                    |> toUnstyled
                    |> List.singleton
            }

        LoginPage ->
            { title = "Login"
            , body =
                model.loginModel
                    |> Login.view
                    |> Html.Styled.map LoginMessage
                    |> toUnstyled
                    |> List.singleton
            }

        SignupPage ->
            { title = "Signup"
            , body =
                model.signupModel
                    |> Signup.view
                    |> Html.Styled.map SignupMessage
                    |> toUnstyled
                    |> List.singleton
            }

        NotFoundPage ->
            { title = "404 - Not Found"
            , body =
                div []
                    [ text "404 - Not Found"
                    , a [ href "/" ] [ text "Go back >" ]
                    ]
                    |> toUnstyled
                    |> List.singleton
            }

        CatalogPage ->
            { title = "Catalog"
            , body =
                model.catalogModel
                    |> Catalog.view
                    |> Html.Styled.map CatalogMessage
                    |> pageWrap model
            }

        CartPage ->
            { title = "Cart"
            , body =
                model.cartModel
                    |> Cart.view
                    |> Html.Styled.map CartMessage
                    |> pageWrap model
            }

        CheckoutPage ->
            { title = "Checkout"
            , body =
                model.checkoutModel
                    |> Checkout.view
                    |> Html.Styled.map CheckoutMessage
                    |> pageWrap model
            }

        ProfilePage ->
            { title = "Profile"
            , body =
                model.profileModel
                    |> Profile.view
                    |> Html.Styled.map ProfileMessage
                    |> pageWrap model
            }

        ProductPage item ->
            { title = "Product " ++ String.fromInt item
            , body =
                model.productModel
                    |> Product.view
                    |> Html.Styled.map ProductMessage
                    |> pageWrap model
            }


viewHeader : Model -> Html Msg
viewHeader model =
    let
        links =
            [ ( "Catalog", "/catalog" )
            , ( "Cart", "/cart" )
            ]
    in
    div
        [ css
            [ padding (px 30)
            , paddingTop (px 3)
            , paddingBottom (px 3)
            , textAlign left
            , backgroundColor theme.secondary
            ]
        ]
        [ List.map
            (\( name, loc ) ->
                li [ css [ display inline ] ]
                    [ headerLink [ href loc ] [ text name ]
                    ]
            )
            links
            ++ (if model.loginModel.loginStatus /= Login.LoggedIn then
                    [ furbyButton [] [ headerLink [ href "/login" ] [ text "Login" ] ] ]

                else
                    [ headerLink [ href "/profile" ] [ text "Profile" ]
                    , furbyButton [ onClick LogoutPressed ] [ text "Logout" ]
                    ]
               )
            |> ul
                [ css
                    [ listStyle Css.none
                    , padding (px 0)
                    , margin (px 12)
                    ]
                ]
        ]


pageWrap : Model -> Html Msg -> List (Html.Html Msg)
pageWrap model page =
    div []
        [ viewHeader model
        , page
        ]
        |> toUnstyled
        |> List.singleton


viewLink : String -> Html msg
viewLink path =
    li [] [ a [ href path ] [ text path ] ]