about summary refs log tree commit diff
path: root/client/src/User.elm
blob: 5216eeada03e7bf459a3d7b9171d3464f38164ed (plain) (blame)
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
module User exposing (render)

import Common
import Date
import DatePicker
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Maybe.Extra as ME
import RemoteData
import State
import Tailwind
import UI
import Utils


createTrip : State.Model -> Html State.Msg
createTrip model =
    div []
        [ UI.header 3 "Plan Upcoming Trip"
        , UI.textField
            { pholder = "Where are you going?"
            , inputId = "destination"
            , handleInput = State.UpdateTripDestination
            , inputValue = model.tripDestination
            }
        , div [ [ "flex" ] |> Tailwind.use |> class ]
            [ UI.datePicker
                { mDate = model.tripStartDate
                , prompt = "Set departure date"
                , prefix = "Departure: "
                , picker = model.startDatePicker
                , onUpdate = State.UpdateTripStartDate
                }
            , UI.datePicker
                { mDate = model.tripEndDate
                , prompt = "Set return date"
                , prefix = "Return: "
                , picker = model.endDatePicker
                , onUpdate = State.UpdateTripEndDate
                }
            ]
        , UI.textField
            { pholder = "Comments?"
            , inputId = "comment"
            , handleInput = State.UpdateTripComment
            , inputValue = model.tripComment
            }
        , UI.baseButton
            { enabled =
                List.all
                    identity
                    [ String.length model.tripDestination > 0
                    , String.length model.tripComment > 0
                    , ME.isJust model.tripStartDate
                    , ME.isJust model.tripEndDate
                    ]
            , extraClasses = [ "my-4" ]
            , handleClick =
                case ( model.tripStartDate, model.tripEndDate ) of
                    ( Nothing, _ ) ->
                        State.DoNothing

                    ( _, Nothing ) ->
                        State.DoNothing

                    ( Just startDate, Just endDate ) ->
                        State.AttemptCreateTrip startDate endDate
            , label = "Schedule trip"
            }
        ]


trips : State.Model -> Html msg
trips model =
    div []
        [ UI.header 3 "Upcoming Trips"
        , case model.trips of
            RemoteData.NotAsked ->
                UI.paragraph "Somehow we've reached the user home page without requesting your trips data. Please report this to our engineering team at bugs@tripplaner.tld"

            RemoteData.Loading ->
                UI.paragraph "Loading your trips..."

            RemoteData.Failure e ->
                UI.paragraph ("Error: " ++ Utils.explainHttpError e)

            RemoteData.Success xs ->
                ul []
                    (xs
                        |> List.map
                            (\trip ->
                                li
                                    [ [ "py-2" ]
                                        |> Tailwind.use
                                        |> class
                                    ]
                                    [ text
                                        (Date.toIsoString trip.startDate
                                            ++ " - "
                                            ++ Date.toIsoString trip.endDate
                                            ++ " -> "
                                            ++ trip.destination
                                        )
                                    ]
                            )
                    )
        ]


render : State.Model -> Html State.Msg
render model =
    div
        [ class
            ([ "container"
             , "mx-auto"
             , "text-center"
             ]
                |> Tailwind.use
            )
        ]
        [ UI.header 2 ("Welcome, " ++ model.username ++ "!")
        , createTrip model
        , trips model
        , UI.textButton
            { label = "Logout"
            , handleClick = State.AttemptLogout
            }
        , Common.allErrors model
        ]