about summary refs log tree commit diff
path: root/users/wpcarro/assessments/tt/client/src/Admin.elm
blob: d95609ee15e4a726d08c0457982cc456ab42fb29 (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
module Admin exposing (render)

import Common
import Date
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


roleToggle : State.Model -> State.Role -> Html State.Msg
roleToggle model role =
    div [ [ "px-1", "inline" ] |> Tailwind.use |> class ]
        [ UI.toggleButton
            { toggled = model.inviteRole == Just role
            , label = State.roleToString role
            , handleEnable = State.UpdateInviteRole (Just role)
            , handleDisable = State.UpdateInviteRole Nothing
            }
        ]


inviteUser : State.Model -> Html State.Msg
inviteUser model =
    div [ [ "pb-6" ] |> Tailwind.use |> class ]
        [ UI.header 3 "Invite a user"
        , UI.textField
            { handleInput = State.UpdateInviteEmail
            , inputId = "invite-email"
            , inputValue = model.inviteEmail
            , pholder = "Email..."
            }
        , div [ [ "pt-4" ] |> Tailwind.use |> class ]
            [ roleToggle model State.User
            , roleToggle model State.Manager
            , roleToggle model State.Admin
            ]
        , UI.baseButton
            { enabled =
                List.all
                    identity
                    [ String.length model.inviteEmail > 0
                    , ME.isJust model.inviteRole
                    ]
            , extraClasses = [ "my-4" ]
            , label =
                case model.inviteResponseStatus of
                    RemoteData.Loading ->
                        "Sending..."

                    _ ->
                        "Send invitation"
            , handleClick =
                case model.inviteRole of
                    Nothing ->
                        State.DoNothing

                    Just role ->
                        State.AttemptInviteUser role
            }
        ]


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.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
users xs =
    ul []
        (xs
            |> List.map
                (\x ->
                    li [ [ "py-4", "flex" ] |> Tailwind.use |> class ]
                        [ p [ [ "flex-1" ] |> Tailwind.use |> class ] [ text x ]
                        , div [ [ "flex-1" ] |> Tailwind.use |> class ]
                            [ UI.simpleButton
                                { label = "Delete"
                                , handleClick = State.AttemptDeleteAccount x
                                }
                            ]
                        ]
                )
        )


render : State.Model -> Html State.Msg
render model =
    div
        [ [ "container"
          , "mx-auto"
          , "text-center"
          ]
            |> Tailwind.use
            |> class
        ]
        [ UI.header 2 "Welcome!"
        , div []
            [ 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.Accounts ->
                div []
                    [ inviteUser model
                    , allUsers model
                    ]

            State.Trips ->
                allTrips model
        , Common.allErrors model
        ]