added example of fetch

This commit is contained in:
Beverly Nguyen
2024-05-30 14:48:25 -07:00
parent ad68c26450
commit 108e889ac5
3 changed files with 20 additions and 17 deletions
+6 -8
View File
@@ -7,16 +7,14 @@
console.log('Connected to the server'); console.log('Connected to the server');
}); });
socket.on('message', function(msg) { // Listen for job updates from the server
var li = document.createElement("li"); socket.on('job_update', function(data) {
li.appendChild(document.createTextNode(msg)); console.log('Received job update:', data);
document.getElementById("messages").appendChild(li);
}); });
document.getElementById('sendButton').addEventListener('click', function() { document.getElementById('fetchJobsButton').addEventListener('click', function() {
var message = document.getElementById("message").value; const serviceId = 'b1226555-1f1a-472c-9086-043b0a69f4ec'; // Example service ID
socket.send(message); socket.emit('fetch_jobs', serviceId);
document.getElementById("message").value = '';
}); });
}); });
+12 -7
View File
@@ -6,7 +6,7 @@ from itertools import groupby
from flask import Response, abort, jsonify, render_template, request, session, url_for from flask import Response, abort, jsonify, render_template, request, session, url_for
from flask_login import current_user from flask_login import current_user
from flask_socketio import send, emit from flask_socketio import SocketIO, emit
from werkzeug.utils import redirect from werkzeug.utils import redirect
from app import ( from app import (
@@ -34,14 +34,20 @@ from app.utils.user import user_has_permissions
from notifications_utils.recipients import format_phone_number_human_readable from notifications_utils.recipients import format_phone_number_human_readable
@socketio.on('message') # @socketio.on('connect')
def handle_message(msg): # def handle_connect():
print('''Message: # print('Client connected')
# @socketio.on('disconnect')
# def handle_disconnect():
# print('Client disconnected')
''' + msg)
emit('message', msg, broadcast=True) @socketio.on('fetch_jobs')
def handle_fetch_jobs(service_id):
job_response = job_api_client.get_jobs(service_id)["data"]
emit('job_update', job_response)
@main.route("/services/<uuid:service_id>/dashboard") @main.route("/services/<uuid:service_id>/dashboard")
@@ -71,7 +77,6 @@ def service_dashboard(service_id):
job_id = notification.get("job", {}).get("id", None) job_id = notification.get("job", {}).get("id", None)
if job_id: if job_id:
aggregate_notifications_by_job[job_id].append(notification) aggregate_notifications_by_job[job_id].append(notification)
job_and_notifications = [ job_and_notifications = [
{ {
"job_id": job["id"], "job_id": job["id"],
+2 -2
View File
@@ -22,8 +22,8 @@
Messages sent Messages sent
</h2> </h2>
<input id="message" autocomplete="off"><button id="sendButton">Send</button> <h1>Job Dashboard</h1>
<ul id="messages"></ul> <button id="fetchJobsButton">Fetch Jobs</button>
{{ ajax_block(partials, updates_url, 'inbox') }} {{ ajax_block(partials, updates_url, 'inbox') }}