Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #62: Alert message on API error #65

Merged
merged 10 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions climbdex/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from flask_parameter_validation import ValidateParameters, Query

import boardlib.api.aurora
import flask
import requests
Expand All @@ -6,6 +8,18 @@

blueprint = flask.Blueprint("api", __name__)

def error_handler(err):
details = str(err)
error_type = str(type(err).__name__)
message = f"There was an issue getting results from the api. If the issue persists please <a href=\"https://github.com/lemeryfertitta/Climbdex/issues/new?title={str(type(err).__name__)}: {str(err)}\" target='_blank'>report it</a>"

return {
"error": True,
"details": details,
"type": error_type,
"message": message,
}, 400


@blueprint.route("/api/v1/<board_name>/layouts")
def layouts(board_name):
Expand All @@ -29,13 +43,36 @@ def sets(board_name, layout_id, size_id):


@blueprint.route("/api/v1/search/count")
def resultsCount():
return flask.jsonify(climbdex.db.get_search_count(flask.request.args))

@ValidateParameters(error_handler)
def resultsCount(
gradeAccuracy: float = Query(),
layout: int = Query(),
maxGrade: int = Query(),
minAscents: int = Query(),
minGrade: int = Query(),
minRating: float = Query(),
size: int = Query(),
):
try:
return flask.jsonify(climbdex.db.get_search_count(flask.request.args))
except Exception as e:
return error_handler(e)

@blueprint.route("/api/v1/search")
def search():
return flask.jsonify(climbdex.db.get_search_results(flask.request.args))
@ValidateParameters(error_handler)
def search(
gradeAccuracy: float = Query(),
layout: int = Query(),
maxGrade: int = Query(),
minAscents: int = Query(),
minGrade: int = Query(),
minRating: float = Query(),
size: int = Query(),
):
try:
return flask.jsonify(climbdex.db.get_search_results(flask.request.args))
except Exception as e:
return error_handler(e)


@blueprint.route("/api/v1/<board_name>/beta/<uuid>")
Expand Down
3 changes: 3 additions & 0 deletions climbdex/static/js/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var el = document.getElementById('toast')
var toast = bootstrap.Toast.getOrCreateInstance(el)

function drawBoard(
svgElementId,
imagesToHolds,
Expand Down
20 changes: 17 additions & 3 deletions climbdex/static/js/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ async function fetchResultsCount() {
const urlParams = new URLSearchParams(window.location.search);
const response = await fetch("/api/v1/search/count?" + urlParams);
const resultsCount = await response.json();
return resultsCount;

if (resultsCount['error'] == true) {
el.classList.add('bg-danger')
el.querySelector('.toast-body').innerHTML = resultsCount['message']
toast.show()
} else {
return resultsCount;
}
}

async function fetchResults(pageNumber, pageSize) {
Expand All @@ -95,7 +102,14 @@ async function fetchResults(pageNumber, pageSize) {
urlParams.append("pageSize", pageSize);
const response = await fetch("/api/v1/search?" + urlParams);
const results = await response.json();
return results;

if (results['error'] == true) {
el.classList.add('bg-danger')
el.querySelector('.toast-body').innerHTML = results['message']
toast.show()
} else {
return results;
}
}

function clickClimbButton(index, pageSize, resultsCount) {
Expand Down Expand Up @@ -230,7 +244,7 @@ function drawResultsPage(results, pageNumber, pageSize, resultsCount) {
}

const backAnchor = document.getElementById("anchor-back");
backAnchor.href = location.origin + "/filter?" + location.search;
backAnchor.href = location.origin + "/filter" + location.search;
brmnjsh marked this conversation as resolved.
Show resolved Hide resolved
if (document.referrer && new URL(document.referrer).origin == location.origin) {
backAnchor.addEventListener("click", function (event) {
event.preventDefault();
Expand Down
4 changes: 4 additions & 0 deletions climbdex/templates/beta.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</head>

<body>
{% include 'toast.html.j2' %}
<div class="container text-center">
{% include 'heading.html.j2' %}
<div class="row justify-content-md-center">
Expand Down Expand Up @@ -52,6 +53,9 @@
{% include 'footer.html.j2'%}
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<script src="{{url_for('static', filename='js/beta.js')}}"></script>
<script src="{{url_for('static', filename='js/swipe.js')}}"></script>

Expand Down
5 changes: 4 additions & 1 deletion climbdex/templates/boardSelection.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</head>

<body>
{% include 'toast.html.j2' %}
<div class="container-sm text-center">
{% include 'heading.html.j2' %}
<div class="row justify-content-md-center">
Expand Down Expand Up @@ -100,7 +101,9 @@
</div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<script src="{{url_for('static', filename='js/boardSelection.js')}}"></script>
</body>

Expand Down
6 changes: 5 additions & 1 deletion climbdex/templates/climbCreation.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</head>

<body>
{% include 'toast.html.j2' %}
brmnjsh marked this conversation as resolved.
Show resolved Hide resolved
<div class="container-sm text-center">
{% include 'heading.html.j2' %}
<div class="row justify-content-md-center">
Expand All @@ -33,9 +34,12 @@
<div class="row justify-content-md-center mt-3">
{% include 'footer.html.j2' %}
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<script src="{{url_for('static', filename='js/common.js')}}"></script>
<script src="{{url_for('static', filename='js/bluetooth.js')}}"></script>
<script src="{{url_for('static', filename='js/climbCreation.js')}}"></script>
<script src="{{url_for('static', filename='js/common.js')}}"></script>
<script>
const appUrl = "{{ app_url }}";
const colors = {{ colors | tojson}};
Expand Down
1 change: 1 addition & 0 deletions climbdex/templates/filterSelection.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<script src="{{url_for('static', filename='js/nouislider.min.js')}}"></script>

<body>
{% include 'toast.html.j2' %}
<div class="container-sm text-center">
{% include 'heading.html.j2' %}
<div class="row justify-content-md-center">
Expand Down
4 changes: 4 additions & 0 deletions climbdex/templates/results.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</head>

<body>
{% include 'toast.html.j2' %}
<div class="container-sm text-center">
{% include 'heading.html.j2' %}
<div class="row justify-content-md-center">
Expand Down Expand Up @@ -76,6 +77,9 @@
<div class="row justify-content-md-center mt-3">
{% include 'footer.html.j2' %}
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<script src="{{url_for('static', filename='js/bluetooth.js')}}"></script>
<script src="{{url_for('static', filename='js/common.js')}}"></script>
<script>
Expand Down
8 changes: 8 additions & 0 deletions climbdex/templates/toast.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="toast-container p-3 top-0 end-0">
<div class="toast text-white" id="toast">
<div class="d-flex">
<div class="toast-body"></div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ typing_extensions==4.9.0
urllib3==2.1.0
Werkzeug==3.0.1
zipp==3.17.0
flask_parameter_validation==2.3.1