about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--client/src/State.elm99
-rw-r--r--client/src/User.elm75
-rw-r--r--client/src/Utils.elm18
3 files changed, 187 insertions, 5 deletions
diff --git a/client/src/State.elm b/client/src/State.elm
index a38895a6c50f..110fb72a78ed 100644
--- a/client/src/State.elm
+++ b/client/src/State.elm
@@ -9,6 +9,7 @@ import Http
 import Json.Decode as JD
 import Json.Decode.Extra as JDE
 import Json.Encode as JE
+import Json.Encode.Extra as JEE
 import Process
 import RemoteData exposing (WebData)
 import Shared
@@ -37,12 +38,16 @@ type Msg
     | UpdateTripStartDate DatePicker.Msg
     | UpdateTripEndDate DatePicker.Msg
     | UpdateTripComment String
+    | UpdateEditTripDestination String
+    | UpdateEditTripComment String
     | ClearErrors
     | ToggleLoginForm
     | PrintPage
     | UpdateInviteEmail String
     | UpdateInviteRole (Maybe Role)
     | ReceiveTodaysDate Date.Date
+    | EditTrip Trip
+    | CancelEditTrip
       -- SPA
     | LinkClicked Browser.UrlRequest
     | UrlChanged Url.Url
@@ -56,6 +61,7 @@ type Msg
     | AttemptCreateTrip Date.Date Date.Date
     | AttemptDeleteTrip Trip
     | AttemptInviteUser Role
+    | AttemptUpdateTrip TripPK Trip
       -- Inbound network
     | GotAccounts (WebData (List Account))
     | GotTrips (WebData (List Trip))
@@ -66,6 +72,7 @@ type Msg
     | GotCreateTrip (Result Http.Error ())
     | GotDeleteTrip (Result Http.Error ())
     | GotInviteUser (Result Http.Error ())
+    | GotUpdateTrip (Result Http.Error ())
 
 
 type Route
@@ -121,6 +128,13 @@ type alias Trip =
     }
 
 
+type alias TripPK =
+    { username : String
+    , destination : String
+    , startDate : Date.Date
+    }
+
+
 type alias Model =
     { route : Maybe Route
     , url : Url.Url
@@ -139,11 +153,15 @@ type alias Model =
     , tripEndDate : Maybe Date.Date
     , tripComment : String
     , trips : WebData (List Trip)
+    , editingTrip : Maybe Trip
+    , editTripDestination : String
+    , editTripComment : String
     , adminTab : AdminTab
     , loginTab : LoginTab
     , inviteEmail : String
     , inviteRole : Maybe Role
     , inviteResponseStatus : WebData ()
+    , updateTripStatus : WebData ()
     , loginError : Maybe Http.Error
     , logoutError : Maybe Http.Error
     , signUpError : Maybe Http.Error
@@ -278,6 +296,24 @@ signUp { username, email, password } =
         }
 
 
+updateTrip : TripPK -> Trip -> Cmd Msg
+updateTrip tripKey trip =
+    Utils.putWithCredentials
+        { url = endpoint [ "trips" ] []
+        , body =
+            Http.jsonBody
+                (JE.object
+                    [ ( "tripKey", encodeTripKey tripKey )
+                    , ( "destination", JE.string trip.destination )
+                    , ( "startDate", encodeDate trip.startDate )
+                    , ( "endDate", encodeDate trip.endDate )
+                    , ( "comment", JE.string trip.comment )
+                    ]
+                )
+        , expect = Http.expectWhatever GotUpdateTrip
+        }
+
+
 inviteUser : { email : String, role : Role } -> Cmd Msg
 inviteUser { email, role } =
     Utils.postWithCredentials
@@ -359,6 +395,15 @@ decodeReview =
         (JD.field "timestamp" JD.string)
 
 
+encodeTripKey : TripPK -> JE.Value
+encodeTripKey tripKey =
+    JE.object
+        [ ( "username", JE.string tripKey.username )
+        , ( "destination", JE.string tripKey.destination )
+        , ( "startDate", encodeDate tripKey.startDate )
+        ]
+
+
 encodeDate : Date.Date -> JE.Value
 encodeDate date =
     date |> Date.toIsoString |> JE.string
@@ -474,6 +519,9 @@ prod _ url key =
       , tripEndDate = Nothing
       , tripComment = ""
       , trips = RemoteData.NotAsked
+      , editingTrip = Nothing
+      , editTripDestination = ""
+      , editTripComment = ""
       , startDatePicker = startDatePicker
       , endDatePicker = endDatePicker
       , adminTab = Accounts
@@ -481,6 +529,7 @@ prod _ url key =
       , inviteEmail = ""
       , inviteRole = Nothing
       , inviteResponseStatus = RemoteData.NotAsked
+      , updateTripStatus = RemoteData.NotAsked
       , loginError = Nothing
       , logoutError = Nothing
       , signUpError = Nothing
@@ -563,7 +612,7 @@ port printPage : () -> Cmd msg
 -}
 init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
 init flags url key =
-    adminHome flags url key
+    prod flags url key
 
 
 {-| Now that we have state, we need a function to change the state.
@@ -650,6 +699,12 @@ update msg model =
         UpdateTripComment x ->
             ( { model | tripComment = x }, Cmd.none )
 
+        UpdateEditTripDestination x ->
+            ( { model | editTripDestination = x }, Cmd.none )
+
+        UpdateEditTripComment x ->
+            ( { model | editTripComment = x }, Cmd.none )
+
         ClearErrors ->
             ( { model
                 | loginError = Nothing
@@ -686,6 +741,24 @@ update msg model =
         ReceiveTodaysDate date ->
             ( { model | todaysDate = Just date }, Cmd.none )
 
+        EditTrip trip ->
+            ( { model
+                | editingTrip = Just trip
+                , editTripDestination = trip.destination
+                , editTripComment = trip.comment
+              }
+            , Cmd.none
+            )
+
+        CancelEditTrip ->
+            ( { model
+                | editingTrip = Nothing
+                , editTripDestination = ""
+                , editTripComment = ""
+              }
+            , Cmd.none
+            )
+
         LinkClicked urlRequest ->
             case urlRequest of
                 Browser.Internal url ->
@@ -839,14 +912,32 @@ update msg model =
                     , Cmd.none
                     )
 
-                Err x ->
+                Err e ->
                     ( { model
-                        | inviteUserError = Just x
-                        , inviteResponseStatus = RemoteData.Failure x
+                        | inviteUserError = Just e
+                        , inviteResponseStatus = RemoteData.Failure e
                       }
                     , sleepAndClearErrors
                     )
 
+        -- PATCH /trips
+        AttemptUpdateTrip tripKey trip ->
+            ( { model | updateTripStatus = RemoteData.Loading }
+            , updateTrip tripKey trip
+            )
+
+        GotUpdateTrip result ->
+            case result of
+                Ok _ ->
+                    ( { model | updateTripStatus = RemoteData.Success () }
+                    , fetchTrips
+                    )
+
+                Err e ->
+                    ( { model | updateTripStatus = RemoteData.Failure e }
+                    , Cmd.none
+                    )
+
         -- POST /accounts
         AttemptSignUp ->
             ( model
diff --git a/client/src/User.elm b/client/src/User.elm
index 84523ef59e01..87871b78dbc4 100644
--- a/client/src/User.elm
+++ b/client/src/User.elm
@@ -71,6 +71,61 @@ createTrip model =
         ]
 
 
+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
@@ -102,6 +157,12 @@ renderTrip today trip =
         , UI.paragraph ("\"" ++ trip.comment ++ "\"")
         , UI.wrapNoPrint
             (UI.textButton
+                { label = "Edit"
+                , handleClick = State.EditTrip trip
+                }
+            )
+        , UI.wrapNoPrint
+            (UI.textButton
                 { label = "Delete"
                 , handleClick = State.AttemptDeleteTrip trip
                 }
@@ -133,7 +194,19 @@ trips model =
                             [ ul [ [ "my-4" ] |> Tailwind.use |> class ]
                                 (xs
                                     |> List.sortWith (\x y -> Date.compare y.startDate x.startDate)
-                                    |> List.map (renderTrip today)
+                                    |> 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
diff --git a/client/src/Utils.elm b/client/src/Utils.elm
index 28f15fb5c101..60343cd87018 100644
--- a/client/src/Utils.elm
+++ b/client/src/Utils.elm
@@ -77,6 +77,24 @@ deleteWithCredentials { url, body, expect } =
         , expect = expect
         }
 
+putWithCredentials :
+    { url : String
+    , body : Http.Body
+    , expect : Http.Expect msg
+    }
+    -> Cmd msg
+putWithCredentials { url, body, expect } =
+    Http.riskyRequest
+        { url = url
+        , headers = [ Http.header "Origin" Shared.clientOrigin ]
+        , method = "PUT"
+        , timeout = Nothing
+        , tracker = Nothing
+        , body = body
+        , expect = expect
+        }
+
+
 
 formatTime : Time.Posix -> String
 formatTime ts =