app.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. document.querySelector('select[name="month"]').addEventListener('change', () => {
  2. const select_value = event.target.value,
  3. options = event.target.options;
  4. for (let key in options) {
  5. if (options[key].value === select_value) {
  6. const days_count = options[key].getAttribute('days-count');
  7. days_list = document.querySelector('select[name="day"]');
  8. days_list.innerHTML = ``;
  9. for (let i = 0; i < days_count; i++) {
  10. days_list.innerHTML += `<option value="${i + 1}">${i + 1}</option>`
  11. }
  12. }
  13. }
  14. });
  15. document.querySelector('#country').addEventListener('change', () => {
  16. const select_value = event.target.value,
  17. options = event.target.options;
  18. for (let key in options) {
  19. if (options[key].value === select_value) {
  20. let city = document.querySelector('#city');
  21. fetch(`/api/cities?country_id=${select_value}`)
  22. .then(res => {
  23. return res.json();
  24. })
  25. .then(data => {
  26. city.innerHTML = ``;
  27. for (let key in data) {
  28. city.innerHTML += `<option value="${data[key].id}">${data[key].name}</option>`;
  29. }
  30. });
  31. }
  32. }
  33. });
  34. function selectAvatar(event) {
  35. const tmp_avatar = URL.createObjectURL(event.target.files[0]);
  36. document.querySelector('.avatar-edit').setAttribute('src', tmp_avatar);
  37. }
  38. function publicArticle(event) {
  39. event.preventDefault();
  40. event.target.classList.add('disable');
  41. event.target.innerHTML = `<i class="fad fa-spinner-third"></i>`;
  42. const body = document.querySelector('textarea[name="body"]').value,
  43. formData = new FormData();
  44. formData.append('body', body);
  45. fetch('/article/public', {
  46. method: "POST",
  47. headers: {
  48. "X-CSRF-TOKEN": document.querySelector('meta[name="csrf"]').getAttribute('content')
  49. },
  50. body: formData
  51. }).then(res => {
  52. return res.json();
  53. }).then(data => {
  54. if (data.body) {
  55. const error_el = document.querySelector('.body-error-field');
  56. error_el.classList.remove('d-none')
  57. error_el.innerHTML = `<strong>Проверьте правильность введенных данных</strong>`
  58. event.target.innerHTML = `<i class="fad fa-paper-plane"></i>`;
  59. event.target.classList.remove('disable');
  60. return;
  61. }
  62. const posts = document.querySelector('.posts');
  63. posts.innerHTML = `
  64. <li>
  65. <div class="author-controllers">
  66. <div style="display:flex; align-items:center;">
  67. <img src="${data.user.avatar_url}" alt="Это вы">
  68. <div style="margin-left: 10px;">
  69. <p style="color: #ffffff;font-weight: 700;">${data.user.name} ${data.user.surname}</p>
  70. </div>
  71. </div>
  72. <div class="controllers">
  73. <i class="far fa-ellipsis-h"></i>
  74. <ul>
  75. <li><a href="#">Удалить</a></li>
  76. </ul>
  77. </div>
  78. </div>
  79. <div class="body">
  80. <p>${body}</p>
  81. </div>
  82. <div style="display:flex;justify-content: space-between; align-items: center;">
  83. <ul class="actions">
  84. <li class="like">
  85. <i class="far fa-heart"></i>
  86. <span>9</span>
  87. </li>
  88. </ul>
  89. <span style="font-style: italic; color: #e3e3e3; font-size: 12px;">${data.created_at}</span>
  90. </div>
  91. </li>
  92. ` + posts.innerHTML;
  93. event.target.innerHTML = `<i class="fad fa-paper-plane"></i>`;
  94. event.target.classList.remove('disable');
  95. document.querySelector('textarea[name="body"]').value = '';
  96. }).catch(err => {
  97. })
  98. }
  99. function likeArticle(article_id) {
  100. const counter = document.querySelector('span.count_likes[article-id="' + article_id + '"]');
  101. if (event.target.classList.contains('fas')) {
  102. event.target.classList.remove('fas');
  103. event.target.classList.add('far');
  104. counter.innerText = parseInt(counter.innerText) - 1;
  105. } else {
  106. event.target.classList.remove('far');
  107. event.target.classList.add('fas');
  108. counter.innerText = parseInt(counter.innerText) + 1;
  109. }
  110. const formData = new FormData();
  111. formData.append('id', article_id);
  112. fetch(`/article/like`, {
  113. method: "POST",
  114. headers: {
  115. "X-CSRF-TOKEN": document.querySelector('meta[name="csrf"]').getAttribute('content')
  116. },
  117. body: formData
  118. }).then(res => {
  119. return res.json();
  120. }).then(data => {
  121. })
  122. }
  123. function searchFriends(event) {
  124. const body = event.target.value;
  125. if (body.length === 0) return;
  126. fetch(`/friends/search?q=${body}`)
  127. .then(res => {
  128. return res.json();
  129. })
  130. .then(data => {
  131. document.querySelector('.search-friends').innerHTML = '';
  132. data.forEach(user => {
  133. document.querySelector('.search-friends').innerHTML += `
  134. <li>
  135. <div class="user-info">
  136. <img src="${user.avatar_url}" alt="">
  137. <a href="/u/${user.username}"><h3>${user.name} ${user.surname}</h3></a>
  138. </div>
  139. <ul class="actions">
  140. <li>
  141. <a href="#">Удалить из друзей</a>
  142. </li>
  143. <li>
  144. <form action="/friends/add" method="post" id="add-friend_${user.id}">
  145. <input type="hidden" name="_token" value="${document.querySelector('meta[name="csrf"]').getAttribute('content')}">
  146. <input type="hidden" name="id" value="${user.id}">
  147. </form>
  148. <a
  149. href="#"
  150. onclick="event.preventDefault(); document.getElementById('add-friend_${user.id}').submit()"
  151. >Добавить в друзья</a>
  152. </li>
  153. </ul>
  154. </li>
  155. `
  156. });
  157. console.log(data)
  158. });
  159. }