xhr errorcallback, login and comment score/votes in inbox

This commit is contained in:
Ryan Stafford 2023-07-08 11:26:41 -04:00
parent 1b5c6f619e
commit f2ca245b13
3 changed files with 36 additions and 18 deletions

View file

@ -1,8 +1,9 @@
function request(url, params, callback) { function request(url, params, callback, errorcallback) {
var xmlHttp = new XMLHttpRequest(); var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() { xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText); return callback(xmlHttp.responseText);
errorcallback(xmlHttp.responseText);
} }
var method = "GET" var method = "GET"
if (params) method = "POST" if (params) method = "POST"
@ -45,8 +46,12 @@ function commentClick(e) {
params += "&" + e.target.name + "=" + e.target.value params += "&" + e.target.name + "=" + e.target.value
params += "&xhr=1" params += "&xhr=1"
e.target.disabled = "disabled" e.target.disabled = "disabled"
request(targ.target, params, function(res){ request(targ.target || "", params,
function(res){
targ.outerHTML = res targ.outerHTML = res
},
function(res){
e.target.disabled = ""
}) })
} }
return false return false
@ -100,7 +105,8 @@ function loadMore(e) {
var urlParams = new URLSearchParams(window.location.search); var urlParams = new URLSearchParams(window.location.search);
urlParams.set("xhr", "1") urlParams.set("xhr", "1")
urlParams.set("page", page) urlParams.set("page", page)
request(window.location.origin+window.location.pathname+"?"+urlParams.toString(), "", function(res){ request(window.location.origin+window.location.pathname+"?"+urlParams.toString(), "",
function(res){
if (res.trim()) { if (res.trim()) {
e.target.outerHTML = res + '<input id="loadmore" type="submit" data-page="'+(parseInt(page)+1)+'" value="load more" onclick="loadMore(event)">' e.target.outerHTML = res + '<input id="loadmore" type="submit" data-page="'+(parseInt(page)+1)+'" value="load more" onclick="loadMore(event)">'
if (showimages = document.getElementById("showimages")) { if (showimages = document.getElementById("showimages")) {
@ -112,7 +118,11 @@ function loadMore(e) {
else { else {
e.target.outerHTML = '<input id="end" type="submit" value="" disabled>' e.target.outerHTML = '<input id="end" type="submit" value="" disabled>'
} }
}) },
function(res) {
e.target.outerHTML = '<input id="end" type="submit" value="loading failed" onclick="loadMore(event)">'
}
)
return false; return false;
} }
function hideAllChildComments(e) { function hideAllChildComments(e) {
@ -146,9 +156,14 @@ function formSubmit(e) {
params += "&" + e.submitter.name + "=" + e.submitter.value params += "&" + e.submitter.name + "=" + e.submitter.value
params += "&xhr=1" params += "&xhr=1"
e.submitter.disabled = "disabled" e.submitter.disabled = "disabled"
request(targ.target, params, function(res){ request(targ.target, params,
function(res){
targ.outerHTML = res targ.outerHTML = res
}) },
function(res){
e.submitter.disabled = ""
}
)
return false return false
} }

View file

@ -1061,6 +1061,7 @@ func GetRouter() *httprouter.Router {
router.GET("/:host/search", middleware(Search)) router.GET("/:host/search", middleware(Search))
router.POST("/:host/search", middleware(UserOp)) router.POST("/:host/search", middleware(UserOp))
router.GET("/:host/inbox", middleware(Inbox)) router.GET("/:host/inbox", middleware(Inbox))
router.POST("/:host/inbox", middleware(UserOp))
router.GET("/:host/login", middleware(GetLogin)) router.GET("/:host/login", middleware(GetLogin))
router.POST("/:host/login", middleware(SignUpOrLogin)) router.POST("/:host/login", middleware(SignUpOrLogin))
router.GET("/:host/settings", middleware(Settings)) router.GET("/:host/settings", middleware(Settings))
@ -1091,6 +1092,7 @@ func GetRouter() *httprouter.Router {
router.GET("/search", middleware(Search)) router.GET("/search", middleware(Search))
router.POST("/search", middleware(UserOp)) router.POST("/search", middleware(UserOp))
router.GET("/inbox", middleware(Inbox)) router.GET("/inbox", middleware(Inbox))
router.POST("/inbox", middleware(UserOp))
router.GET("/login", middleware(GetLogin)) router.GET("/login", middleware(GetLogin))
router.POST("/login", middleware(SignUpOrLogin)) router.POST("/login", middleware(SignUpOrLogin))
router.GET("/settings", middleware(Settings)) router.GET("/settings", middleware(Settings))

View file

@ -349,6 +349,7 @@ func (state *State) GetMessages() {
Post: m.Post, Post: m.Post,
Creator: m.Creator, Creator: m.Creator,
Community: m.Community, Community: m.Community,
Counts: m.Counts,
}, },
Op: unread, Op: unread,
State: state, State: state,