Skip to content

Commit

Permalink
Поправил ошибку с выполнением запроса погоды после перехода с About н…
Browse files Browse the repository at this point in the history
…а main
  • Loading branch information
SergeyAkkuratov committed Jun 4, 2024
1 parent ab0e900 commit bbcf97d
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/weatherApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,30 @@ export default async function weatherApp(rootElement) {
}
}

const ipInfo = await getInfoByIP();

rootElement.innerHTML = mainTemplate;

rootElement
.querySelector("#weatherForm")
.addEventListener("submit", (event) => {
event.preventDefault();
function switchToMainPage() {
rootElement.innerHTML = mainTemplate;

rootElement
.querySelector("#weatherForm")
.addEventListener("submit", (event) => {
event.preventDefault();

// читаем значение из формы
const formElement = event.target;
const inputEl = formElement.querySelector("#userInput");
const cityName = inputEl.value;
inputEl.value = "";
router.navigate(`${PREFIX}/weather/${cityName}`);
});

loadHistory();
}

// читаем значение из формы
const formElement = event.target;
const inputEl = formElement.querySelector("#userInput");
const cityName = inputEl.value;
inputEl.value = "";
router.navigate(`${PREFIX}/weather/${cityName}`);
});
function switchToAboutPage() {
rootElement.innerHTML = aboutTemplate;
}

// Объявляем общие для всех старниц роуты и листенеры
rootElement.addEventListener("click", (event) => {
if (event.target instanceof HTMLAnchorElement) {
event.preventDefault();
Expand All @@ -121,19 +128,16 @@ export default async function weatherApp(rootElement) {

router.addRoute({
path: `${PREFIX}/about`,
onEnter: () => {
rootElement.innerHTML = aboutTemplate;
},
onEnter: switchToAboutPage,
});

router.addRoute({
path: `${PREFIX}/`,
onEnter: () => {
rootElement.innerHTML = mainTemplate;
loadHistory();
},
onEnter: switchToMainPage,
});

loadHistory();
// Стартовые действия при открытия приложения
switchToMainPage();
const ipInfo = await getInfoByIP();
if (ipInfo.city) router.navigate(`${PREFIX}/weather/${ipInfo.city}`);
}

0 comments on commit bbcf97d

Please sign in to comment.