From 487c9a4a3fa4109559aa2a25184224b992705eef Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 16 Aug 2020 08:58:50 +0200 Subject: Discussions: Split post editing out of Discussions::Edit + support editing review comments This split simplifies Discussions::Edit a little bit and allows Discussions::PostEdit to be generic enough to handle editing review comments as well. --- elm/Discussions/Edit.elm | 30 ++++-------- elm/Discussions/PostEdit.elm | 108 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 elm/Discussions/PostEdit.elm (limited to 'elm') diff --git a/elm/Discussions/Edit.elm b/elm/Discussions/Edit.elm index 9a98733b..6008cdef 100644 --- a/elm/Discussions/Edit.elm +++ b/elm/Discussions/Edit.elm @@ -26,7 +26,6 @@ main = Browser.element type alias Model = { state : Api.State , tid : Maybe String - , num : Maybe Int , can_mod : Bool , can_private : Bool , locked : Bool @@ -50,7 +49,6 @@ init d = , can_mod = d.can_mod , can_private = d.can_private , tid = d.tid - , num = d.num , locked = d.locked , hidden = d.hidden , private = d.private @@ -73,7 +71,6 @@ searchConfig = { wrap = BoardSearch, id = "boardadd", source = A.boardSource } encode : Model -> GDE.Send encode m = { tid = m.tid - , num = m.num , locked = m.locked , hidden = m.hidden , private = m.private @@ -148,8 +145,6 @@ update msg model = view : Model -> Html Msg view model = let - thread = model.tid == Nothing || model.num == Just 1 - board n bd = li [] <| [ text "[" @@ -184,7 +179,7 @@ view model = else text "" ] - poll () = + poll = [ tr [ class "newpart" ] [ td [ colspan 2 ] [ text "" ] ] , formField "" [ label [] [ inputCheck "" model.pollEnabled PollEnabled, text " Add poll" ] ] ] ++ @@ -211,26 +206,22 @@ view model = in form_ Submit (model.state == Api.Loading) [ div [ class "mainbox" ] - [ h1 [] [ text <| if model.tid == Nothing then "Create new thread" else "Edit post" ] + [ h1 [] [ text <| if model.tid == Nothing then "Create new thread" else "Edit thread" ] , table [ class "formtable" ] <| - [ if thread - then formField "title::Thread title" [ inputText "title" (Maybe.withDefault "" model.title) Title (style "width" "400px" :: required True :: GDE.valTitle) ] - else formField "Topic" [ a [ href <| "/" ++ Maybe.withDefault "" model.tid ] [ text (Maybe.withDefault "" model.title) ] ] - , if thread && model.can_mod + [ formField "title::Thread title" [ inputText "title" (Maybe.withDefault "" model.title) Title (style "width" "400px" :: required True :: GDE.valTitle) ] + , if model.can_mod then formField "" [ label [] [ inputCheck "" model.locked Locked, text " Locked" ] ] else text "" , if model.can_mod then formField "" [ label [] [ inputCheck "" model.hidden Hidden, text " Hidden" ] ] else text "" - , if thread && model.can_private + , if model.can_private then formField "" [ label [] [ inputCheck "" model.private Private, text " Private" ] ] else text "" , if model.tid /= Nothing && model.can_mod then formField "" [ label [] [ inputCheck "" model.nolastmod Nolastmod, text " Don't update last modification timestamp" ] ] else text "" - , if thread - then formField "boardadd::Boards" (boards ()) - else text "" + , formField "boardadd::Boards" (boards ()) , tr [ class "newpart" ] [ td [ colspan 2 ] [ text "" ] ] , formField "msg::Message" [ TP.view "msg" model.msg Content 700 ([rows 12, cols 50] ++ GDE.valMsg) @@ -239,15 +230,10 @@ view model = ] ] ] - ++ (if thread then poll () else []) + ++ poll ++ (if not model.can_mod || model.tid == Nothing then [] else [ tr [ class "newpart" ] [ td [ colspan 2 ] [ text "DANGER ZONE" ] ] - , formField "" - [ inputCheck "" model.delete Delete - , text <| " Permanently delete this " ++ if thread then "thread and all replies." else "post." - , text <| if thread then "" else " This causes all replies after this one to be renumbered." - , text <| " This action can not be reverted, only do this with obvious spam!" - ] + , formField "" [ inputCheck "" model.delete Delete, text " Permanently delete this thread and all replies. This action can not be reverted, only do this with obvious spam!" ] ]) ] , div [ class "mainbox" ] diff --git a/elm/Discussions/PostEdit.elm b/elm/Discussions/PostEdit.elm new file mode 100644 index 00000000..0eb787d2 --- /dev/null +++ b/elm/Discussions/PostEdit.elm @@ -0,0 +1,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#3" ] [ 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 ] ] + ] -- cgit v1.2.3