Skip to content

Commit

Permalink
Improved messages, and added authorization token to all js api call h…
Browse files Browse the repository at this point in the history
…eaders
  • Loading branch information
klpulliam-37 committed May 3, 2023
1 parent fb04bd3 commit 4e968ef
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 30 deletions.
70 changes: 49 additions & 21 deletions UserInterface/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class GreenhouseProxy {
const options = {
method: 'POST',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
Expand Down Expand Up @@ -92,8 +93,14 @@ export class GreenhouseProxy {

// Get all users
async getUsers() {
const options = {
headers: {
'Authorization': token
}
}

try {
let response = await fetch(`/users`);
let response = await fetch(`/users`, options);
if (response.ok) {
let data = await response.json();

Expand All @@ -116,6 +123,7 @@ export class GreenhouseProxy {
const options = {
method: 'PATCH',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
Expand All @@ -136,7 +144,10 @@ export class GreenhouseProxy {
// Delete user by id
async deleteUser(userID) {
const options = {
method: 'DELETE'
method: 'DELETE',
headers: {
'Authorization': token
}
}

try {
Expand All @@ -153,8 +164,14 @@ export class GreenhouseProxy {
// ------------------ROOM------------------ //
// Get all rooms
async getRooms() {
const options = {
headers: {
'Authorization': token
}
}

try {
let response = await fetch(`/rooms`);
let response = await fetch(`/rooms`, options);
return await response.json();
} catch (error) {
console.log(error);
Expand All @@ -167,6 +184,7 @@ export class GreenhouseProxy {
const options = {
method: 'POST',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(room)
Expand All @@ -184,8 +202,14 @@ export class GreenhouseProxy {

// Get room by id
async getRoomByID(roomID) {
const options = {
headers: {
'Authorization': token
}
}

try {
let response = await fetch(`/rooms/${roomID}`);
let response = await fetch(`/rooms/${roomID}`, options);
if (response.ok) {
let data = await response.json();
return data;
Expand All @@ -201,6 +225,7 @@ export class GreenhouseProxy {
const options = {
method: 'PATCH',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(name)
Expand Down Expand Up @@ -242,6 +267,7 @@ export class GreenhouseProxy {
const options = {
method: 'PUT',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(dateObj)
Expand All @@ -263,6 +289,7 @@ export class GreenhouseProxy {
const options = {
method: 'POST',
headers: {
'Authorization': token,
'Content-Type': 'application/json',
'Key': 'TRD1IU4G5E45RZU8UOXNNMR7WID34Q7SM0EFHV6FHZS9PQBNYXINTTS1BSR8' // Agent Key for Room 1
},
Expand All @@ -284,6 +311,7 @@ export class GreenhouseProxy {
getAllMessages() {
const options = {
headers: {
'Authorization': token,
'Content-Type': 'application/json'
}
}
Expand All @@ -297,6 +325,7 @@ export class GreenhouseProxy {
async getAllMessagesByRoom(roomID) {
const options = {
headers: {
'Authorization': token,
'Content-Type': 'application/json'
}
}
Expand All @@ -322,6 +351,7 @@ export class GreenhouseProxy {
const options = {
method: 'POST',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(_body)
Expand All @@ -344,8 +374,14 @@ export class GreenhouseProxy {
// ------------------EXPERIMENT------------------
// Get all experiments
async getExperiments() {
const options = {
headers: {
'Authorization': token,
}
}

try {
let response = await fetch(`/experiments`);
let response = await fetch(`/experiments`, options);
if (response.ok) {
let data = await response.json();
return data;
Expand All @@ -360,6 +396,7 @@ export class GreenhouseProxy {
const options = {
method: 'POST',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(experimentObj)
Expand All @@ -377,6 +414,12 @@ export class GreenhouseProxy {

// Get experiment by id
async getExperimentByID(experimentID) {
const options = {
headers: {
'Authorization': token
}
}

try {
let response = await fetch(`/experiments/${experimentID}`, options);
if (response.ok) {
Expand All @@ -393,6 +436,7 @@ export class GreenhouseProxy {
const options = {
method: 'PATCH',
headers: {
'Authorization': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(experimentObj)
Expand Down Expand Up @@ -459,19 +503,3 @@ export class GreenhouseProxy {

// Delete an agent by id
}

// const proxy = new GreenhouseProxy();
// proxy.registerUser(userRegister2);
// proxy.registerUser(userRegister3);
// proxy.login();
// proxy.getUsers();
// proxy.createRoom("Room 1");
// proxy.getRooms();
// proxy.deleteRoom(1);
// proxy.createMeasurement(1, measurement);
// proxy.getAllMessages();
// proxy.getAllMessagesByRoom(1);
// proxy.getAllMessagesByRoom(2);
// proxy.getAllMessagesByRoom(3);
// proxy.getAllMessagesByRoom(4);
// proxy.createRoomMessage(4, 1, "That sounds like a great idea. Let's do it!");
14 changes: 10 additions & 4 deletions UserInterface/js/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function renderMeasurements() {
exportDataBtn.addEventListener('click', () => {
const csvData = Utils.csvMaker(measurements);
// console.log(csvData);

Utils.download(csvData['csv_data'], csvData['file_name']);
});

Expand All @@ -62,9 +62,15 @@ async function renderMeasurements() {
const day = date.getUTCDate();
const month = date.getUTCMonth();
const hours = date.getUTCHours();
const minutes = date.getUTCMinutes();
const seconds = date.getUTCSeconds();
const timestamp = `${day} ${Utils.months[month]} ${Utils.toStandardTime(hours)}:${minutes}:${seconds}`;
let minutes = date.getUTCMinutes();
if (minutes < 10) {
minutes = '0' + minutes;
}
let seconds = date.getUTCSeconds();
if (seconds < 10) {
seconds = '0' + seconds;
}
const timestamp = `${day} ${Utils.months[month]} ${Utils.toStandardTime(hours)['hours']}:${minutes}:${seconds}`;
dates.push(timestamp);

const temperatureValue = measurement['temperature'];
Expand Down
25 changes: 23 additions & 2 deletions UserInterface/js/roomMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,42 @@ async function renderMessages() {

if (messages.length) {
messages.forEach(message => {
const user = message["user_id"];
const textContainer = document.createElement('div');
textContainer.setAttribute('class', 'd-flex align-items-baseline mb-4');
const container2 = document.createElement('div');
container2.setAttribute('class', 'pe-2');
const textCard = document.createElement('div');
textCard.setAttribute('class', 'card card-text d-inline-block p-2 px-3 m-1');
// const info = document.createElement('div');
// info.setAttribute('class', 'd-flex');
const user = document.createElement('div');
user.setAttribute('class', 'small');
user.textContent = usernames[message['user_id']];
const date = document.createElement('div');
date.setAttribute('class', 'small');
date.textContent = usernames[message['user_id']];

const dateObj = new Date(message['timestamp']);
const day = dateObj.getDate();
const month = Utils.months[dateObj.getMonth()];
const year = dateObj.getFullYear();
const hoursObj = Utils.toStandardTime(dateObj.getHours());
let minutes = dateObj.getMinutes();
if (minutes < 10) {
minutes = '0' + minutes;
}
const hours = hoursObj['hours'];
const amPm = hoursObj['amPm'];
const dateStr = `${hours}:${minutes} ${amPm} - ${day} ${month}, ${year}`;

date.textContent = dateStr;

textCard.textContent = message["body"];

container2.append(user);
container2.append(textCard);
container2.append(date);
// info.append(user);
// info.append(date);

textContainer.append(container2);

Expand Down
16 changes: 13 additions & 3 deletions UserInterface/js/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ export function logout() {

export function toStandardTime(_hours) {
let hours = _hours;
let amPm = '';
if (hours > 12) {
amPm = 'AM';
hours -= 12;
} else if (hours == 0) {
timeValue= "12";
amPm = 'AM';
hours = "12";
}else{
amPm = 'PM';
}

return hours;

const hoursObj = {
hours: hours,
amPm: amPm
}

return hoursObj;
}

export function download(data, filename) {
Expand Down

0 comments on commit 4e968ef

Please sign in to comment.