diff --git a/api/handlers.go b/api/handlers.go index e77b306..e421420 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -24,8 +24,15 @@ func GetApiLogout(c *fiber.Ctx) error { } func GetApiFeed(c *fiber.Ctx) error { - sql, args, err := sq.Select("*").From("message"). + page := c.QueryInt("page", 0) + if page < 0 { page = 0 } + + sql, args, err := sq.Select("*"). + Limit(2). + Offset(uint64(2 * page)). + From("message"). Where(sq.Eq{"user_id": c.Params("user_id")}).ToSql() + if err != nil { return IfErrNil(err, c) } err = data.SelectJson[data.Message](c, err, sql, args...) diff --git a/static/js/alpine-intersect.js b/static/js/alpine-intersect.js new file mode 100644 index 0000000..2342257 --- /dev/null +++ b/static/js/alpine-intersect.js @@ -0,0 +1 @@ +(()=>{function o(e){e.directive("intersect",e.skipDuringClone((t,{value:i,expression:l,modifiers:n},{evaluateLater:r,cleanup:c})=>{let s=r(l),a={rootMargin:x(n),threshold:f(n)},u=new IntersectionObserver(d=>{d.forEach(h=>{h.isIntersecting!==(i==="leave")&&(s(),n.includes("once")&&u.disconnect())})},a);u.observe(t),c(()=>{u.disconnect()})}))}function f(e){if(e.includes("full"))return .99;if(e.includes("half"))return .5;if(!e.includes("threshold"))return 0;let t=e[e.indexOf("threshold")+1];return t==="100"?1:t==="0"?0:Number(`.${t}`)}function p(e){let t=e.match(/^(-?[0-9]+)(px|%)?$/);return t?t[1]+(t[2]||"px"):void 0}function x(e){let t="margin",i="0px 0px 0px 0px",l=e.indexOf(t);if(l===-1)return i;let n=[];for(let r=1;r<5;r++)n.push(p(e[l+r]||""));return n=n.filter(r=>r!==void 0),n.length?n.join(" ").trim():i}document.addEventListener("alpine:init",()=>{window.Alpine.plugin(o)});})(); diff --git a/static/js/code.js b/static/js/code.js index b317a93..4115938 100644 --- a/static/js/code.js +++ b/static/js/code.js @@ -4,7 +4,7 @@ class PaginateTable { this.items = []; this.url = url; this.headers = []; - this.search_query="" + this.search_query=""; } async contents() { @@ -31,6 +31,39 @@ class PaginateTable { } } +class ForeverScroll { + constructor(url) { + this.page = 0; + this.items = []; + this.url = url; + this.end = false; + } + + async init() { + const resp = await fetch(this.url); + console.assert(resp.status == 200, "failed to get it"); + + const items = await resp.json(); + if(items) this.items = items; + } + + async load() { + this.page += 1 + let url = `${this.url}?page=${this.page}`; + + const resp = await fetch(url); + console.assert(resp.status == 200, "failed to get it"); + + const items = await resp.json(); + + if(items) { + this.items.push(...items); + } else { + this.end = true; + } + } +} + const GetJson = async (url) => { const resp = await fetch(url); console.assert(resp.status == 200, "failed to get it"); diff --git a/views/feed.html b/views/feed.html index 3c7f63d..e4e9756 100644 --- a/views/feed.html +++ b/views/feed.html @@ -28,10 +28,10 @@ {{end}} -
+
Notifications @@ -43,7 +43,7 @@ -