Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. {
  2. const $searchInput = document.querySelector(`[name="q"]`);
  3. const $activityContainer = document.querySelector(`.activity-container`);
  4.  
  5. const init = () => {
  6. $searchInput.addEventListener(`input`, handleInputSearch);
  7. };
  8.  
  9. const handleInputSearch = () => {
  10. const q = $searchInput.value.trim();
  11. if (q.length > 0) {
  12. fetch(`index.php?q=${q}`, {
  13. headers: new Headers({
  14. Accept: `application/json`,
  15. }),
  16. })
  17. .then(r => r.json())
  18. .then(data => parse(data));
  19. }
  20. };
  21.  
  22. const parse = results => {
  23. $activityContainer.innerHTML = results
  24. .map(event => createEventArticle(event))
  25. .join(``);
  26. };
  27.  
  28. const createEventArticle = event => {
  29. return `<li>
  30. <a href="index.php?page=events&amp;id=${event.id}">
  31. <h2>${event.title}</h2>
  32. </a>
  33. </li>`;
  34. };
  35.  
  36. init();
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement