summaryrefslogtreecommitdiff
path: root/elm3/UVNList/Status.elm
blob: 7a2782d22ccadfdcf3464f9cd7a0a918daf2b392 (plain)
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
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 as Gen


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 Gen.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 Gen.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)) Gen.vnlistStatus)
    ]