Spit and polish

* Refactor pagination into a DRY template func
* Better guide users with no profile pic to upload one
This commit is contained in:
Noah Petherbridge 2023-06-21 20:46:27 -07:00
parent 2439c714e5
commit 0f6b627156
16 changed files with 116 additions and 212 deletions

View File

@ -40,6 +40,17 @@ func Upload() http.HandlerFunc {
vars["PhotoCount"] = photoCount
vars["PhotoQuota"] = photoQuota
// If they do not have a profile picture currently set (and are not uploading one now),
// the front-end should point this out to them.
if (user.ProfilePhotoID == nil || *user.ProfilePhotoID == 0) && vars["Intent"] != "profile_pic" {
// If they have no photo at all, make the default intent to upload one.
if photoCount == 0 {
templates.Redirect(w, r.URL.Path+"?intent=profile_pic")
return
}
vars["NoProfilePicture"] = true
}
// Are they POSTing?
if r.Method == http.MethodPost {
var (

View File

@ -1,6 +1,7 @@
package templates
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
@ -11,6 +12,7 @@ import (
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/markdown"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/photo"
"code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/utility"
@ -57,6 +59,7 @@ func TemplateFuncs(r *http.Request) template.FuncMap {
"SubtractInt": SubtractInt,
"UrlEncode": UrlEncode,
"QueryPlus": QueryPlus(r),
"SimplePager": SimplePager(r),
}
}
@ -185,3 +188,34 @@ func QueryPlus(r *http.Request) func(...interface{}) template.URL {
return template.URL(strings.Join(parts, "&"))
}
}
// SimplePager creates a paginator row (partial template).
//
// Use it like: {{SimplePager .Pager}}
//
// It runs the template partial 'simple_pager.html' to customize it for the site theme.
func SimplePager(r *http.Request) func(*models.Pagination) template.HTML {
return func(pager *models.Pagination) template.HTML {
tmpl, err := template.New("index").Funcs(template.FuncMap{
"QueryPlus": QueryPlus(r),
}).ParseFiles(config.TemplatePath + "/partials/simple_pager.html")
if err != nil {
return template.HTML(err.Error())
}
var (
vars = struct {
Pager *models.Pagination
Request *http.Request
}{pager, r}
buf = bytes.NewBuffer([]byte{})
)
err = tmpl.ExecuteTemplate(buf, "SimplePager", vars)
if err != nil {
return template.HTML(err.Error())
}
return template.HTML(buf.String())
}
}

View File

@ -18,24 +18,7 @@
</div>
<div class="block">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
<div class="columns is-multiline">

View File

@ -22,18 +22,6 @@
Found {{.Pager.Total}} user{{Pluralize64 .Pager.Total}}
(page {{.Pager.Page}} of {{.Pager.Pages}}).
</div>
<div class="column is-narrow">
<button type="submit"
class="button ml-6"
name="page"
value="{{.Pager.Previous}}"
{{if not .Pager.HasPrevious}}disabled{{end}}>Previous</button>
<button type="submit"
class="button button-primary"
name="page"
value="{{.Pager.Next}}"
{{if not .Pager.HasNext}}disabled{{end}}>Next page</button>
</div>
</div>
<div class="block">
@ -173,6 +161,8 @@
</div>
{{SimplePager .Pager}}
<div class="columns is-multiline">
{{range .Users}}
@ -274,6 +264,8 @@
{{end}}<!-- range .Friends -->
</div>
{{SimplePager .Pager}}
</div>
</form>
</div>

View File

@ -57,23 +57,7 @@
</div>
{{if .Pager}}
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
{{end}}
<div class="columns is-multiline">

View File

@ -48,25 +48,7 @@
</div>
</div>
{{if .Pager}}
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{end}}
{{SimplePager .Pager}}
<div class="columns is-multiline">
{{range .Feedback}}

View File

@ -1,4 +1,5 @@
{{define "title"}}Untitled{{end}}
{{define "content"}}{{end}}
{{define "scripts"}}{{end}}
{{define "base"}}
<!DOCTYPE html>

View File

@ -28,24 +28,7 @@
</p>
<div class="block p-2">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
<div class="block p-2">

View File

@ -53,24 +53,7 @@
{{end}}
<div class="block p-2">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
{{$Root := .}}

View File

@ -38,24 +38,7 @@
</div>
<div class="p-4">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
<div class="p-4">

View File

@ -97,24 +97,7 @@
</p>
<div class="block p-2">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
{{$Root := .}}

View File

@ -51,24 +51,7 @@
</div>
<div class="block">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
<div class="columns is-multiline">

View File

@ -0,0 +1,31 @@
<!--
A simple and generic Paginator row, with page buttons and Previous/Next
Usage: [[SimplePager .Pager]]
Give it your Pagination object (with .Previous, .Next, .Iter() and so on). It
links to the current .Request.URL.Path and with all query parameters + the page
added. Should be suitable for most pagers that don't need any specialized logic.
See also: template_funcs.go for the SimplePager wrapper function.
-->
{{define "SimplePager"}}
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{end}}

View File

@ -79,30 +79,6 @@
</a>
{{end}}
<!-- Reusable pager -->
{{define "pager"}}
{{if .Pager.Total}}
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{end}}
{{end}}
<!-- Main content template -->
{{define "content"}}
<div class="container">
@ -361,7 +337,7 @@
{{end}}
{{end}}
{{template "pager" .}}
{{SimplePager .Pager}}
<!-- "Full" view style? (blog style) -->
{{if eq .ViewStyle "full"}}
@ -576,7 +552,7 @@
</div>
{{end}}<!-- ViewStyle -->
{{template "pager" .}}
{{SimplePager .Pager}}
</div>
</div>

View File

@ -69,24 +69,7 @@
{{end}}
<div class="block">
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous{{if not .Pager.HasPrevious}} is-disabled{{end}}" title="Previous"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Previous}}">Previous</a>
<a class="pagination-next{{if not .Pager.HasNext}} is-disabled{{end}}" title="Next"
href="{{.Request.URL.Path}}?{{QueryPlus "page" .Pager.Next}}">Next page</a>
<ul class="pagination-list">
{{$Root := .}}
{{range .Pager.Iter}}
<li>
<a class="pagination-link{{if .IsCurrent}} is-current{{end}}"
aria-label="Page {{.Page}}"
href="{{$Root.Request.URL.Path}}?{{QueryPlus "page" .Page}}">
{{.Page}}
</a>
</li>
{{end}}
</ul>
</nav>
{{SimplePager .Pager}}
</div>
<div class="columns is-multiline">

View File

@ -88,6 +88,18 @@
</div>
{{end}}
<!-- No profile picture and they aren't uploading one now? -->
{{if .NoProfilePicture}}
<div class="notification is-warning block">
<p class="block">
<strong>Notice:</strong> you currently do not have a Default Profile Picture set up on your
account. You can <a href="/photo/upload?intent=profile_pic">click here to upload a new one</a>
or click the Edit button on <a href="/photo/u/{{.CurrentUser.Username}}">one of your existing photos</a>
to crop a square profile picture from one of them.
</p>
</div>
{{end}}
{{if or .EditPhoto (lt .PhotoCount .PhotoQuota)}}
<div class="columns">
<div class="column">