module Discussions.Reply exposing (main) import Html exposing (..) import Html.Attributes exposing (..) import Browser import Browser.Navigation exposing (load,reload) import Lib.Html exposing (..) import Lib.TextPreview as TP import Lib.Api as Api import Gen.Api as GApi import Gen.DiscussionsReply as GDR main : Program Int 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 , tid : Int , msg : TP.Model } init : Int -> Model init tid = { state = Api.Normal , tid = tid , msg = TP.bbcode "" } type Msg = Content TP.Msg | Submit | Submitted GApi.Response update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of Content m -> let (nm,nc) = TP.update m model.msg in ({ model | msg = nm }, Cmd.map Content nc) Submit -> let body = GDR.encode { msg = model.msg.data, tid = model.tid } in ({ model | state = Api.Loading }, Api.post "/t/reply.json" body Submitted) -- Reload is necessary because s may be the same as the current URL (with a location.hash) Submitted (GApi.Redirect s) -> (model, Cmd.batch [ load s, reload ]) Submitted r -> ({ model | state = Api.Error r }, Cmd.none) view : Model -> Html Msg view model = form_ Submit (model.state == Api.Loading) [ div [ class "mainbox" ] [ fieldset [ class "submit" ] [ TP.view "msg" model.msg Content 600 ([rows 4, cols 50] ++ GDR.valMsg) [ b [] [ text "Quick reply" ] , b [ class "standout" ] [ text " (English please!) " ] , a [ href "/d9#3" ] [ text "Formatting" ] ] , submitButton "Submit" model.state True ] ] ]