about summary refs log tree commit diff
path: root/client/src/User.elm
blob: 84523ef59e01e0f3afce5d57e8eee2c572e0adc6 (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
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
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"
            }
        ]


renderTrip : Date.Date -> State.Trip -> Html State.Msg
renderTrip today trip =
    li
        [ [ "py-2" ]
            |> Tailwind.use
            |> class
        ]
        [ if Date.compare today trip.startDate == GT then
            UI.paragraph
                (String.fromInt (Date.diff Date.Days trip.startDate today)
                    ++ " days until you're travelling to "
                    ++ trip.destination
                    ++ " for "
                    ++ String.fromInt
                        (Date.diff
                            Date.Days
                            trip.startDate
                            trip.endDate
                        )
                    ++ " days."
                )

          else
            UI.paragraph
                (String.fromInt (Date.diff Date.Days today trip.endDate)
                    ++ " days ago you returned from your trip to "
                    ++ trip.destination
                )
        , UI.paragraph ("\"" ++ trip.comment ++ "\"")
        , UI.wrapNoPrint
            (UI.textButton
                { label = "Delete"
                , handleClick = State.AttemptDeleteTrip trip
                }
            )
        ]


trips : State.Model -> Html State.Msg
trips model =
    div []
        [ UI.header 3 "Your 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 ->
                case model.todaysDate of
                    Nothing ->
                        text ""

                    Just today ->
                        div [ [ "mb-10" ] |> Tailwind.use |> class ]
                            [ ul [ [ "my-4" ] |> Tailwind.use |> class ]
                                (xs
                                    |> List.sortWith (\x y -> Date.compare y.startDate x.startDate)
                                    |> List.map (renderTrip today)
                                )
                            , UI.wrapNoPrint
                                (UI.simpleButton
                                    { label = "Print iternary"
                                    , handleClick = State.PrintPage
                                    }
                                )
                            ]
        ]


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