summaryrefslogtreecommitdiff
path: root/elm/TagEdit.elm
blob: 906512cc5045eb02c2953748550e2b5ba515e298 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
module TagEdit exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Browser
import Browser.Navigation exposing (load)
import Lib.Html exposing (..)
import Lib.TextPreview as TP
import Lib.Api as Api
import Lib.Util exposing (..)
import Lib.Autocomplete as A
import Lib.Ffi as Ffi
import Gen.Api as GApi
import Gen.Types exposing (tagCategories)
import Gen.TagEdit as GTE


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


type alias Model =
  { formstate    : Api.State
  , id           : Maybe Int
  , name         : String
  , aliases      : String
  , state        : Int
  , cat          : String
  , description  : TP.Model
  , searchable   : Bool
  , applicable   : Bool
  , defaultspoil : Int
  , parents      : List GTE.RecvParents
  , parentAdd    : A.Model GApi.ApiTagResult
  , addedby      : String
  , wipevotes    : Bool
  , merge        : List GTE.RecvParents
  , mergeAdd     : A.Model GApi.ApiTagResult
  , canMod       : Bool
  , dupNames     : List GApi.ApiDupNames
  }


init : GTE.Recv -> Model
init d =
  { formstate    = Api.Normal
  , id           = d.id
  , name         = d.name
  , aliases      = String.join "\n" d.aliases
  , state        = d.state
  , cat          = d.cat
  , description  = TP.bbcode d.description
  , searchable   = d.searchable
  , applicable   = d.applicable
  , defaultspoil = d.defaultspoil
  , parents      = d.parents
  , parentAdd    = A.init ""
  , addedby      = d.addedby
  , wipevotes    = False
  , merge        = []
  , mergeAdd     = A.init ""
  , canMod       = d.can_mod
  , dupNames     = []
  }


splitAliases : String -> List String
splitAliases l = String.lines l |> List.map String.trim |> List.filter (\s -> s /= "")

findDup : Model -> String -> List GApi.ApiDupNames
findDup model a = List.filter (\t -> String.toLower t.name == String.toLower a) model.dupNames

isValid : Model -> Bool
isValid model = not (List.any (findDup model >> List.isEmpty >> not) (model.name :: splitAliases model.aliases))

parentConfig : A.Config Msg GApi.ApiTagResult
parentConfig = { wrap = ParentSearch, id = "parentadd", source = A.tagSource }

mergeConfig : A.Config Msg GApi.ApiTagResult
mergeConfig = { wrap = MergeSearch, id = "mergeadd", source = A.tagSource }


encode : Model -> GTE.Send
encode m =
  { id           = m.id
  , name         = m.name
  , aliases      = splitAliases m.aliases
  , state        = m.state
  , cat          = m.cat
  , description  = m.description.data
  , searchable   = m.searchable
  , applicable   = m.applicable
  , defaultspoil = m.defaultspoil
  , parents      = List.map (\l -> {id=l.id}) m.parents
  , wipevotes    = m.wipevotes
  , merge        = List.map (\l -> {id=l.id}) m.merge
  }


type Msg
  = Noop
  | Name String
  | Aliases String
  | State Int
  | Searchable Bool
  | Applicable Bool
  | Cat String
  | DefaultSpoil Int
  | Description TP.Msg
  | ParentDel Int
  | ParentSearch (A.Msg GApi.ApiTagResult)
  | WipeVotes Bool
  | MergeDel Int
  | MergeSearch (A.Msg GApi.ApiTagResult)
  | Submit
  | Submitted (GApi.Response)


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Noop          -> (model, Cmd.none)
    Name s        -> ({ model | name = s }, Cmd.none)
    Aliases s     -> ({ model | aliases = String.replace "," "\n" s }, Cmd.none)
    State n       -> ({ model | state = n }, Cmd.none)
    Searchable b  -> ({ model | searchable = b }, Cmd.none)
    Applicable b  -> ({ model | applicable = b }, Cmd.none)
    Cat s         -> ({ model | cat = s }, Cmd.none)
    DefaultSpoil n-> ({ model | defaultspoil = n }, Cmd.none)
    WipeVotes b   -> ({ model | wipevotes = b }, Cmd.none)
    Description m -> let (nm,nc) = TP.update m model.description in ({ model | description = nm }, Cmd.map Description nc)

    ParentDel i   -> ({ model | parents = delidx i model.parents }, Cmd.none)
    ParentSearch m ->
      let (nm, c, res) = A.update parentConfig m model.parentAdd
      in case res of
        Nothing -> ({ model | parentAdd = nm }, c)
        Just p  ->
          if List.any (\e -> e.id == p.id) model.parents
          then ({ model | parentAdd = nm }, c)
          else ({ model | parentAdd = A.clear nm "", parents = model.parents ++ [{ id = p.id, name = p.name}] }, c)

    MergeDel i   -> ({ model | merge = delidx i model.merge }, Cmd.none)
    MergeSearch m ->
      let (nm, c, res) = A.update mergeConfig m model.mergeAdd
      in case res of
        Nothing -> ({ model | mergeAdd = nm }, c)
        Just p  -> ({ model | mergeAdd = A.clear nm "", merge = model.merge ++ [{ id = p.id, name = p.name}] }, c)

    Submit -> ({ model | formstate = Api.Loading }, GTE.send (encode model) Submitted)
    Submitted (GApi.DupNames l) -> ({ model | dupNames = l, formstate = Api.Normal }, Cmd.none)
    Submitted (GApi.Redirect s) -> (model, load s)
    Submitted r -> ({ model | formstate = Api.Error r }, Cmd.none)


view : Model -> Html Msg
view model =
  form_ Submit (model.formstate == Api.Loading)
  [ div [ class "mainbox" ]
    [ h1 [] [ text <| if model.id == Nothing then "Submit new tag" else "Edit tag" ]
    , table [ class "formtable" ] <|
      [ if model.id == Nothing then text "" else
        formField "Added by" [ span [ Ffi.innerHtml model.addedby ] [], br_ 2 ]
      , formField "name::Primary name" [ inputText "name" model.name Name GTE.valName ]
      , formField "aliases::Aliases"
        -- BUG: Textarea doesn't validate the maxlength and patterns for aliases, we don't have a client-side fallback check either.
        [ inputTextArea "aliases" model.aliases Aliases []
        , let dups = List.concatMap (findDup model) (model.name :: splitAliases model.aliases)
          in if List.isEmpty dups
             then span [] [ br [] [], text "Tag name and aliases must be unique and self-describing." ]
             else div []
             [ b [ class "standout" ] [ text "The following tag names are already present in the database:" ]
             , ul [] <| List.map (\t ->
                 li [] [ a [ href ("/g"++String.fromInt t.id) ] [ text t.name ] ]
               ) dups
             ]
        ]
      , tr [ class "newpart" ] [ td [ colspan 2 ] [ text "" ] ]
      , if not model.canMod then text "" else
        formField "state::State" [ inputSelect "state" model.state State GTE.valState
          [ (0, "Awaiting Moderation")
          , (1, "Deleted/hidden")
          , (2, "Approved")
          ]
        ]
      , if not model.canMod then text "" else
        formField "" [ label [] [ inputCheck "" model.searchable Searchable, text " Searchable (people can use this tag to find VNs)" ] ]
      , if not model.canMod then text "" else
        formField "" [ label [] [ inputCheck "" model.applicable Applicable, text " Applicable (people can apply this tag to VNs)" ] ]
      , formField "cat::Category" [ inputSelect "cat" model.cat Cat GTE.valCat tagCategories ]
      , formField "defaultspoil::Default spoiler level" [ inputSelect "defaultspoil" model.defaultspoil DefaultSpoil GTE.valDefaultspoil 
        [ (0, "No spoiler")
        , (1, "Minor spoiler")
        , (2, "Major spoiler")
        ] ]
      , text "" -- aliases
      , formField "description::Description"
        [ TP.view "description" model.description Description 700 ([rows 12, cols 50] ++ GTE.valDescription) []
        , text "What should the tag be used for? Having a good description helps users choose which tags to link to a VN."
        ]
      , tr [ class "newpart" ] [ td [ colspan 2 ] [ text "" ] ]
      , formField "Parent tags"
        [ table [ class "compact" ] <| List.indexedMap (\i p -> tr []
            [ td [ style "text-align" "right" ] [ b [ class "grayedout" ] [ text <| "g" ++ String.fromInt p.id ++ ":" ] ]
            , td [] [ a [ href <| "/g" ++ String.fromInt p.id ] [ text p.name ] ]
            , td [] [ inputButton "remove" (ParentDel i) [] ]
            ]
          ) model.parents
        , A.view parentConfig model.parentAdd [placeholder "Add parent tag..."]
        ]
      ]
      ++ if not model.canMod || model.id == Nothing then [] else
      [ tr [ class "newpart" ] [ td [ colspan 2 ] [ text "DANGER ZONE" ] ]
      , formField ""
        [ inputCheck "" model.wipevotes WipeVotes
        , text " Delete all direct votes on this tag. WARNING: cannot be undone!", br [] []
        , b [ class "grayedout" ] [ text "Does not affect votes on child tags. Old votes may still show up for 24 hours due to database caching." ]
        ]
      , tr [ class "newpart" ] [ td [ colspan 2 ] [ text "" ] ]
      , formField "Merge votes"
        [ text "All direct votes on the listed tags will be moved to this tag. WARNING: cannot be undone!", br [] []
        , table [ class "compact" ] <| List.indexedMap (\i p -> tr []
            [ td [ style "text-align" "right" ] [ b [ class "grayedout" ] [ text <| "g" ++ String.fromInt p.id ++ ":" ] ]
            , td [] [ a [ href <| "/g" ++ String.fromInt p.id ] [ text p.name ] ]
            , td [] [ inputButton "remove" (MergeDel i) [] ]
            ]
          ) model.merge
        , A.view mergeConfig model.mergeAdd [placeholder "Add tag to merge..."]
        ]
      ]
    ]
  , div [ class "mainbox" ]
    [ fieldset [ class "submit" ] [ submitButton "Submit" model.formstate (isValid model) ] ]
  ]