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


renderEditTrip : State.Model -> State.Trip -> Html State.Msg
renderEditTrip model trip =
    li []
        [ div []
            [ UI.textField
                { handleInput = State.UpdateEditTripDestination
                , inputId = "edit-trip-destination"
                , inputValue = model.editTripDestination
                , pholder = "Destination"
                }
            , UI.textField
                { handleInput = State.UpdateEditTripComment
                , inputId = "edit-trip-comment"
                , inputValue = model.editTripComment
                , pholder = "Comment"
                }
            ]
        , div []
            [ UI.baseButton
                { enabled =
                    case model.updateTripStatus of
                        RemoteData.Loading ->
                            False

                        _ ->
                            True
                , extraClasses = []
                , label =
                    case model.updateTripStatus of
                        RemoteData.Loading ->
                            "Saving..."

                        _ ->
                            "Save"
                , handleClick =
                    State.AttemptUpdateTrip
                        { username = trip.username
                        , destination = trip.destination
                        , startDate = trip.startDate
                        }
                        { username = trip.username
                        , destination = model.editTripDestination
                        , startDate = trip.startDate
                        , endDate = trip.endDate
                        , comment = model.editTripComment
                        }
                }
            , UI.simpleButton
                { label = "Cancel"
                , handleClick = State.CancelEditTrip
                }
            ]
        ]


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 = "Edit"
                , handleClick = State.EditTrip trip
                }
            )
        , 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
                                        (\trip ->
                                            case model.editingTrip of
                                                Nothing ->
                                                    renderTrip today trip

                                                Just x ->
                                                    if x == trip then
                                                        renderEditTrip model trip

                                                    else
                                                        renderTrip today trip
                                        )
                                )
                            , 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
                ]
        )