Skip to content

Commit

Permalink
deprioritize pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Phommathep committed Mar 15, 2023
1 parent 1bb2c8b commit f4919de
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 75 deletions.
37 changes: 6 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -53,11 +52,7 @@ type Work struct {
Results []string `json:"results"`
}
type APIResponse struct {
Works []Work `json:"works"`
ResultCount int `json:"resultCount"`
Limit int `json:"resultLimit"`
Page int `json:"page"`
TotalPages int `json:"totalPages"`
Works []Work `json:"works"`
}

func handleSearch(searcher Searcher) func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -69,21 +64,7 @@ func handleSearch(searcher Searcher) func(w http.ResponseWriter, r *http.Request
return
}

var err error
pageStr := r.URL.Query().Get("page")
page := 1

if pageStr != "" {
page, err = strconv.Atoi(pageStr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println("Error", err)
fmt.Fprintf(w, "unable to parse page number")
return
}
}

works, totalResults, err := searcher.Search(query[0])
works, err := searcher.Search(query[0])

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -92,14 +73,8 @@ func handleSearch(searcher Searcher) func(w http.ResponseWriter, r *http.Request
return
}

totalPages := totalResults / RESULT_LIMIT

res := APIResponse{
Works: works,
ResultCount: totalResults,
Limit: RESULT_LIMIT,
Page: page,
TotalPages: totalPages,
Works: works,
}

buf := &bytes.Buffer{}
Expand Down Expand Up @@ -188,15 +163,15 @@ func getWorkTitles() []string {
}
}

func (s *Searcher) Search(query string) ([]Work, int, error) {
func (s *Searcher) Search(query string) ([]Work, error) {
works := []Work{}
results := []string{}
count := 0
workIdx := 0

regex, err := regexp.Compile(fmt.Sprintf("(?i)\\w?%s\\w?", query))
if err != nil {
return []Work{}, count, err
return []Work{}, err
}

workTitles := getWorkTitles()
Expand Down Expand Up @@ -237,7 +212,7 @@ func (s *Searcher) Search(query string) ([]Work, int, error) {
}
}

return works, count, nil
return works, nil
}

func (s *Searcher) markResult(idx []int, query string, results []string) []string {
Expand Down
41 changes: 1 addition & 40 deletions static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const Controller = {
const response = fetch(`/search?q=${query}&limit=10&page=${page}`).then((response) => {
response.json().then((response) => {
Controller.updateTable(response);
Controller.updatePagination(response, query);
});
});
},
Expand All @@ -38,45 +37,7 @@ const Controller = {
}
}
},
updatePagination: (response, query) => {
let pagination = document.getElementById("pagination");
pagination.innerHTML = "";
let previous = false;

for (let page = response.page, start = page, end = page + RESULT_LIMIT; page <= end && page < response.totalPages; page++) {
previousPage = start - 1
if (previous == false && previousPage > 0) {
paginate(pagination, previousPage);
previous = true;
}

if (page == start) {
let firstPage = "";
if (page == 1) {
firstPage = "disabled";
}
paginate(pagination, page, firstPage);
}

if (page > response.page && page < end) {
paginate(pagination, page);
}
}

function paginate(pagination, page, disabled = '') {
var li = document.createElement("li");
li.className = "page-item " + disabled;
var a = document.createElement("a");
a.className = "page-link ";
a.setAttribute("href", `/search?q=${query}&page=${page}`);
a.innerHTML = page;
li.appendChild(a);
pagination.appendChild(li);
return { li, a };
}
},
};

const form = document.getElementById("form");
form.addEventListener("submit", Controller.search);
document.getElementById("pagination").addEventListener("click", Controller.search);
form.addEventListener("submit", Controller.search);
4 changes: 0 additions & 4 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ <h1>ShakeSearch</h1>
<tbody id="table-body"></tbody>
</table>

<nav aria-label="Page navigation">
<ul id="pagination" class="pagination"></ul>
</nav>

<script src="app.js"></script>
</body>

Expand Down

0 comments on commit f4919de

Please sign in to comment.