Skip to content

Commit

Permalink
Добавил сохранение истории городов между запусками
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyAkkuratov committed Mar 19, 2024
1 parent 04ce289 commit 51b73e8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function setToString(set) {
let str = "";
set.forEach((value) => {
str += `${value} `;
});
return str.trim();
}

function stringToSet(string) {
const set = new Set();
string.split(" ").forEach((value) => {
set.add(value);
});
return set;
}

export function getHistorySet() {
let historySet = new Set();
const historyString = localStorage.getItem("history");
if (historyString !== null && historyString.length > 0) {
historySet = stringToSet(historyString);
}
return historySet;
}

export function setHistorySet(historySet) {
localStorage.setItem("history", setToString(historySet));
}
17 changes: 11 additions & 6 deletions src/weatherApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// eslint-disable-next-line import/no-self-import
import oopsImg from "./assets/oops.png";
import { getInfoByIP, getMap, getWeather } from "./externalRequests";
import { getHistorySet, setHistorySet } from "./localStorage";

export default async function weatherApp(element) {
const maxHistorylines = 10;
const historyList = new Set();
const historySet = new Set();

function showWeather(data) {
const weatherInfo = element.querySelector("#info");
Expand Down Expand Up @@ -41,18 +42,20 @@ export default async function weatherApp(element) {
}

async function updateHistoryBlock(cityName) {
const historyElement = element.querySelector("#history");
if (!historyList.has(cityName)) {
historyList.add(cityName);
if (!historySet.has(cityName)) {
const historyElement = element.querySelector("#history");
historySet.add(cityName);
const p = document.createElement("p");
p.innerHTML = cityName;
p.addEventListener("click", onclickHistoryLine);
historyElement.querySelector("span").insertAdjacentElement("afterend", p);

if (historyElement.querySelectorAll("p").length > maxHistorylines) {
historyList.delete(historyElement.lastElementChild.innerHTML);
historySet.delete(historyElement.lastElementChild.innerHTML);
historyElement.removeChild(historyElement.lastElementChild);
}

setHistorySet(historySet);
}
}

Expand All @@ -75,7 +78,9 @@ export default async function weatherApp(element) {
<div id="history" class="history-block">
<span>History:</span>
</div>`;

if (getHistorySet().size > 0) {
getHistorySet().forEach((cityName) => updateHistoryBlock(cityName));
}
if (ipInfo.region) updateWeather(ipInfo.region);

element
Expand Down
15 changes: 14 additions & 1 deletion src/weatherApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe("Weather application tests", () => {
}

beforeEach(() => {
localStorage.clear();
// Моки
exteranlApi.getWeather.mockReturnValue(weather.Moscow);
exteranlApi.getInfoByIP.mockReturnValue(ipInfo);
Expand Down Expand Up @@ -153,7 +154,7 @@ describe("Weather application tests", () => {
);
});

it("History block have only one distinct city names", async () => {
it("History block have only distinct city names", async () => {
for (let i = 0; i < 5; i += 1) {
specifyCityName(`DefaultCity`);
submit();
Expand All @@ -164,4 +165,16 @@ describe("Weather application tests", () => {

expect(getAllHistoryParagraphs()).toStrictEqual(["DefaultCity"]);
});

it("History loads from localStorage", async () => {
specifyCityName(`Moscow`);
submit();

weatherApp(el);

// Надо подождать, когда все асинк функции закончатся, а потом продолжать выполнять синхронный код
await new Promise(process.nextTick);

expect(getAllHistoryParagraphs()).toStrictEqual(["Moscow"]);
});
});

0 comments on commit 51b73e8

Please sign in to comment.