From 5075f0ef4573fa95252c1a91b62239cc9b6347bb Mon Sep 17 00:00:00 2001 From: Yorhel Date: Tue, 10 Dec 2019 10:32:37 +0100 Subject: Elm: Add Redirect response + fix redirect to last post on reply Fixes https://vndb.org/t2520/14#334 - I originally had some trouble to do this because `load` doesn't actually reload the page if you're just changing the hash. The `reload` following it handles that now. The Redirect response is just cleanup, there's several places that could benefit form it. --- elm/Discussions/Reply.elm | 15 +++++++-------- elm/DocEdit.elm | 2 +- elm/Lib/Api.elm | 2 +- elm/StaffEdit/Main.elm | 2 +- lib/VNWeb/Discussions/Thread.pm | 13 +++---------- lib/VNWeb/Docs/Edit.pm | 2 +- lib/VNWeb/Elm.pm | 2 +- lib/VNWeb/Staff/Edit.pm | 2 +- 8 files changed, 16 insertions(+), 24 deletions(-) diff --git a/elm/Discussions/Reply.elm b/elm/Discussions/Reply.elm index ed239cbe..69d112cd 100644 --- a/elm/Discussions/Reply.elm +++ b/elm/Discussions/Reply.elm @@ -3,7 +3,7 @@ module Discussions.Reply exposing (main) import Html exposing (..) import Html.Attributes exposing (..) import Browser -import Browser.Navigation exposing (load) +import Browser.Navigation exposing (load,reload) import Lib.Html exposing (..) import Lib.TextPreview as TP import Lib.Api as Api @@ -11,7 +11,7 @@ import Gen.Api as GApi import Gen.DiscussionsReply as GDR -main : Program GDR.Recv Model Msg +main : Program Int Model Msg main = Browser.element { init = \e -> (init e, Cmd.none) , view = view @@ -22,17 +22,15 @@ main = Browser.element type alias Model = { state : Api.State - , newurl : String , tid : Int , msg : TP.Model } -init : GDR.Recv -> Model -init d = +init : Int -> Model +init tid = { state = Api.Normal - , newurl = d.newurl - , tid = d.tid + , tid = tid , msg = TP.bbcode "" } @@ -51,7 +49,8 @@ update msg model = Submit -> let body = GDR.encode { msg = model.msg.data, tid = model.tid } in ({ model | state = Api.Loading }, Api.post "/t/reply.json" body Submitted) - Submitted GApi.Success -> (model, load model.newurl) + -- 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) diff --git a/elm/DocEdit.elm b/elm/DocEdit.elm index 3fad1f8f..e4282213 100644 --- a/elm/DocEdit.elm +++ b/elm/DocEdit.elm @@ -73,7 +73,7 @@ update msg model = let body = GD.encode (encode model) in ({ model | state = Api.Loading }, Api.post "/d/edit.json" body Submitted) - Submitted (GApi.Changed id rev) -> (model, load <| "/d" ++ String.fromInt id ++ "." ++ String.fromInt rev) + Submitted (GApi.Redirect s) -> (model, load s) Submitted r -> ({ model | state = Api.Error r }, Cmd.none) diff --git a/elm/Lib/Api.elm b/elm/Lib/Api.elm index 1e028904..b4dfb78f 100644 --- a/elm/Lib/Api.elm +++ b/elm/Lib/Api.elm @@ -26,11 +26,11 @@ showResponse res = HTTPError (Http.BadBody r) -> "Invalid response from the server, please report a bug (debug info: " ++ r ++")." HTTPError (Http.BadUrl _) -> unexp Success -> unexp + Redirect _ -> unexp CSRF -> "Invalid CSRF token, please refresh the page and try again." Invalid -> "Invalid form data, please report a bug." Unauth -> "You do not have the permission to perform this action." Unchanged -> "No changes" - Changed _ _ -> unexp Content _ -> unexp BadLogin -> "Invalid username or password." LoginThrottle -> "Action throttled, too many failed login attempts." diff --git a/elm/StaffEdit/Main.elm b/elm/StaffEdit/Main.elm index 4ea50fd5..2aace15a 100644 --- a/elm/StaffEdit/Main.elm +++ b/elm/StaffEdit/Main.elm @@ -151,7 +151,7 @@ update msg model = let body = GSE.encode (encode model) in ({ model | state = Api.Loading }, Api.post "/s/edit.json" body Submitted) - Submitted (GApi.Changed id rev) -> (model, load <| "/s" ++ String.fromInt id ++ "." ++ String.fromInt rev) + Submitted (GApi.Redirect s) -> (model, load s) Submitted r -> ({ model | state = Api.Error r }, Cmd.none) diff --git a/lib/VNWeb/Discussions/Thread.pm b/lib/VNWeb/Discussions/Thread.pm index 507d8605..d783a4bd 100644 --- a/lib/VNWeb/Discussions/Thread.pm +++ b/lib/VNWeb/Discussions/Thread.pm @@ -30,19 +30,12 @@ elm_form 'DiscussionsPoll' => $POLL_OUT, $POLL_IN; -my $REPLY_OUT = form_compile any => { - tid => { id => 1 }, - newurl => { }, -}; - - my $REPLY_IN = form_compile any => { tid => { id => 1 }, msg => { maxlength => 32768 } }; - -elm_form 'DiscussionsReply' => $REPLY_OUT, $REPLY_IN; +elm_form 'DiscussionsReply' => undef, $REPLY_IN; @@ -113,7 +106,7 @@ sub reply_ { my($t, $page) = @_; return if $t->{count} > $page*25; if(can_edit t => $t) { - elm_ 'Discussions.Reply' => $REPLY_OUT, { tid => $t->{id}, newurl => post_url($t->{id}, $t->{count}+1) }; + elm_ 'Discussions.Reply' => undef, $t->{id}*1; } else { div_ class => 'mainbox', sub { h1_ 'Reply'; @@ -207,7 +200,7 @@ json_api qr{/t/reply\.json}, $REPLY_IN, sub { my $msg = bb_subst_links $data->{msg}; tuwf->dbExeci('INSERT INTO threads_posts (tid, num, uid, msg) VALUES (', sql_comma(\$t->{id}, \$num, \auth->uid, \$msg), ')'); tuwf->dbExeci('UPDATE threads SET count =', \$num, 'WHERE id =', \$t->{id}); - elm_Success + elm_Redirect post_url $t->{id}, $num, 'last'; }; 1; diff --git a/lib/VNWeb/Docs/Edit.pm b/lib/VNWeb/Docs/Edit.pm index fd3ce958..65aa6442 100644 --- a/lib/VNWeb/Docs/Edit.pm +++ b/lib/VNWeb/Docs/Edit.pm @@ -42,7 +42,7 @@ json_api qr{/d/edit\.json}, $FORM_IN, sub { return elm_Unchanged if !form_changed $FORM_CMP, $data, $doc; my($id,undef,$rev) = db_edit d => $doc->{id}, $data; - elm_Changed $id, $rev; + elm_Redirect "/d$id.$rev"; }; diff --git a/lib/VNWeb/Elm.pm b/lib/VNWeb/Elm.pm index 6fa710f9..d8e9a73a 100644 --- a/lib/VNWeb/Elm.pm +++ b/lib/VNWeb/Elm.pm @@ -31,8 +31,8 @@ our @EXPORT = qw/ my %apis = ( Unauth => [], # Not authorized Unchanged => [], # No changes - Changed => [ { id => 1 }, { uint => 1 } ], # [ id, chrev]; DB entry has been successfully changed Success => [], + Redirect => [{}], # Redirect to the given URL CSRF => [], # Invalid CSRF token Invalid => [], # POST data did not validate the schema Content => [{}], # Rendered HTML content (for markdown/bbcode APIs) diff --git a/lib/VNWeb/Staff/Edit.pm b/lib/VNWeb/Staff/Edit.pm index f0fc9d7c..2feb52c1 100644 --- a/lib/VNWeb/Staff/Edit.pm +++ b/lib/VNWeb/Staff/Edit.pm @@ -97,7 +97,7 @@ json_api qr{/s/edit\.json}, $FORM_IN, sub { return elm_Unchanged if !$new && !form_changed $FORM_CMP, $data, $e; my($id,undef,$rev) = db_edit s => $e->{id}, $data; - elm_Changed $id, $rev; + elm_Redirect "/s$id.$rev"; }; 1; -- cgit v1.2.3