about summary refs log tree commit diff
path: root/client/src/Admin.elm
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-08-02T14·15+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-08-02T14·15+0100
commitfe609bbe5804be229a7e5c0d276654fb3e45179b (patch)
treee939713105443914e045974c89e8f3cdf0c3294c /client/src/Admin.elm
parent81c3db20d4775a115f148ed64c5bc1e54c5a3b65 (diff)
Support CRUDing records on Admin page
TL;DR:
- Prefer the more precise verbiage, "Accounts", to "Users"
- Add username field to Trip instead of relying on session.username
- Ensure that decodeRole can JD.fail for invalid inputs
Diffstat (limited to 'client/src/Admin.elm')
-rw-r--r--client/src/Admin.elm95
1 files changed, 72 insertions, 23 deletions
diff --git a/client/src/Admin.elm b/client/src/Admin.elm
index e8e33bde617b..17155c1d8e22 100644
--- a/client/src/Admin.elm
+++ b/client/src/Admin.elm
@@ -1,21 +1,50 @@
 module Admin exposing (render)
 
+import Common
+import Date
 import Html exposing (..)
 import Html.Attributes exposing (..)
 import Html.Events exposing (..)
 import RemoteData
 import State
-import Common
 import Tailwind
 import UI
 import Utils
 
 
+allTrips : State.Model -> Html State.Msg
+allTrips model =
+    case model.trips of
+        RemoteData.NotAsked ->
+            UI.absentData { handleFetch = State.AttemptGetTrips }
+
+        RemoteData.Loading ->
+            UI.paragraph "Loading..."
+
+        RemoteData.Failure e ->
+            UI.paragraph ("Error: " ++ Utils.explainHttpError e)
+
+        RemoteData.Success xs ->
+            ul []
+                (xs
+                    |> List.map
+                        (\trip ->
+                            li []
+                                [ UI.paragraph (Date.toIsoString trip.startDate ++ " - " ++ Date.toIsoString trip.endDate ++ ", " ++ trip.username ++ " is going " ++ trip.destination)
+                                , UI.textButton
+                                    { label = "delete"
+                                    , handleClick = State.AttemptDeleteTrip trip
+                                    }
+                                ]
+                        )
+                )
+
+
 allUsers : State.Model -> Html State.Msg
 allUsers model =
-    case model.users of
+    case model.accounts of
         RemoteData.NotAsked ->
-            UI.absentData { handleFetch = State.AttemptGetUsers }
+            UI.absentData { handleFetch = State.AttemptGetAccounts }
 
         RemoteData.Loading ->
             UI.paragraph "Loading..."
@@ -24,14 +53,23 @@ allUsers model =
             UI.paragraph ("Error: " ++ Utils.explainHttpError e)
 
         RemoteData.Success xs ->
-            div []
-                [ UI.header 3 "Admins"
-                , users xs.admin
-                , UI.header 3 "Managers"
-                , users xs.manager
-                , UI.header 3 "Users"
-                , users xs.user
-                ]
+            ul []
+                (xs
+                    |> List.map
+                        (\account ->
+                            li []
+                                [ UI.paragraph
+                                    (account.username
+                                        ++ " - "
+                                        ++ State.roleToString account.role
+                                    )
+                                , UI.textButton
+                                    { label = "delete"
+                                    , handleClick = State.AttemptDeleteAccount account.username
+                                    }
+                                ]
+                        )
+                )
 
 
 users : List String -> Html State.Msg
@@ -45,7 +83,7 @@ users xs =
                         , div [ [ "flex-1" ] |> Tailwind.use |> class ]
                             [ UI.simpleButton
                                 { label = "Delete"
-                                , handleClick = State.AttemptDeleteUser x
+                                , handleClick = State.AttemptDeleteAccount x
                                 }
                             ]
                         ]
@@ -63,21 +101,32 @@ render model =
             |> Tailwind.use
             |> class
         ]
-        [ UI.header 2 "Welcome back!"
-        , UI.simpleButton
-            { label = "Logout"
-            , handleClick = State.AttemptLogout
-            }
+        [ UI.header 2 "Welcome!"
         , div []
-            [ UI.baseButton
-                { label = "Switch to users"
-                , handleClick = State.UpdateAdminTab State.Users
-                , enabled = not (model.adminTab == State.Users)
-                , extraClasses = []
+            [ UI.textButton
+                { label = "Logout"
+                , handleClick = State.AttemptLogout
                 }
             ]
+        , div [ [ "py-3" ] |> Tailwind.use |> class ]
+            [ case model.adminTab of
+                State.Accounts ->
+                    UI.textButton
+                        { label = "Switch to trips"
+                        , handleClick = State.UpdateAdminTab State.Trips
+                        }
+
+                State.Trips ->
+                    UI.textButton
+                        { label = "Switch to accounts"
+                        , handleClick = State.UpdateAdminTab State.Accounts
+                        }
+            ]
         , case model.adminTab of
-            State.Users ->
+            State.Accounts ->
                 allUsers model
+
+            State.Trips ->
+                allTrips model
         , Common.allErrors model
         ]