summaryrefslogtreecommitdiff
path: root/elm3/User/Settings.elm
blob: 538225ca96045e8134351139439feb894f687d57 (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
module User.Settings exposing (main)

import Bitwise exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Browser
import Browser.Navigation exposing (reload)
import Lib.Html exposing (..)
import Lib.Gen exposing (..)
import Lib.Api as Api


main : Program UserEdit Model Msg
main = Browser.element
  { init   = init
  , view   = view
  , update = update
  , subscriptions = always Sub.none
  }


type alias Model =
  { state       : Api.State
  , saved       : Bool
  , data        : UserEdit
  , cpass       : Bool
  , pass1       : String
  , pass2       : String
  , opass       : String
  , passNeq     : Bool
  }


init : UserEdit -> (Model, Cmd Msg)
init d =
  ({state         = Api.Normal
  , saved         = False
  , data          = d
  , cpass         = False
  , pass1         = ""
  , pass2         = ""
  , opass         = ""
  , passNeq       = False
  }, Cmd.none)


encode : Model -> UserEditSend
encode model =
  { hide_list     = model.data.hide_list
  , ign_votes     = model.data.ign_votes
  , mail          = model.data.mail
  , password      = if model.cpass then Just { old = model.opass, new = model.pass1 } else Nothing
  , perm          = model.data.perm
  , show_nsfw     = model.data.show_nsfw
  , spoilers      = model.data.spoilers
  , tags_all      = model.data.tags_all
  , tags_cont     = model.data.tags_cont
  , tags_ero      = model.data.tags_ero
  , tags_tech     = model.data.tags_tech
  , traits_sexual = model.data.traits_sexual
  , username      = model.data.username
  }


type UpdateMsg
  = Username String
  | Email String
  | Perm Int Bool
  | IgnVotes Bool
  | HideList Bool
  | ShowNsfw Bool
  | TraitsSexual Bool
  | Spoilers String
  | TagsAll  Bool
  | TagsCont Bool
  | TagsEro  Bool
  | TagsTech Bool

type Msg
  = Submit
  | Submitted Api.Response
  | Set UpdateMsg
  | CPass Bool
  | OPass String
  | Pass1 String
  | Pass2 String


updateField : UpdateMsg -> UserEdit -> UserEdit
updateField msg model =
  case msg of
    Username s -> { model | username = s }
    Email s    -> { model | mail = s }
    Perm n b   -> { model | perm = if b then or model.perm n else and model.perm (complement n) }
    IgnVotes b -> { model | ign_votes = b }
    HideList b -> { model | hide_list = b }
    ShowNsfw b -> { model | show_nsfw = b }
    TraitsSexual b -> { model | traits_sexual = b }
    Spoilers s -> { model | spoilers = Maybe.withDefault model.spoilers (String.toInt s) }
    TagsAll  b -> { model | tags_all  = b }
    TagsCont b -> { model | tags_cont = b }
    TagsEro  b -> { model | tags_ero  = b }
    TagsTech b -> { model | tags_tech = b }


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Set m      -> ({ model | saved = False, data = updateField m model.data }, Cmd.none)
    CPass b    -> ({ model | saved = False, cpass = b }, Cmd.none)
    OPass s    -> ({ model | saved = False, opass = s }, Cmd.none)
    Pass1 s    -> ({ model | saved = False, pass1 = s, passNeq = s /= model.pass2 }, Cmd.none)
    Pass2 s    -> ({ model | saved = False, pass2 = s, passNeq = s /= model.pass1 }, Cmd.none)

    Submit ->
      let
        path = "/u" ++ String.fromInt model.data.id ++ "/edit"
        body = usereditSendEncode (encode model)
      in ({ model | state = Api.Loading }, Api.post path body Submitted)

    Submitted (Api.Success) ->
      ( { model | state = Api.Normal, saved = True, cpass = False, opass = "", pass1 = "", pass2 = "" }
      , if model.cpass then reload else Cmd.none
      )
    Submitted r -> ({ model | state = Api.Error r }, Cmd.none)


view : Model -> Html Msg
view model =
  form_ Submit (model.state == Api.Loading)
    [ card "account" "Account info" [] <|

      [ cardRow "General" Nothing <| formGroups
        [ [ label [ for "username" ] [ text "Username" ]
          , inputText "username" model.data.username (Set << Username) [required True, maxlength 200, pattern "[a-z0-9-]{2,15}", disabled (not model.data.authmod)]
          ]
        , [ label [ for "email" ] [ text "Email address" ]
          , inputText "email" model.data.mail (Set << Email) [type_ "email", required True, pattern emailPattern]
          ]
        ]

      , cardRow "Password" Nothing <| formGroups <|
          [ label [ class "checkbox" ]
            [ inputCheck "" model.cpass CPass
            , text " Change password" ]
          ]
        :: if not model.cpass then [] else
        [ [ label [ class "opass" ] [ text "Current password" ]
          , inputText "opass" model.opass OPass [type_ "password", required True, minlength 4, maxlength 500]
          ]
        , [ label [ class "pass1" ] [ text "New password" ]
          , inputText "pass1" model.pass1 Pass1 [type_ "password", required True, minlength 4, maxlength 500]
          ]
        , [ label [ class "pass2" ] [ text "Repeat" ]
          , inputText "pass2" model.pass2 Pass2 [type_ "password", required True, minlength 4, maxlength 500, classList [("is-invalid", model.passNeq)]]
          , if model.passNeq
            then div [class "invalid-feedback"]
              [ text "Passwords do not match." ]
            else text ""
          ]
        ]

      ] ++ if not model.data.authmod then [] else
      [ cardRow "Mod options" Nothing <| formGroups
        [ [ label [] [ text "Permissions" ]
          ] ++ List.map (\(n,s) ->
            label [ class "checkbox" ] [ inputCheck "" (and model.data.perm n > 0) (Set << Perm n), text (" " ++ s) ]
          ) userPerms
        , [ label [] [ text "Other" ]
          , label [ class "checkbox" ] [ inputCheck "" model.data.ign_votes (Set << IgnVotes), text "Ignore votes in VN statistics" ]
          ]
        ]
      ]

    , card "preferences" "Preferences" [] <|

      [ cardRow "Privacy" Nothing <| formGroup
        [ label [ class "checkbox" ] [ inputCheck "" model.data.hide_list (Set << HideList), text "Hide my visual novel list, vote list and wishlist" ] ]

      , cardRow "NSFW" Nothing <| formGroups
        [ [ label [ class "checkbox" ] [ inputCheck "" model.data.show_nsfw     (Set << ShowNsfw),     text "Disable warnings for images that are not safe for work" ] ]
        , [ label [ class "checkbox" ] [ inputCheck "" model.data.traits_sexual (Set << TraitsSexual), text "Show sexual traits by default on character pages" ] ]
        ]

      , cardRow "Spoilers" Nothing <| formGroup
        [ label [ for "spoilers" ] [ text "Default spoiler level" ]
        , inputSelect [onInput (Set << Spoilers)] (String.fromInt model.data.spoilers)
          [ ("0", "Hide spoilers")
          , ("1", "Show only minor spoilers")
          , ("2", "Show all spoilers")
          ]
        ]

      , cardRow "Tags" Nothing <| formGroups
        [ [ label [ class "checkbox" ] [ inputCheck "" model.data.tags_all (Set << TagsAll), text "Show all tags by default on visual novel pages (don't summarize)" ] ]
        , [ label [] [ text "Default tag categories on visual novel pages:" ]
          , label [ class "chexkbox" ] [ inputCheck "" model.data.tags_cont (Set << TagsCont), text "Content" ]
          , label [ class "chexkbox" ] [ inputCheck "" model.data.tags_ero  (Set << TagsEro ), text "Sexual content" ]
          , label [ class "chexkbox" ] [ inputCheck "" model.data.tags_tech (Set << TagsTech), text "Technical" ]
          ]
        ]
      ]

    , submitButton (if model.saved then "Saved!" else "Save") model.state (not model.passNeq) False
    ]