Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. // For full API documentation, including code examples, visit http://wix.to/94BuAAs
  2.  
  3. import wixData from 'wix-data';
  4. import wixUsers from 'wix-users';
  5. import {resolveVanityUsername} from 'backend/FetchCalls';
  6.  
  7. $w.onReady(function () {
  8. //TODO: write your page related code here...
  9.  
  10. });
  11.  
  12. function insertVote(W, X, Y, Z) {
  13. let toInsert = {
  14. "title": W,
  15. "targetId": X,
  16. "generalVote": Y,
  17. "comment": Z
  18. };
  19. wixData.insert("VoteMappingDB", toInsert)
  20. .then( (results) => {
  21. //let item = results;
  22. } )
  23. .catch( (err) => {
  24. let errorMsg = err;
  25. } );
  26. }
  27.  
  28. function updateVote(id_, Y, Z) {
  29. let toUpdate = {
  30. "_id": id_,
  31. "generalVote": Y,
  32. "comment": Z
  33. };
  34. wixData.update("VoteMappingDB", toUpdate)
  35. .then( (results) => {
  36. //let item = results;
  37. } )
  38. .catch( (err) => {
  39. let errorMsg = err;
  40. } );
  41. }
  42.  
  43. export function updateZeusDBSub(steamID, point) {
  44. wixData.query("ZeusDB")
  45. .eq("zeusId", steamID)
  46. .find()
  47. .then( (results) => {
  48. console.log("Query to ZeusDB found a result, updating existing entry");
  49. let index = results.items[0];
  50. let dvotes = index['downvotes']; // DOWNVOTES
  51. let uvotes = index['upvotes']; // UPVOTES
  52. if(point === -1) {dvotes++;}
  53. if(point === 1) {uvotes++;}
  54. if(point === 0) {}
  55. let item = {
  56. "_id": index['_id'],
  57. "zeusid": steamID,
  58. "upvotes": uvotes,
  59. "downvotes": dvotes
  60. }
  61. wixData.update("ZeusDB", item)
  62. .then( (results) => {
  63. let item = results; //see item below
  64. } )
  65. .catch( (err) => {
  66. let errorMsg = err;
  67. } );
  68. } )
  69. .catch( (err) => {
  70. let errorMsg = err;
  71. console.log("ERROR: " + errorMsg);
  72. console.log("Could not find any record with matching Key: " + steamID + ". Creating new entry.");
  73. if(point === 0){let uvotes2 = 0; let dvotes2 = 0;}
  74. if(point === 1){let uvotes2 = 1; let dvotes2 = 0;}
  75. if(point === -1){let uvotes2 = 0; let dvotes2 = 1;}
  76. let item2 = {
  77. "zeusid": steamID,
  78. "upvotes": uvotes2,
  79. "downvotes": dvotes2,
  80. }
  81. wixData.insert("ZeusDB", item2)
  82. .then( (results) => {
  83. let item = results; //see item below
  84. } )
  85. .catch( (err) => {
  86. let errorMsg = err;
  87. } );
  88. } );
  89. }
  90.  
  91. export function submit_feedback_click(event) {
  92. //Add your code for this event here:
  93. let IDBox = $w("#input1").value;
  94. let goodnessIndex = $w("#RadioGroup1").selectedIndex;
  95. let comment = $w("#textBox1").value;
  96. console.log("IDBox: " + IDBox + " || goodnessIndex: " + goodnessIndex + " || comment: " + comment);
  97. if(IDBox === "" || goodnessIndex === -1) // Invalid user entry
  98. {
  99. // Player ID cannot be blank
  100. return;
  101. }
  102.  
  103. resolveVanityUsername(IDBox)
  104. .then((steamIDJSON) => {
  105. console.log("Response JSON: " + steamIDJSON);
  106.  
  107.  
  108. if(steamIDJSON === "42") // Exit if not found
  109. {
  110. console.log("This Player ID does not exist");
  111. $w("#text7").text = "This Player ID does not exist";
  112. return;
  113. }
  114. // At this point, we have steamID = unique PID
  115.  
  116. var genvote;
  117. if(goodnessIndex === 0) {genvote = 1;}
  118. if(goodnessIndex === 1) {genvote = -1;}
  119. if(goodnessIndex === 2) {genvote = 0;}
  120. console.log("genvote: " + genvote);
  121.  
  122. // Check if player has already submitted against the entered PID
  123. console.log("steamID: " + steamIDJSON + " || Type: " + typeof steamIDJSON);
  124. wixData.query("VoteMappingDB")
  125. .eq("targetId", steamIDJSON)
  126. .find()
  127. .then( (results) => {
  128. // These results represent all of the comments made by this user
  129. //let index = results.items[0];
  130. console.log("YES WE FOUND A RECORD");
  131. let resultsSize = results.totalCount;
  132. var i;
  133. let t = 0;
  134. let x = false;
  135. for (i = 0; i < resultsSize; i++)
  136. {
  137. console.log(i);
  138. if (results.items[i]['title'].toString() === wixUsers.currentUser.id)
  139. {
  140. // If we get here, we found a match, and should do nothing (perhaps print something out to user)
  141. x = true;
  142. t = i;
  143. break;
  144. }
  145. }
  146. if(x)
  147. {
  148. console.log("X was true");
  149. // If we get here, we should INSERT a record into the VoteMappingDB Table
  150. insertVote(wixUsers.currentUser.id, steamIDJSON, genvote, comment);
  151. }
  152. else
  153. {
  154. console.log("X was false");
  155. // If we get here, we should UPDATE a record into the VoteMappingDB Table
  156. updateVote(results.items[t]['_id'], genvote, comment);
  157. }
  158. //$w("#text9").text = index['downvotes'].toString(); // DOWNVOTES
  159. //$w("#text10").text = index['upvotes'].toString(); // UPVOTES
  160. } )
  161. .catch( (err) => {
  162. let errorMsg = err;
  163. console.log("Could not find any record with matching Key: " + steamIDJSON);
  164. // If we get here, we should insert a record into the VoteMappingCB Table
  165. insertVote(wixUsers.currentUser.id, steamIDJSON, genvote, comment);
  166. } );
  167.  
  168. });
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement