Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. /* eslint-disable indent */
  2. /* eslint-disable max-len */
  3. /* eslint-disable no-undef */
  4. import { config } from './src/helpers/config.js';
  5. $(document).ready(() => {
  6. // get the location
  7. const ipLookUp = () => {
  8. $.get('http://ip-api.com/json')
  9. .then((data) => {
  10. appiCall(data.city);
  11. })
  12. .fail(() => {
  13. console.log('Failed to detect user location');
  14. });
  15. };
  16. ipLookUp();
  17.  
  18. // getting the weather from appi on fav click or search lick
  19. const appiCall = (city) => {
  20. $.get(`${config.basicUrl}${'forecast'}?q=${city}${config.key}${config.toCelsius}`)
  21. .then((data) => {
  22. let j = 0;
  23. const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  24. for (let i = 8; i < data.list.length; i += 8) {
  25. j++;
  26. const temp = data.list[i].main.temp.toFixed(0);
  27. const description = data.list[i].weather[0].description;
  28. const icon = 'https://openweathermap.org/img/w/' + data.list[i].weather[0].icon + '.png';
  29. const strDate = new Date(data.list[i].dt_txt);
  30. const day = weekDays[strDate.getDay()];
  31. $('#title' + j).html(day).css('font-weight', 'lighter');
  32. $('#icon' + j).attr('src', icon);
  33. $('#temp' + j).html(temp + '&deg').css('font-weight', 'lighter');
  34. $('#desc' + j).html(description)
  35. .css('text-transform', 'capitalize')
  36. .css('font-weight', 'normal');
  37. }
  38.  
  39. const location = data.city.name + ', ' + data.city.country;
  40. const icon = 'https://openweathermap.org/img/w/' + data.list[0].weather[0].icon + '.png';
  41. const temp = data.list[0].main.temp.toFixed(0);
  42. const humidity = data.list[0].main.humidity.toFixed(0) + '%';
  43. const wind = data.list[0].wind.speed.toFixed(0);
  44. const description = data.list[0].weather[0].description;
  45.  
  46. $('#curr-city').html(location);
  47. $('#today').html('Today');
  48. $('#icon').attr('src', icon).css('width', '40%');
  49. $('#temp-holder').html(temp + '&deg');
  50. $('#wind-holder').html(`Wind &lt; ${wind}mph`);
  51. $('#humidity-holder').html(`Humidity ${humidity}`);
  52. $('#desc-holder').html(description).css('text-transform', 'capitalize');
  53. })
  54.  
  55. .fail(() => console.log('Unexpected error, try again!'));
  56. };
  57.  
  58. // hiding/showing fav icon on keyup func
  59. $('#city').keyup(() => {
  60. if ($('#city').val()) {
  61. $('#favourite').show();
  62. } else {
  63. $('#favourite').hide();
  64. }
  65. });
  66.  
  67. // updating the fav list and refreshing the page
  68. const updateFavList = () => {
  69. Object.keys(localStorage)
  70. .reduce((obj, key) => {
  71. obj[key] = localStorage.getItem(key);
  72. $('#favourites')
  73. .append(`
  74. <li class='d-flex justify-content-between align-items-center fav-click'>
  75. <div
  76. class='h3 font-weight-light'
  77. title="Check the weather for ${key}">${key}
  78. </div>
  79. <i class='fas fa-trash-alt mx-1'>
  80. </li>`)
  81. .css('text-transform', 'capitalize');
  82. return obj;
  83. }, []);
  84. };
  85. updateFavList();
  86.  
  87. // saving favourite;
  88. $('#favourite').on('click', () => {
  89. const fav = $('#city').val();
  90. if (localStorage.getItem(fav) !== fav) {
  91. localStorage.setItem(fav, fav);
  92. }
  93. location.reload();
  94. });
  95.  
  96. // remove favourite click
  97. $('.fa-trash-alt').on('click', (event) => {
  98. const remove = $(event.target).parent().text().trim();
  99. localStorage.removeItem(remove);
  100. $(event.target)
  101. .parent()
  102. .animate({
  103. opacity: 0,
  104. }, 300, () => {
  105. $(event.target).parent().remove();
  106. });
  107. });
  108.  
  109. // fav-click showing weather
  110. $('.fav-click').on('click', (event) => {
  111. const favCity = $(event.target).text().trim();
  112. appiCall(favCity);
  113. });
  114.  
  115. // find city in the search
  116. $('#search').on('click', () => {
  117. const city = $('#city').val();
  118. appiCall(city);
  119. });
  120. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement