Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // For full API documentation, including code examples, visit http://wix.to/94BuAAs
- import wixData from 'wix-data';
- import wixUsers from 'wix-users';
- import {resolveVanityUsername} from 'backend/FetchCalls';
- $w.onReady(function () {
- //TODO: write your page related code here...
- });
- function insertVote(W, X, Y, Z) {
- let toInsert = {
- "title": W,
- "targetId": X,
- "generalVote": Y,
- "comment": Z
- };
- wixData.insert("VoteMappingDB", toInsert)
- .then( (results) => {
- //let item = results;
- } )
- .catch( (err) => {
- let errorMsg = err;
- } );
- }
- function updateVote(id_, Y, Z) {
- let toUpdate = {
- "_id": id_,
- "generalVote": Y,
- "comment": Z
- };
- wixData.update("VoteMappingDB", toUpdate)
- .then( (results) => {
- //let item = results;
- } )
- .catch( (err) => {
- let errorMsg = err;
- } );
- }
- export function updateZeusDBSub(steamID, point) {
- wixData.query("ZeusDB")
- .eq("zeusId", steamID)
- .find()
- .then( (results) => {
- console.log("Query to ZeusDB found a result, updating existing entry");
- let index = results.items[0];
- let dvotes = index['downvotes']; // DOWNVOTES
- let uvotes = index['upvotes']; // UPVOTES
- if(point === -1) {dvotes++;}
- if(point === 1) {uvotes++;}
- if(point === 0) {}
- let item = {
- "_id": index['_id'],
- "zeusid": steamID,
- "upvotes": uvotes,
- "downvotes": dvotes
- }
- wixData.update("ZeusDB", item)
- .then( (results) => {
- let item = results; //see item below
- } )
- .catch( (err) => {
- let errorMsg = err;
- } );
- } )
- .catch( (err) => {
- let errorMsg = err;
- console.log("ERROR: " + errorMsg);
- console.log("Could not find any record with matching Key: " + steamID + ". Creating new entry.");
- if(point === 0){let uvotes2 = 0; let dvotes2 = 0;}
- if(point === 1){let uvotes2 = 1; let dvotes2 = 0;}
- if(point === -1){let uvotes2 = 0; let dvotes2 = 1;}
- let item2 = {
- "zeusid": steamID,
- "upvotes": uvotes2,
- "downvotes": dvotes2,
- }
- wixData.insert("ZeusDB", item2)
- .then( (results) => {
- let item = results; //see item below
- } )
- .catch( (err) => {
- let errorMsg = err;
- } );
- } );
- }
- export function submit_feedback_click(event) {
- //Add your code for this event here:
- let IDBox = $w("#input1").value;
- let goodnessIndex = $w("#RadioGroup1").selectedIndex;
- let comment = $w("#textBox1").value;
- console.log("IDBox: " + IDBox + " || goodnessIndex: " + goodnessIndex + " || comment: " + comment);
- if(IDBox === "" || goodnessIndex === -1) // Invalid user entry
- {
- // Player ID cannot be blank
- return;
- }
- resolveVanityUsername(IDBox)
- .then((steamIDJSON) => {
- console.log("Response JSON: " + steamIDJSON);
- if(steamIDJSON === "42") // Exit if not found
- {
- console.log("This Player ID does not exist");
- $w("#text7").text = "This Player ID does not exist";
- return;
- }
- // At this point, we have steamID = unique PID
- var genvote;
- if(goodnessIndex === 0) {genvote = 1;}
- if(goodnessIndex === 1) {genvote = -1;}
- if(goodnessIndex === 2) {genvote = 0;}
- console.log("genvote: " + genvote);
- // Check if player has already submitted against the entered PID
- console.log("steamID: " + steamIDJSON + " || Type: " + typeof steamIDJSON);
- wixData.query("VoteMappingDB")
- .eq("targetId", steamIDJSON)
- .find()
- .then( (results) => {
- // These results represent all of the comments made by this user
- //let index = results.items[0];
- console.log("YES WE FOUND A RECORD");
- let resultsSize = results.totalCount;
- var i;
- let t = 0;
- let x = false;
- for (i = 0; i < resultsSize; i++)
- {
- console.log(i);
- if (results.items[i]['title'].toString() === wixUsers.currentUser.id)
- {
- // If we get here, we found a match, and should do nothing (perhaps print something out to user)
- x = true;
- t = i;
- break;
- }
- }
- if(x)
- {
- console.log("X was true");
- // If we get here, we should INSERT a record into the VoteMappingDB Table
- insertVote(wixUsers.currentUser.id, steamIDJSON, genvote, comment);
- }
- else
- {
- console.log("X was false");
- // If we get here, we should UPDATE a record into the VoteMappingDB Table
- updateVote(results.items[t]['_id'], genvote, comment);
- }
- //$w("#text9").text = index['downvotes'].toString(); // DOWNVOTES
- //$w("#text10").text = index['upvotes'].toString(); // UPVOTES
- } )
- .catch( (err) => {
- let errorMsg = err;
- console.log("Could not find any record with matching Key: " + steamIDJSON);
- // If we get here, we should insert a record into the VoteMappingCB Table
- insertVote(wixUsers.currentUser.id, steamIDJSON, genvote, comment);
- } );
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement