summaryrefslogtreecommitdiff
path: root/elm/Discussions/PostEdit.elm
blob: d8de1aab56fc65467635c3704048e9af344a5bd8 (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
module Discussions.PostEdit 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 Gen.Api as GApi
import Gen.DiscussionsPostEdit as GPE


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


type alias Model =
  { state       : Api.State
  , id          : String
  , num         : Int
  , can_mod     : Bool
  , hidden      : Bool
  , nolastmod   : Bool
  , delete      : Bool
  , msg         : TP.Model
  }


init : GPE.Recv -> Model
init d =
  { state       = Api.Normal
  , id          = d.id
  , num         = d.num
  , can_mod     = d.can_mod
  , hidden      = d.hidden
  , nolastmod   = False
  , delete      = False
  , msg         = TP.bbcode d.msg
  }

encode : Model -> GPE.Send
encode m =
  { id        = m.id
  , num       = m.num
  , hidden    = m.hidden
  , nolastmod = m.nolastmod
  , delete    = m.delete
  , msg       = m.msg.data
  }


type Msg
  = Hidden Bool
  | Nolastmod Bool
  | Delete Bool
  | Content TP.Msg
  | Submit
  | Submitted GApi.Response


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Hidden  b     -> ({ model | hidden  = b }, Cmd.none)
    Nolastmod b   -> ({ model | nolastmod=b }, Cmd.none)
    Delete  b     -> ({ model | delete  = b }, Cmd.none)
    Content m     -> let (nm,nc) = TP.update m model.msg in ({ model | msg = nm }, Cmd.map Content nc)

    Submit -> ({ model | state = Api.Loading }, GPE.send (encode model) Submitted)
    Submitted (GApi.Redirect s) -> (model, load s)
    Submitted r -> ({ model | state = Api.Error r }, Cmd.none)


view : Model -> Html Msg
view model =
  form_ "" Submit (model.state == Api.Loading)
  [ div [ class "mainbox" ]
    [ h1 [] [ text "Edit post" ]
    , table [ class "formtable" ] <|
      [ formField "Post" [ a [ href <| "/" ++ model.id ++ "." ++ String.fromInt model.num ] [ text <| "#" ++ String.fromInt model.num ++ " on " ++ model.id ] ]
      , if model.can_mod
        then formField "" [ label [] [ inputCheck "" model.hidden Hidden, text " Hidden" ] ]
        else text ""
      , if model.can_mod
        then formField "" [ label [] [ inputCheck "" model.nolastmod Nolastmod, text " Don't update last modification timestamp" ] ]
        else text ""
      , tr [ class "newpart" ] [ td [ colspan 2 ] [ text "" ] ]
      , formField "msg::Message"
        [ TP.view "msg" model.msg Content 700 ([rows 12, cols 50] ++ GPE.valMsg)
          [ b [ class "standout" ] [ text " (English please!) " ]
          , a [ href "/d9#4" ] [ text "Formatting" ]
          ]
        ]
      ]
      ++ (if not model.can_mod then [] else
      [ tr [ class "newpart" ] [ td [ colspan 2 ] [ text "DANGER ZONE" ] ]
      , formField "" [ inputCheck "" model.delete Delete, text " Permanently delete this post. This action can not be reverted, only do this with obvious spam!" ]
      ])
    ]
  , div [ class "mainbox" ]
    [ fieldset [ class "submit" ] [ submitButton "Submit" model.state True ] ]
  ]