-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-room.js
114 lines (86 loc) · 3.72 KB
/
add-room.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function initAddRoomPage() {
// Add code here to initialize the page
}
// Call the init function when the page is loaded
window.onload = initAddRoomPage;
async function populateRoomTypes() {
const response = await fetch('/api/room_types');
const roomTypes = await response.json();
const typeSelect = document.getElementById('type');
roomTypes.forEach(roomType => {
const option = document.createElement('option');
option.value = roomType.type_name;
option.textContent = roomType.type_name;
typeSelect.appendChild(option);
});
}
populateRoomTypes();
async function onSubmit(event) {
event.preventDefault();
const admin_notes = document.getElementById('admin_notes').value;
const floor = document.getElementById('floor').value;
const room_notes = document.getElementById('room_notes').value;
const room_number = document.getElementById('room_number').value;
const room_status = document.getElementById('room_status').value;
const type = document.getElementById('type').value;
const response = await fetch('/api/rooms', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
admin_notes,
floor,
room_notes,
room_number,
room_status,
type
}),
});
if (response.ok) {
alert('Room added successfully');
// Clear form fields after successful submission
document.getElementById('admin_notes').value = '';
document.getElementById('floor').value = '';
document.getElementById('room_notes').value = '';
document.getElementById('room_number').value = '';
document.getElementById('room_status').value = '';
document.getElementById('type').value = '';
} else {
alert('Failed to add room');
}
}
async function searchStudents() {
const searchType = document.getElementById('student_search_type').value;
const searchTerm = document.getElementById('student_search_term').value;
const response = await fetch(`/api/students/search?type=${searchType}&term=${searchTerm}`);
const students = await response.json();
const resultsContainer = document.getElementById('search_results'); // Assuming you have a container for the results with id 'search_results'.
resultsContainer.innerHTML = ''; // Clear previous results.
students.forEach(student => {
const studentElement = document.createElement('div');
studentElement.textContent = `${student.first_name} ${student.last_name} (${student.pratt_id})`;
const assignButton = document.createElement('button');
assignButton.textContent = 'Assign to Room';
assignButton.addEventListener('click', () => assignStudent(student.pratt_id));
studentElement.appendChild(assignButton);
resultsContainer.appendChild(studentElement);
});
}
async function assignStudent(studentId) {
const roomId = document.getElementById('room_id').value;
const response = await fetch(`/api/rooms/${roomId}/assignees/${studentId}`, {
method: 'PUT'
});
if (response.ok) {
alert('Student assigned successfully');
const studentResponse = await fetch(`/api/students/${studentId}`);
const student = await studentResponse.json();
const assignedStudentsContainer = document.getElementById('assigned_students');
const studentElement = document.createElement('div');
studentElement.textContent = `${student.first_name} ${student.last_name} (${student.pratt_id})`;
assignedStudentsContainer.appendChild(studentElement);
} else {
alert('Failed to assign student');
}
}