diff options
-rw-r--r-- | elm/Reviews/Comment.elm | 52 | ||||
-rw-r--r-- | lib/VNWeb/Reviews/Page.pm | 23 |
2 files changed, 73 insertions, 2 deletions
diff --git a/elm/Reviews/Comment.elm b/elm/Reviews/Comment.elm new file mode 100644 index 00000000..fba37168 --- /dev/null +++ b/elm/Reviews/Comment.elm @@ -0,0 +1,52 @@ +module Reviews.Comment 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.ReviewsComment as GRC + + +main : Program GRC.Send Model Msg +main = Browser.element + { init = \e -> ((Api.Normal, e.id, TP.bbcode ""), Cmd.none) + , view = view + , update = update + , subscriptions = always Sub.none + } + +type alias Model = (Api.State, String, TP.Model) + +type Msg + = Content TP.Msg + | Submit + | Submitted GApi.Response + + +update : Msg -> Model -> (Model, Cmd Msg) +update msg (state,id,content) = + case msg of + Content m -> let (nm,nc) = TP.update m content in ((state,id,nm), Cmd.map Content nc) + Submit -> ((Api.Loading,id,content), GRC.send { msg = content.data, id = id } Submitted) + Submitted (GApi.Redirect s) -> ((state,id,content), load s) + Submitted r -> ((Api.Error r,id,content), Cmd.none) + + +view : Model -> Html Msg +view (state,_,content) = + form_ Submit (state == Api.Loading) + [ div [ class "mainbox" ] + [ fieldset [ class "submit" ] + [ TP.view "msg" content Content 600 ([rows 4, cols 50] ++ GRC.valMsg) + [ b [] [ text "Comment" ] + , b [ class "standout" ] [ text " (English please!) " ] + , a [ href "/d9#3" ] [ text "Formatting" ] + ] + , submitButton "Submit" state True + ] + ] + ] diff --git a/lib/VNWeb/Reviews/Page.pm b/lib/VNWeb/Reviews/Page.pm index 69af9054..190e317a 100644 --- a/lib/VNWeb/Reviews/Page.pm +++ b/lib/VNWeb/Reviews/Page.pm @@ -4,6 +4,25 @@ use VNWeb::Prelude; use VNWeb::Releases::Lib; +my $COMMENT = form_compile any => { + id => { vndbid => 'w' }, + msg => { maxlength => 32768 } +}; + +elm_api ReviewsComment => undef, $COMMENT, sub { + my($data) = @_; + my $w = tuwf->dbRowi('SELECT id, false AS locked FROM reviews WHERE id =', \$data->{id}); + return tuwf->resNotFound if !$w->{id}; + return elm_Unauth if !can_edit t => $w; + + my $num = sql 'COALESCE((SELECT MAX(num)+1 FROM reviews_posts WHERE id =', \$data->{id}, '),1)'; + my $msg = bb_subst_links $data->{msg}; + $num = tuwf->dbVali('INSERT INTO reviews_posts', { id => $w->{id}, num => $num, uid => auth->uid, msg => $msg }, 'RETURNING num'); + elm_Redirect "/$w->{id}.$num#last"; +}; + + + sub review_ { my($w) = @_; @@ -111,10 +130,10 @@ TUWF::get qr{/$RE{wid}(?:(?<sep>[\./])$RE{num})?}, sub { review_ $w; }; if(grep !$_->{hidden}, @$posts) { - h1_ class => 'boxtitle', 'Comments'; # XXX: How does this look with pagination? + h1_ class => 'boxtitle', 'Comments'; VNWeb::Discussions::Thread::posts_($w, $posts, $page); } - # TODO: "Add comment" form + fix post reporting. + elm_ 'Reviews.Comment' => $COMMENT, { id => $w->{id}, msg => '' } if $w->{count} <= $page*25 && can_edit t => $w; }; }; |