content/hifi-content/Experiences/LoadTest/AppLoader/Apps/Feedback/feedback_loader.js
2022-02-13 23:16:46 +01:00

137 lines
No EOL
4.4 KiB
JavaScript

"use strict";
//
// feedbackApp.js
// tablet-app
//
// Integrated AppUi by Robin Wilson 10/11/2018
// Created by Mark Brosche and Robin Wilson on Aug. 31, 2018
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
/* global AccountServices */
(function () {
var ui;
var FEEDBACK_APP_DATA = "io.highfidelity.feedbackApp.data";
var FEEDBACK_CURRENT_EVENTNAME = "io.highfidelity.feedbackApp.currentEventName";
var currentEventName;
var VOTED = "voted";
var NOT_DONE_VOTING = "notdone";
var AppUi = Script.require('appUi');
var APP_NAME = "FEEDBACK";
var APP_URL = "https://hifi-content.s3.amazonaws.com/Experiences/LoadTest/FeedbackApp/AppScripts/feedbackAppWebPage.html?v2";
//Get a reference to the Google Sheets API
var GOOGLE_API_URL = "https://script.google.com/macros/s/AKfycbyWC4btUmE3LD6_dV2Nfkoc6C9FPKtRwtQRr23dNAMcX2ur-f0u/exec";
function startup() {
ui = new AppUi({
buttonName: APP_NAME,
home: APP_URL,
graphicsDirectory: Script.resolvePath("./icons/"),
onMessage: function (event) {
// Converts the event to a JavasScript Object
if (typeof event === "string") {
try {
event = JSON.parse(event);
} catch (e) {
console.error(e);
}
}
if (event.type === "click") {
// Define the parameters to output from HiFi to Google Sheets and send them.
var params = {
username: AccountServices.username,
displayName: MyAvatar.displayName,
date: Date(Date.now()),
event: currentEventName,
rating: event.data,
isApp: true,
UUID: MyAvatar.sessionUUID
};
sendInput(params);
}
}
});
}
// Send input
function sendInput(params) {
var paramString = encodeURLParams(params);
var request = new XMLHttpRequest();
request.open('GET', GOOGLE_API_URL + "?" + paramString);
request.timeout = 10000;
request.onreadystatechange = function () { // called after load when readyState = 4
if (request.readyState === 4) {
Settings.setValue(FEEDBACK_APP_DATA, {
eventName: currentEventName,
voted: VOTED
});
Script.setTimeout(function () {
// do the unload
ui.onScriptEnding();
}, 2000);
}
};
request.send();
}
// Encode params into a string to send.
function encodeURLParams(params) {
var paramPairs = [];
for (var key in params) {
paramPairs.push(key + "=" + params[key]);
}
return paramPairs.join("&");
}
function shouldLoadApp () {
var feedbackData = Settings.getValue(FEEDBACK_APP_DATA);
currentEventName = Settings.getValue(FEEDBACK_CURRENT_EVENTNAME);
var shouldLoad;
var voted = false;
// current event data is present
if (feedbackData && feedbackData.eventName === currentEventName) {
// voted is true if they voted, not true if they haven't
voted = feedbackData.voted === VOTED;
}
// current event data is not present, set the event data to the present event
if (!feedbackData || feedbackData.eventName !== currentEventName) {
Settings.setValue(FEEDBACK_APP_DATA, {
eventName: currentEventName,
voted: NOT_DONE_VOTING
});
}
// load app only if they have not voted
shouldLoad = !voted;
return shouldLoad;
}
if (shouldLoadApp()) {
// never loaded feedback app before
startup();
} else {
// loaded feedback but hasnt voted
// empty
}
}());