summaryrefslogtreecommitdiff
path: root/elm/Subscribe.elm
blob: ca70a675733cc8254a9df95953f87166b5b9c85a (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Subscribe exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Browser
import Lib.Html exposing (..)
import Lib.Api as Api
import Lib.DropDown exposing (onClickOutside)
import Gen.Api as GApi
import Gen.Subscribe as GS


main : Program GS.Send Model Msg
main = Browser.element
  { init   = \e -> ({ state = Api.Normal, opened = False, data = e}, Cmd.none)
  , view   = view
  , update = update
  , subscriptions = \m -> if m.opened then onClickOutside "subscribe" (Opened False) else Sub.none
  }

type alias Model =
  { state  : Api.State
  , opened : Bool
  , data   : GS.Send
  }

type Msg
  = Opened Bool
  | SubNum Bool Bool
  | SubReview Bool
  | SubApply Bool
  | Submitted GApi.Response


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  let dat = model.data
      save nd = ({ model | data = nd, state = Api.Loading }, GS.send nd Submitted)
  in
  case msg of
    Opened b    -> ({ model | opened = b }, Cmd.none)
    SubNum v b  -> save { dat | subnum = if b then Just v else Nothing }
    SubReview b -> save { dat | subreview = b }
    SubApply b  -> save { dat | subapply = b }
    Submitted e -> ({ model | state = if e == GApi.Success then Api.Normal else Api.Error e }, Cmd.none)


view : Model -> Html Msg
view model =
  let
    dat = model.data
    t = String.left 1 dat.id
    msg txt = p [] [ text txt, text " These can be disabled globally in your ", a [ href "/u/notifies" ] [ text "notification settings" ], text "." ]
  in
  div []
  [ a [ href "#", onClickD (Opened (not model.opened))
      , class (if (dat.noti > 0 && dat.subnum /= Just False) || dat.subnum == Just True || dat.subreview || dat.subapply then "active" else "inactive")
      ] [ text "🔔" ]
  , if not model.opened then text ""
    else div [] [ div []
    [ h4 []
      [ if model.state == Api.Loading then span [ class "spinner", style "float" "right" ] [] else text ""
      , text "Manage Notifications"
      ]
    , case (t, dat.noti) of
        ("t", 1) -> msg "You receive notifications for replies because you have posted in this thread."
        ("t", 2) -> msg "You receive notifications for replies because this thread is linked to your personal board."
        ("t", 3) -> msg "You receive notifications for replies because you have posted in this thread and it is linked to your personal board."
        ("w", 1) -> msg "You receive notifications for new comments because you have commented on this review."
        ("w", 2) -> msg "You receive notifications for new comments because this is your review."
        ("w", 3) -> msg "You receive notifications for new comments because this is your review and you have commented it."
        (_,   1) -> msg "You receive edit notifications for this entry because you have contributed to it."
        _ -> text ""
    , if dat.noti == 0 then text "" else
      label []
      [ inputCheck "" (dat.subnum == Just False) (SubNum False)
      , case t of
          "t" -> text " Disable notifications only for this thread."
          "w" -> text " Disable notifications only for this review."
          _   -> text " Disable edit notifications only for this entry."
      ]
    , if t == "i" then text "" else label []
      [ inputCheck "" (dat.subnum == Just True) (SubNum True)
      , case t of
          "t" -> text " Enable notifications for new replies"
          "w" -> text " Enable notifications for new comments"
          _   -> text " Enable notifications for new edits"
      , if dat.noti == 0 then text "." else text ", regardless of the global setting."
      ]
    , if t /= "v" then text "" else
      label [] [ inputCheck "" dat.subreview SubReview, text " Enable notifications for new reviews." ]
    , if t /= "i" then text "" else
      label [] [ inputCheck "" dat.subapply SubApply, text " Enable notifications when this trait is applied or removed from a character." ]
    , case model.state of
        Api.Error e -> b [ class "standout" ] [ br [] [], text (Api.showResponse e) ]
        _ -> text ""
    ] ]
  ]