Skip to content

Commit

Permalink
Now pulls the available slots to populate the calendar via AJAX from …
Browse files Browse the repository at this point in the history
…the API server. Part of #9
  • Loading branch information
Adrian McEwen committed Jan 5, 2021
1 parent b864a4d commit 8fd9c68
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions api/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');
const cors = require('cors');
const app = express();

require('dotenv').config();
Expand All @@ -9,6 +10,7 @@ const bookingRoutes = require('./routes/bookings');
const calendarRoutes = require('./routes/calendar');
const slotRoutes = require('./routes/slots');

app.use(cors());
app.use(express.json());
app.use('/api/resources', resourceRoutes);
app.use('/api/bookings', bookingRoutes);
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"homepage": "https://github.com/DoESLiverpool/optimism#readme",
"dependencies": {
"axios": "^0.20.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"fullcalendar": "^5.3.2",
Expand Down
2 changes: 1 addition & 1 deletion website/routes/select-a-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ router.get('/select-a-time', function (req, res) {

const resourceUrl = `${apiUrl}/resources/${resourceId}`;
const calendarUrl = `${apiUrl}/calendar/${fromDate.format('YYYY-MM-DD')}/${toDate.format('YYYY-MM-DD')}/${resourceId}`;
const templateVariables = {};
const templateVariables = { apiUrl: settings.apiUrl };

axios.get(resourceUrl)
.then(function (response) {
Expand Down
35 changes: 21 additions & 14 deletions website/templates/select-a-time.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@

{% block header %}
<link href='/css/main.css' rel='stylesheet' />
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src='/scripts/main.js'></script>
<script>

document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'timeGridWeek',
events: [
{% for d in calendar.dates %}
{% for r in d.resources %}
{% for s in r.slots %}
{
title: '{{r.resourceName}} - {{s.status}}',
start: '{{s.starts}}',
end: '{{s.ends}}',
allDay: false
},
{% endfor %}
{% endfor %}
{% endfor %}
]
events: function(info, successCallback, failureCallback) {
axios.get("{{apiUrl}}/calendar/"+info.startStr.substr(0,info.startStr.indexOf("T"))+"/"+info.endStr.substr(0,info.endStr.indexOf("T"))+"/{{resource.id}}")
.then(function(details) {
var events = [];
details.data.dates.forEach(function(d) {
d.resources.forEach(function(r) {
r.slots.forEach(function(s) {
events.push({
title: r.resourceName+" - "+s.status,
start: s.starts,
end: s.ends,
allDay: false
});
});
});
});
successCallback(events);
})
.catch(failureCallback);
}
});
calendar.render();
});
Expand Down

0 comments on commit 8fd9c68

Please sign in to comment.