summaryrefslogtreecommitdiff
path: root/elm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-12-10 10:32:37 +0100
committerYorhel <git@yorhel.nl>2019-12-10 10:32:39 +0100
commit5075f0ef4573fa95252c1a91b62239cc9b6347bb (patch)
treefcc6be6d5cad66dc1af217a4f6b3fe733df60945 /elm
parentfa7b9e6a14a1597516738a75b6dfb9d326be7d48 (diff)
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.
Diffstat (limited to 'elm')
-rw-r--r--elm/Discussions/Reply.elm15
-rw-r--r--elm/DocEdit.elm2
-rw-r--r--elm/Lib/Api.elm2
-rw-r--r--elm/StaffEdit/Main.elm2
4 files changed, 10 insertions, 11 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)