unknown преди 2 години
родител
ревизия
6c3585407a
променени са 5 файла, в които са добавени 15357 реда и са изтрити 205 реда
  1. 15173 167
      package-lock.json
  2. 1 0
      package.json
  3. 13 6
      src/index.html
  4. 114 0
      src/java-script/hw.js
  5. 56 32
      src/styles.css

Файловите разлики са ограничени, защото са твърде много
+ 15173 - 167
package-lock.json


+ 1 - 0
package.json

@@ -50,6 +50,7 @@
     "webpackbar": "^4.0.0"
   },
   "dependencies": {
+    "axios": "^0.24.0",
     "basiclightbox": "^5.0.3",
     "core-js": "^3.6.4",
     "handlebars": "^4.7.6",

+ 13 - 6
src/index.html

@@ -8,11 +8,18 @@
     <link rel="stylesheet" href="styles.css" />
   </head>
   <body>
-    <h2>FetchBasic</h2>
-    <table class="semen" id="semen"></table>
-    <h2>FetchImproved</h2>
-    <table class="coco" id="coco"></table>
-    <h2>FetchImprovedRecursion</h2>
-    <table class="oil" id="oil"></table>
+    <form class="chatForm" id="chatForm">
+      <label>Write down your nick</label>
+      <input class="chatForm__input" id="nick" name="nick" />
+      <label>Write down your message</label>
+      <input
+        class="chatForm__input"
+        class="chatForm__input"
+        id="message"
+        name="message"
+      />
+      <button type="submit">Submit</button>
+    </form>
+    <div id="chat" class="chat"></div>
   </body>
 </html>

+ 114 - 0
src/java-script/hw.js

@@ -0,0 +1,114 @@
+// Chat Homework
+
+const nick = document.getElementById('nick');
+const message = document.getElementById('message');
+const form = document.getElementById('chatForm');
+const chat = document.getElementById('chat');
+let id = 0;
+
+async function jsonPost(body) {
+  try {
+    const settings = {
+      method: 'POST',
+      body: JSON.stringify(body),
+    };
+    const data = await fetch('http://students.a-level.com.ua:10012', settings);
+    return data.json();
+  } catch (e) {
+    console.error(e);
+  }
+}
+
+async function sendMessage(nick, message) {
+  try {
+    const body = {
+      func: 'addMessage',
+      nick,
+      message,
+    };
+    return await jsonPost(body);
+  } catch (e) {
+    console.log(e);
+  }
+}
+
+async function getMessage() {
+  try {
+    const body = {
+      func: 'getMessages',
+      nextMessageId: id,
+    };
+    const { data, nextMessageId } = await jsonPost(body);
+
+    data.slice(id).forEach(obj => {
+      const containerMsg = document.createElement('div');
+      containerMsg.classList.add('containerMsg');
+
+      const containerNick = document.createElement('div');
+      containerNick.textContent = obj.nick;
+      containerNick.classList.add('containerNick');
+
+      const containerMessage = document.createElement('div');
+      containerMessage.textContent = obj.message;
+      containerMessage.classList.add('containerMessage');
+
+      const containerTimestamp = document.createElement('div');
+      const date = new Date(obj.timestamp);
+      const years = date.getFullYear();
+      const mouths = date.getMonth();
+      const hours = date.getHours();
+      const minutes = date.getMinutes();
+      const seconds = date.getSeconds();
+      containerTimestamp.textContent = `${years}:${mouths}:${hours}:${minutes}:${seconds}`;
+      containerTimestamp.classList.add('containerTimestamp');
+
+      containerMsg.append(containerNick, containerMessage, containerTimestamp);
+      chat.append(containerMsg);
+      id = nextMessageId;
+    });
+  } catch (e) {
+    console.error(e);
+  }
+}
+
+async function sendAndCheck(e) {
+  try {
+    e.preventDefault();
+    if (!nick.value || !message.value) return;
+    await sendMessage(nick.value, message.value);
+    await getMessage();
+    scrollTo(0, document.body.scrollHeight);
+  } catch (e) {
+    console.error(e);
+  }
+}
+
+form.addEventListener('submit', sendAndCheck);
+
+(async function checkLoop(delay) {
+  setInterval(getMessage, delay);
+})(3000);
+
+function jsonMessageXMLHttpRequest(url, data) {
+  return new Promise((resolve, reject) => {
+    var x = new XMLHttpRequest();
+    x.onerror = () => reject(new Error('jsonPost failed'));
+    //x.setRequestHeader('Content-Type', 'application/json');
+    x.open('POST', url, true);
+    x.send(JSON.stringify(data));
+
+    x.onreadystatechange = () => {
+      if (x.readyState == XMLHttpRequest.DONE && x.status == 200) {
+        resolve(JSON.parse(x.responseText));
+      } else if (x.status != 200) {
+        reject(new Error('status is not 200'));
+      }
+    };
+  });
+}
+
+// jsonMessageXMLHttpRequest('http://students.a-level.com.ua:10012', {
+//   func: 'addMessage',
+//   nick: 'Ado',
+//   message: 'XMLHttpRequest',
+// }).then(data => console.log(data)).catch(e => console.log(e));

+ 56 - 32
src/styles.css

@@ -1,52 +1,76 @@
 body{
   position: relative;
-  padding-bottom: 300px;
+  margin: 0;
   background-color: rgb(226, 221, 221);
   min-height: 100vh;
 }
-button{
- min-width: 40px;
- min-height: 20px;
- background-color: rgb(149, 240, 4);
- font-size: 14px;
+
+
+
+.chat{
+  background-color: gray;
+  min-height: 100vh;
+  padding: 30px;
+}
+.containerMsg{
+  margin-bottom: 5px;
+  border-radius: 10px;
+  border: solid 3px rgb(3, 3, 3);
+  padding: 10px;
+  background-color: rgb(63, 61, 61);
 }
 
-table{
-  max-width: 100%;
-  margin: 0 auto;
-  margin-bottom: 30px;
+.containerNick{
+  color: white;
+  font-size: 20px;
+  margin-bottom: 5px;
 }
 
-.semen{
-  background-color: rgb(233, 213, 101);
-  padding: 10px;
-  border-radius: 10px;
-  border: solid 3px orange;
-  color: rgb(247, 45, 9);
+.containerMessage{
+  color: rgb(248, 175, 18);
+  font-size: 16px;
+  margin-bottom: 5px;
 }
 
-.coco{
-  background-color: rgb(50, 177, 236);
-  padding: 10px;
-  border-radius: 10px;
-  border: solid 3px rgb(11, 7, 238);
-  color: rgb(25, 9, 247);
+.containerTimestamp{
+  color: rgb(87, 241, 26);
+  font-size: 16px;
 }
 
-.oil{
-  background-color: rgb(228, 109, 168);
-  padding: 10px;
+.chatForm{
+  position: fixed;
+  bottom: 20px;
+  width: 20%;
+  left: 40%;
+  margin: 0;
+  display: flex;
+  flex-direction: column;
+  align-items: center;
   border-radius: 10px;
-  border: solid 3px rgb(238, 7, 7);
-  color: rgb(247, 247, 247);
+  padding: 10px;
+  padding-bottom: 20px;
+  background-color: rgb(80, 78, 78);
+  z-index: 1;
+}
+
+.chatForm__input:first-child{
+  margin-right: 20px;
+}
+.chatForm__input{
+  margin-bottom: 5px;
+  width: 100%;
 }
 
-h2{
-  text-align: center;
-  color: brown;
+label{
+  margin-bottom: 5px;
+  color: rgb(249, 250, 252);
+  font-size: 18px;
 }
 
-a{
-  height: auto;
+button {
+  background-color: green;
+  width: 100px;
+  font-size: 24px;
+  color: aliceblue;
 }