links.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const toggleVisible = ele => ele.style.display = ele.style.display === 'none' ? 'block' : 'none'
  2. const toggleClass = (ele, class1, class2) => ele.className = ele.className === class1 ? class2 : class1
  3. const guildIdField = document.getElementById('guildId')
  4. const userIdField = document.getElementById('userId')
  5. const generateButton = document.getElementById('generateButton')
  6. const generateInfo = document.getElementById('generateInfo')
  7. const userIdFeedback = document.getElementById('userIdFeedback')
  8. async function loadExperiment(){
  9. var urlParams = new URLSearchParams(window.location.search);
  10. if(urlParams.get('id')){
  11. current_guild_id = urlParams.get('id')
  12. for (let option of guildIdField.options){
  13. if (option.value == current_guild_id){
  14. option.defaultSelected = true
  15. }
  16. }
  17. }
  18. }
  19. // check enter key issue
  20. async function searchUserId(){
  21. // reinit feedback div and form
  22. // userIdFeedback.innerText = ''
  23. userIdField.className = 'form-control'
  24. generateButton.disabled = true
  25. // get form value
  26. let guildId = guildIdField.value
  27. let userId = userIdField.value
  28. if (!userId.length){
  29. return
  30. }
  31. const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value
  32. let data = {'guildId': guildId, 'userId': userId}
  33. fetch('/check', {
  34. method: 'POST',
  35. body: JSON.stringify(data),
  36. headers: {
  37. 'Content-type': 'application/json; charset=utf-8',
  38. 'X-CSRFToken': csrfToken
  39. }
  40. })
  41. .then((resp) =>
  42. resp.json()
  43. )
  44. .then(res => {
  45. // update status field and enable link generation if valid
  46. userIdFeedback.innerText = res.message
  47. if (res.status){
  48. userIdField.className = 'form-control is-valid'
  49. userIdFeedback.className = 'valid-feedback'
  50. generateButton.disabled = false
  51. }else{
  52. userIdField.className = 'form-control is-invalid'
  53. userIdFeedback.className = 'invalid-feedback'
  54. generateButton.disabled = true
  55. }
  56. })
  57. }
  58. async function guildIdChanged(){
  59. // reinit field if guild value changed
  60. userIdField.className = 'form-control'
  61. userIdFeedback.className = ''
  62. userIdFeedback.innerText = ''
  63. userIdField.value = ''
  64. generateButton.disabled = true
  65. }
  66. async function generateLink(){
  67. // reinit field if guild value changed
  68. let guildId = guildIdField.value
  69. let userId = userIdField.value
  70. const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value
  71. let data = {'guildId': guildId, 'userId': userId}
  72. fetch('/generate', {
  73. method: 'POST',
  74. body: JSON.stringify(data),
  75. headers: {
  76. 'Content-type': 'application/json; charset=utf-8',
  77. 'X-CSRFToken': csrfToken
  78. }
  79. })
  80. .then((resp) =>
  81. resp.json()
  82. )
  83. .then(res => {
  84. // update status field and enable link generation if valid
  85. console.log(res)
  86. user_config = res.config
  87. user_link = res.link
  88. if (res.status){
  89. // Add link to local storage
  90. var localStorage = window.localStorage
  91. // link composed of {'experimentId', 'link'}
  92. expes_link = {}
  93. // retrieve expe links if exists
  94. if (localStorage.getItem('p3d-user-links')){
  95. expes_link = JSON.parse(localStorage.getItem('p3d-user-links'))
  96. }
  97. if (!expes_link[user_config.experimentName]){
  98. // create for this experiment
  99. expes_link[user_config.experimentName] = {}
  100. }
  101. if (!expes_link[user_config.experimentName][user_config.experimentId]){
  102. // create for this experiment and experiment id
  103. expes_link[user_config.experimentName][user_config.experimentId] = {}
  104. }
  105. if (!expes_link[user_config.experimentName][user_config.experimentId][user_config.userId]){
  106. // create for this experiment, experiment id and user id
  107. expes_link[user_config.experimentName][user_config.experimentId][user_config.userId] = {'config': user_config, 'link': user_link}
  108. }
  109. localStorage.setItem('p3d-user-links', JSON.stringify(expes_link))
  110. generateInfo.className = 'alert alert-dismissible alert-success'
  111. generateInfo.style.display = 'block'
  112. generateInfo.innerHTML = '<button id="generateInfoClose" type="button" class="close" data-dismiss="alert">&times;</button> \
  113. Well done! You successfully generate <a href="' + user_link + '" class="alert-link">your experiment link</a>. \
  114. <strong>This link is unique and associated with your browser</strong>..'
  115. }else{
  116. generateInfo.className = 'alert alert-dismissible alert-danger'
  117. generateInfo.style.display = 'block'
  118. generateInfo.innerHTML = '<button id="generateInfoClose" type="button" class="close" data-dismiss="alert">&times;</button> \
  119. <strong>An error occured!</strong> ' + res.message + '.'
  120. }
  121. let generateInfoClose = document.getElementById('generateInfoClose')
  122. // link function
  123. generateInfoClose.addEventListener('click', (e) => {
  124. toggleVisible(generateInfo)
  125. })
  126. // reset form
  127. loadLinks()
  128. guildIdChanged()
  129. })
  130. }
  131. // check by default
  132. searchUserId()
  133. document.addEventListener('DOMContentLoaded', loadExperiment)
  134. userIdField.addEventListener('keyup', searchUserId)
  135. guildIdField.addEventListener('change', guildIdChanged)
  136. generateButton.addEventListener('click', generateLink)
  137. // implement `key` events
  138. // document.addEventListener('keydown', checkKey)