Skip to content

Commit 3d86bb1

Browse files
committed
добавления активного поста в state, обработка клика по кнопке открытия модалки
1 parent 12d5dd6 commit 3d86bb1

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

src/js/initApp.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { renderUIText } from "./view.js";
2-
import { updateInputValue, addRssFeed, validateInput } from "./model.js";
2+
import {
3+
updateInputValue,
4+
addRssFeed,
5+
validateInput,
6+
setActivePost,
7+
} from "./model.js";
38

49
export const initApp = () => {
510
renderUIText();
611

712
const input = document.querySelector("#url-input");
813
const form = document.querySelector("#rss-form");
14+
const postsContainer = document.querySelector(".posts");
915

1016
input.addEventListener("input", (e) => {
1117
updateInputValue(e.target.value);
@@ -17,4 +23,12 @@ export const initApp = () => {
1723
.then(() => addRssFeed())
1824
.catch(() => console.log("валидация не пройдена"));
1925
});
26+
27+
postsContainer.addEventListener("click", (e) => {
28+
const button = e.target.closest(".modal-btn");
29+
console.log("id:", button.dataset.id);
30+
if (button && button.dataset.id) {
31+
setActivePost(button.dataset.id);
32+
}
33+
});
2034
};

src/js/model.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ export const addRssFeed = () => {
5151
throw error;
5252
});
5353
};
54+
55+
export const setActivePost = (id) => {
56+
console.log(state.posts);
57+
state.activeItem = state.posts.find((post) => post.id === id);
58+
};

src/js/parseRss.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const parseRss = (xmlString) => {
88
}
99

1010
const channel = {
11-
id: 1, // добавить
11+
id: doc.querySelector("channel > link")?.textContent || "",
1212
title: doc.querySelector("channel > title")?.textContent || "",
1313
description: doc.querySelector("channel > description")?.textContent || "",
1414
link: doc.querySelector("channel > link")?.textContent || "",

src/js/renderRssFeed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export const renderRssFeed = (state) => {
3333
(post) => `
3434
<li class="list-group-item d-flex justify-content-between align-items-start border-0 border-end-0">
3535
<a href="${post.link}" class="fw-bold" data-id="${post.id}" target="_blank" rel="noopener noreferrer">${post.title}</a>
36-
<button type="button" class="btn btn-outline-primary btn-sm" data-id="${post.id}" data-bs-toggle="modal" data-bs-target="#modal">Просмотр</button>
36+
<button type="button" class="btn btn-outline-primary btn-sm modal-btn" data-id="${post.id}" data-bs-toggle="modal" data-bs-target="#modal">Просмотр</button>
3737
</li>
38-
`
38+
`,
3939
)
4040
.join("")}
4141
</ul>

src/js/state.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import onChange from "on-change";
22
import { renderRssFeed } from "./renderRssFeed.js";
3-
import { renderErrors, renderInputValue } from "./view.js";
3+
import { renderErrors, renderInputValue, showModal } from "./view.js";
44

55
const createState = () => {
66
const object = {
@@ -19,8 +19,7 @@ const createState = () => {
1919
renderRssFeed(state);
2020
}
2121
if (path === "activeItem") {
22-
// поиск по массиву постов
23-
// рендер модалки
22+
showModal(state.activeItem);
2423
}
2524
if (path === "form.inputValue") {
2625
renderInputValue(value);

src/js/view.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ export const renderErrors = (errors) => {
3333
export const renderInputValue = (value) => {
3434
input.value = value;
3535
};
36+
37+
export const showModal = (activeItem) => {
38+
console.log("Активный пост:", activeItem);
39+
};

0 commit comments

Comments
 (0)