main.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //Chat
  2. /*
  3. {
  4. let inputNick = document.createElement('input')
  5. inputNick.placeholder = 'Your nick'
  6. let inputMessage = document.createElement('input')
  7. inputMessage.placeholder = 'Your message'
  8. let button = document.createElement('button')
  9. button.innerText = 'SEND'
  10. let divBox = document.createElement('div')
  11. divBox.style.width = '50%'
  12. divBox.style.height = '100%'
  13. divBox.style.border = '2px solid'
  14. document.body.append(inputNick,inputMessage,button,divBox)
  15. let nextMessageId = 0
  16. let nickValue,messageValue
  17. function nickHandler() {
  18. nickValue = inputNick.value
  19. }
  20. inputNick.addEventListener('input', nickHandler)
  21. function messageHandler (){
  22. messageValue = inputMessage.value
  23. }
  24. inputMessage.addEventListener('input', messageHandler)
  25. async function sendMessage (nick,message){
  26. let messages = await jsonPost('http://students.a-level.com.ua:10012', {func: "addMessage", nick: nick, message: message})
  27. let numberOfMessages = messages.nextMessageId
  28. if(nextMessageId < numberOfMessages){
  29. nextMessageId = numberOfMessages
  30. }
  31. else {
  32. nextMessageId = null
  33. }
  34. return messages
  35. }
  36. //stage 2
  37. async function getMessages(){
  38. let messages = await jsonPost('http://students.a-level.com.ua:10012', {func: "getMessage", nextMessageId: 0})
  39. numberOfMessages = messages.nextMessageId
  40. for (let element in messages.data){
  41. let divMessage = document.createElement('div')
  42. divBox.append(divMessage)
  43. let timestamp = new Date(messages.data[element].timestamp).toLocaleString()
  44. divMessage.innerHTML = `${messages.data[el].nick} : ${messages.data[el].message} at ${timestamp}`
  45. }
  46. return messages.data
  47. }
  48. //stage 3
  49. async function getNewMessages(){
  50. let messages = await jsonPost('http://students.a-level.com.ua:10012', {func: "getMessages", nextMessageId: 0})
  51. let numberOfMessages = messages.nextMessageId
  52. if(nextMessageId < numberOfMessages){
  53. nextMessageId = numberOfMessages
  54. }
  55. else {
  56. nextMessageId = null
  57. }
  58. for (let element in messages.data){
  59. let divMessage = document.createElement('div')
  60. divBox.prepend(divMessage)
  61. let timestamp = new Date(messages.data[element].timestamp).toLocaleString()
  62. divMessage.innerHTML = `${messages.data[element].nick} : ${messages.data[element].message} at ${timestamp}`
  63. divMessage.style.color = `rgb(${Math.random()*255},${Math.random()*255},${Math.random()*255})`
  64. }
  65. return messages.data
  66. }
  67. //stage 4
  68. setInterval(getNewMessages(),4000)
  69. //stage 5
  70. button.addEventListener('click',buttonHandler)
  71. function buttonHandler(){
  72. sendAndCheck(nickValue, messageValue)
  73. }
  74. async function sendAndCheck(nick, message){
  75. await Promise.all([sendMessage(nick,message),getMessages()])
  76. }
  77. async function checkLoop (){
  78. function delay(ms){
  79. function executor(fulfill,reject){
  80. setTimeout(() => fulfill(ms), ms)
  81. }
  82. return new Promise(executor)
  83. }
  84. while(true){
  85. await delay(3000).then(()=> getNewMessages())
  86. }
  87. }
  88. checkLoop()
  89. //stage 6
  90. function jsonPost (url,data){
  91. return new Promise((resolve,reject) => {
  92. fetch(url,{method: 'POST',body: JSON.stringify(data)})
  93. .then(responce =>responce.json())
  94. .then(responce => resolve(responce),()=> reject(new Error('JSONPost failed')))
  95. })
  96. }
  97. }
  98. */
  99. //SWAPI Links
  100. {
  101. async function swapiLinks(url){
  102. let data = await fetchMyLink(url)
  103. console.log(data)
  104. let result = {}
  105. let arr = []
  106. for(let key in data){
  107. if(Array.isArray(data[key])){
  108. arr.push(Promise.all(data.key.map(fetchMyLink)).then(res => result[key]= res))
  109. }
  110. else if (typeof(data[key]) === 'string' && (data[key].includes('https://swapi.dev/api/'))){
  111. arr.push(fetchMyLink(data[key]).then(res => result[key] = res))
  112. }
  113. else result[key] = data[key]
  114. }
  115. return Promise.all(arr).then(()=>result)
  116. }
  117. async function fetchMyLink(link){
  118. let result = await fetch(link).then(res => res.json)
  119. return result
  120. }
  121. swapiLinks("https://swapi.dev/api/people/20/")
  122. .then(yodaWithLinks => console.log(JSON.stringify(yodaWithLinks, null, 4)))
  123. }
  124. //domEventPromise
  125. {
  126. let knopka = document.createElement('button')
  127. knopka.innerText = "click"
  128. document.body.append(knopka)
  129. function domEventPromise(element, eventName){
  130. function executor(resolve){
  131. function clickEvent(event){
  132. resolve(event)
  133. element.removeEventListener(eventName,clickEvent)
  134. }
  135. element.addEventListener(eventName,clickEvent)
  136. }
  137. return new Promise(executor)
  138. }
  139. domEventPromise(knopka, 'click').then( e => console.log('event click happens', e))
  140. }