diff --git a/helperfuncs.go b/helperfuncs.go index 4725663b8297ea01b90abf4f2216ab746decf2da..4783fe40e8c476e6ed20b2581d099390e57c5377 100644 --- a/helperfuncs.go +++ b/helperfuncs.go @@ -153,19 +153,29 @@ func (m model) authenticated(links map[string]string) string { linksKeys := maps.Keys(links) slices.Sort(linksKeys) + type pageData struct { + Table string // HTML Table inserted to home page + Start string // Start + End string + } + + var homePage pageData + // And ranging through the keys to pull ordered values from the map - var table string for _, k := range linksKeys { v := links[k] - table = table + fmt.Sprintf(` + homePage.Table = homePage.Table + fmt.Sprintf(`

%s

%s

EditDelete `, k, v, k, url.QueryEscape(string(v)), k) } + homePage.Start = linksKeys[0] + homePage.End = linksKeys[len(linksKeys)-1] + page := new(strings.Builder) - err = tmpl.Execute(page, table) + err = tmpl.Execute(page, homePage) if err != nil { log.Println(err) } diff --git a/root.go b/root.go index dd38a56d53f090b7215aad26dfcc0818e7b0b1c0..ab7fc1d0e18470d706c3072ce21cfcfd4efd7632 100644 --- a/root.go +++ b/root.go @@ -79,6 +79,14 @@ func (m model) root(writer http.ResponseWriter, request *http.Request) { return } + if len(links) == 0 { + links, err = m.read("", "", count) + if err != nil { + http.Error(writer, err.Error(), 400) + return + } + } + _, err = io.WriteString(writer, m.authenticated(links)) if err != nil { log.Println(err) diff --git a/templates/home_authenticated.html b/templates/home_authenticated.html index 1ce3db0b76602835573c4412a86a7f4f75dc0c67..af96bc3b224b9d406243af2fd197eb9f60ff8b7f 100644 --- a/templates/home_authenticated.html +++ b/templates/home_authenticated.html @@ -83,6 +83,9 @@ .links_table { overflow: scroll; } + .pagination { + margin: 35px 0; + } @@ -104,8 +107,13 @@ Short link Long link Actions - {{ . }} + {{ .Table }} +