links.js 6.0 KB

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