Skip to content

Commit bcfa60d

Browse files
laymonagethibaudcolas
authored andcommitted
Use API to display location operating status
1 parent dbbe8b4 commit bcfa60d

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

bakerydemo/locations/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def is_open(self):
192192
day=current_day,
193193
opening_time__lte=current_time,
194194
closing_time__gte=current_time,
195+
closed=False,
195196
)
196197
return True
197198
except LocationOperatingHours.DoesNotExist:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class LocationStatus extends HTMLElement {
2+
connectedCallback() {
3+
this.url = this.getAttribute('url');
4+
this.updateStatus();
5+
}
6+
7+
async updateStatus() {
8+
const data = await this.fetchPage();
9+
if (!data || typeof data.is_open !== 'boolean') {
10+
this.textContent =
11+
"Sorry, we couldn't retrieve the status of this location.";
12+
} else if (data.is_open) {
13+
this.textContent = 'This location is currently open.';
14+
} else {
15+
this.textContent = 'Sorry, this location is currently closed.';
16+
}
17+
}
18+
19+
fetchPage() {
20+
return fetch(this.url)
21+
.then((response) => response.json())
22+
.catch(() => null);
23+
}
24+
}
25+
26+
window.customElements.define('location-status', LocationStatus);

bakerydemo/templates/base.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
{% include "includes/footer.html" %}
5656

57-
<script type="module" src="{% static 'js/main.js' %}"></script>
57+
{% block js %}
58+
<script type="module" src="{% static 'js/main.js' %}"></script>
59+
{% endblock %}
5860
</body>
5961
</html>

bakerydemo/templates/locations/location_page.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "base.html" %}
2-
{% load wagtailimages_tags navigation_tags %}
2+
{% load wagtailimages_tags navigation_tags static %}
33

44
{% block content %}
55
{% include "base/include/header-hero.html" %}
@@ -25,11 +25,14 @@
2525
<div class="row">
2626
<div class="bread-detail__meta">
2727
<h2 class="location__meta-title">Operating Status</h2>
28-
{% if page.is_open %}
29-
This location is currently open.
30-
{% else %}
31-
Sorry, this location is currently closed.
32-
{% endif %}
28+
{% comment %}
29+
Fetch the status of the location on the client side
30+
as a Wagtail API usage example and to allow for
31+
caching of the whole page without the status.
32+
{% endcomment %}
33+
<location-status url="{% url 'wagtailapi:pages:detail' page.pk %}">
34+
Please wait...
35+
</location-status>
3336

3437
<h2 class="location__meta-title">Address</h2>
3538
<address>{{ page.address|linebreaks }}</address>
@@ -67,3 +70,8 @@ <h2 class="location__meta-title">Opening hours</h2>
6770
</div>
6871

6972
{% endblock content %}
73+
74+
{% block js %}
75+
{{ block.super }}
76+
<script type="module" src="{% static 'js/location-status.js' %}"></script>
77+
{% endblock js %}

0 commit comments

Comments
 (0)