Skip to content

Commit

Permalink
local storage function update
Browse files Browse the repository at this point in the history
-> added function to save the search data array of weather.
  • Loading branch information
aizubair21 committed Dec 16, 2023
1 parent 50d1aac commit 2ade101
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
46 changes: 39 additions & 7 deletions localstorageFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand Down Expand Up @@ -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")
}
}
10 changes: 6 additions & 4 deletions weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let otherDayWeatherList = '';
let indexCount = [];
let tempArray = [];
let searchArray = [];
let getSearchWeatherArray = [];
// const descriptionElement = document.getElementById('description');

const todayElement = document.getElementById("toDay");
Expand Down Expand Up @@ -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;
Expand All @@ -1638,7 +1639,7 @@ async function getWeather(city) {
weather: weather.weather,
active: false,
};
localStorageFunction(localStorageObject);
localStorageFunction(localStorageObject, weather);
// setTimeout(() => {
// showLocalStorageSearchData();
// }, 1000);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 = `<img src="img/clear_sky.png">`;
// todayImg = img/clear_sky.png;
Expand Down

0 comments on commit 2ade101

Please sign in to comment.