summaryrefslogtreecommitdiff
path: root/elm/Reviews/Vote.elm
blob: 717539c18ccbbd8a25a9838c091e1bb04c588008 (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
module Reviews.Vote exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Browser
import Lib.Util exposing (..)
import Lib.Html exposing (..)
import Lib.Api as Api
import Gen.Api as GApi
import Gen.ReviewsVote as GRV


main : Program GRV.Recv Model Msg
main = Browser.element
  { init = \d -> (init d, Cmd.none)
  , subscriptions = always Sub.none
  , view = view
  , update = update
  }

type alias Model =
  { state    : Api.State
  , id       : String
  , my       : Maybe Bool
  , up       : Int
  , down     : Int
  }

init : GRV.Recv -> Model
init d =
  { state    = Api.Normal
  , id       = d.id
  , my       = d.my
  , up       = d.up
  , down     = d.down
  }

type Msg
  = Vote Bool
  | Saved GApi.Response


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Vote b ->
      let nm = case (model.my, b) of
                (Nothing,    True)  -> { model | my = Just b,  up = model.up+1                      }
                (Nothing,    False) -> { model | my = Just b                  , down = model.down+1 }
                (Just True,  False) -> { model | my = Just b,  up = model.up-1, down = model.down+1 }
                (Just False, True)  -> { model | my = Just b,  up = model.up+1, down = model.down-1 }
                (Just True,  True)  -> { model | my = Nothing, up = model.up-1                      }
                (Just False, False) -> { model | my = Nothing                 , down = model.down-1 }
      in ({ nm | state = Api.Loading }, GRV.send { id = nm.id, my = nm.my } Saved)

    Saved GApi.Success -> ({ model | state = Api.Normal }, Cmd.none)
    Saved e -> ({ model | state = Api.Error e }, Cmd.none)


view : Model -> Html Msg
view model =
  let but opt lbl = a [ href "#", onClickD (Vote opt), classList [("votebut", True), ("myvote", model.my == Just opt)] ] [ text lbl ]
  in
  span []
  [ case model.state of
      Api.Loading -> span [ class "spinner" ] []
      Api.Error e -> b [ class "standout" ] [ text (Api.showResponse e) ]
      Api.Normal  -> if model.my == Nothing then text "Was this review helpful? " else text ""
  , but True ("👍 " ++ String.fromInt model.up)
  , text " "
  , but False ("👎 " ++ String.fromInt model.down)
  ]