summaryrefslogtreecommitdiff
path: root/elm3/UVNList/Status.elm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-07-25 14:30:04 +0200
committerYorhel <git@yorhel.nl>2019-07-25 14:36:21 +0200
commitf296495a912ce759df11c43e78b4552788bdbff2 (patch)
tree0c10802de65fb7c8475722e12234bff5eb980628 /elm3/UVNList/Status.elm
parent0f3cfeb85caec6424bcbea47142eefbf8011636b (diff)
Merge the v3 branch into separate namespace + fix Docker stuff (again)
I was getting tired of having to keep two branches up-to-date with the latest developments, so decided to throw v3 into the same branch - just different files (...which will get mostly rewritten again soon). The two versions aren't very different in terms of dependencies, build system and support code, so they can now properly share files. Added a section to the README to avoid confusion. This merge also makes it easier to quickly switch between the different versions, which is handy for development. It's even possible to run both at the same time, but my scripts use the same port so that needs a workaround. And it's amazing how often I break the Docker scripts.
Diffstat (limited to 'elm3/UVNList/Status.elm')
-rw-r--r--elm3/UVNList/Status.elm77
1 files changed, 77 insertions, 0 deletions
diff --git a/elm3/UVNList/Status.elm b/elm3/UVNList/Status.elm
new file mode 100644
index 00000000..e11443df
--- /dev/null
+++ b/elm3/UVNList/Status.elm
@@ -0,0 +1,77 @@
+module UVNList.Status exposing (main)
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Json.Encode as JE
+import Browser
+import Lib.Api as Api
+import Lib.Html exposing (..)
+import Lib.Util exposing (lookup)
+import Lib.Gen exposing (vnlistStatus)
+
+
+main : Program Flags Model Msg
+main = Browser.element
+ { init = \f -> (init f, Cmd.none)
+ , subscriptions = always Sub.none
+ , view = view
+ , update = update
+ }
+
+type alias Flags =
+ { uid : Int
+ , vid : Int
+ , status : Int
+ }
+
+type alias Model =
+ { state : Api.State
+ , flags : Flags
+ }
+
+init : Flags -> Model
+init f =
+ { state = Api.Normal
+ , flags = f
+ }
+
+
+encodeForm : Model -> JE.Value
+encodeForm o = JE.object
+ [ ("uid", JE.int o.flags.uid)
+ , ("vid", JE.int o.flags.vid)
+ , ("status", JE.int o.flags.status) ]
+
+
+type Msg
+ = Input String
+ | Saved Api.Response
+
+
+update : Msg -> Model -> (Model, Cmd Msg)
+update msg model =
+ case msg of
+ Input s ->
+ let flags = model.flags
+ nflags = { flags | status = Maybe.withDefault 0 <| String.toInt s }
+ nmodel = { model | flags = nflags, state = Api.Loading }
+ in ( nmodel
+ , Api.post "/u/setvnstatus" (encodeForm nmodel) Saved )
+
+ Saved Api.Success -> ({ model | state = Api.Normal }, Cmd.none)
+ Saved e -> ({ model | state = Api.Error e }, Cmd.none)
+
+
+view : Model -> Html Msg
+view model =
+ -- TODO: Display error somewhere
+ if model.state == Api.Loading
+ then div [ class "spinner spinner--md" ] []
+ else div []
+ [ text <| Maybe.withDefault "" <| lookup model.flags.status vnlistStatus
+ , inputSelect
+ [ class "form-control--table-edit form-control--table-edit-overlay", onInput Input ]
+ (String.fromInt model.flags.status)
+ (List.map (\(a,b) -> (String.fromInt a, b)) vnlistStatus)
+ ]