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
This commit is contained in:
William Carroll 2020-08-02 15:15:01 +01:00
parent 81c3db20d4
commit fe609bbe58
4 changed files with 171 additions and 87 deletions

View file

@ -1,21 +1,22 @@
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
allUsers : State.Model -> Html State.Msg
allUsers model =
case model.users of
allTrips : State.Model -> Html State.Msg
allTrips model =
case model.trips of
RemoteData.NotAsked ->
UI.absentData { handleFetch = State.AttemptGetUsers }
UI.absentData { handleFetch = State.AttemptGetTrips }
RemoteData.Loading ->
UI.paragraph "Loading..."
@ -24,14 +25,51 @@ 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
(\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.accounts of
RemoteData.NotAsked ->
UI.absentData { handleFetch = State.AttemptGetAccounts }
RemoteData.Loading ->
UI.paragraph "Loading..."
RemoteData.Failure e ->
UI.paragraph ("Error: " ++ Utils.explainHttpError e)
RemoteData.Success xs ->
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
]