From 2ade101b51633565e455a9b0fc66a00a60802e47 Mon Sep 17 00:00:00 2001 From: Amimul Ihsan Zubair <86657812+aizubair21@users.noreply.github.com> Date: Sat, 16 Dec 2023 08:30:59 +0600 Subject: [PATCH] local storage function update -> added function to save the search data array of weather. --- localstorageFunction.js | 46 ++++++++++++++++++++++++++++++++++------- weather.js | 10 +++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/localstorageFunction.js b/localstorageFunction.js index fe31472..163d570 100644 --- a/localstorageFunction.js +++ b/localstorageFunction.js @@ -43,28 +43,60 @@ function showLocalStorageSearchData() { } } +/** + * localstorage function to keep search data to localstorage + */ +async function localStorageFunction(searchData = null, swData = null) { + // console.log(searchWeather); -//local storege function -async function localStorageFunction(searchData = null) { - + /** + * search data + */ if (!localStorage.getItem('search')) { + + // search data searchArray.push(searchData); searchData.active = true; localStorage.setItem('search', JSON.stringify(searchArray)); + } else { - let haveLareadyInseted = false; + + let haveAlreadyInseted = false; searchArray = await JSON.parse(localStorage.getItem('search')); searchArray.forEach(rd => { if (rd.keyword == searchData.keyword) { - haveLareadyInseted = true; + haveAlreadyInseted = true; } }); - if (searchData != null && !haveLareadyInseted) { + if (searchData != null && !haveAlreadyInseted) { searchArray.push(searchData); localStorage.setItem('search', JSON.stringify(searchArray)); triggerActiveSearchData(searchArray.length - 1, true); } + } + + /** + * search weather info save to local storage + */ + if (!localStorage.getItem('searchWeather')) { + getSearchWeatherArray.push(swData); + localStorage.setItem('searchWeather', JSON.stringify(getSearchWeatherArray)); + } else { + let haveAlreadyInserted = false; + getSearchWeatherArray = await JSON.parse(localStorage.getItem(getSearchWeatherArray)); + getSearchWeatherArray.forEach((wea) => { + if (wea.cityName == swData.cityName) { + haveAlreadyInserted = true; + } + }) + if (swData !== undefined && !haveAlreadyInserted) { + getSearchWeatherArray.push(swData); + localStorage.setItem('searchWeather', JSON.stringify(getSearchWeatherArray)) + } + + } + showLocalStorageSearchData(); // console.log(searchData); // console.log(JSON.parse(localStorage.getItem('search'))); @@ -121,6 +153,6 @@ function deleteLocalStorageData(targetI) { localStorage.setItem("search", JSON.stringify(ld)); showLocalStorageSearchData(); } else { - alert("no data to remove") + alert("No data to remove") } } \ No newline at end of file diff --git a/weather.js b/weather.js index b1652ad..236aa34 100644 --- a/weather.js +++ b/weather.js @@ -37,6 +37,7 @@ let otherDayWeatherList = ''; let indexCount = []; let tempArray = []; let searchArray = []; +let getSearchWeatherArray = []; // const descriptionElement = document.getElementById('description'); const todayElement = document.getElementById("toDay"); @@ -1626,7 +1627,7 @@ async function getWeather(city) { await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${city??"Dhaka"}&APPID=2acfc6c74c2396283f1bf3b656fc902f`) .then((response) => response.data) .then((weather) => { - console.log(weather.weather[0].main); + // console.log(weather.weather[0].main); // console.log(weather); setLocationBtn.innerHTML = weather.name; // searchInputElement.value = weather.name; @@ -1638,7 +1639,7 @@ async function getWeather(city) { weather: weather.weather, active: false, }; - localStorageFunction(localStorageObject); + localStorageFunction(localStorageObject, weather); // setTimeout(() => { // showLocalStorageSearchData(); // }, 1000); @@ -1694,7 +1695,8 @@ async function getWeather(city) { `; sunSetElement.innerHTML = sunSet; - document.getElementById('weatherTitle').innerHTML = weather.main; + // console.log(weather); + document.getElementById('weatherTitle').innerHTML = weather.weather[0].main; getWeatherNextFiveHours(city); rotateSunPostion(sunRiseLocalString, sunSetLocalString); @@ -1781,7 +1783,7 @@ async function getWeather(city) { /** * left side main image */ - switch (weather.main) { + switch (weather.weather[0].main) { case 'Clear': document.getElementById('mainImg').innerHTML = ``; // todayImg = img/clear_sky.png;