import '../styles/index.less'; import {BASE_URL} from './config'; const URL_LIST = '/api/news/'; const URL_DETAIL = '/api/news/1/'; /** * Запрос данных */ function getData(url, detail = false) { fetch(url) .then((response) => response.json()) .then((data) => { if (detail) { renderItem(data); } else { renderList(data); } }) .catch(err => { console.log(err); }); } /** * Рендер списка */ function renderList(data = []) { if (!data.length) { return; } let content = document.querySelector('.js-list'); for (let item of data) { let block = document.createElement('article'); block.className = 'b-items__item b-preview'; block.id = item.id; block.innerHTML = `

${item.title}

${item.title}
${item.short_description}
`; content.append(block); } } /** * Рендер деталки */ function renderItem (data) { if (!data) { return; } let block = document.querySelector('.js-item'); block.innerHTML = `

${data.title}

${data.title}
${data.description}
`; } let url = window.location.pathname; let regexp = /^\/$/; let regexpDetail = /\/detail\/\d*\/$/; console.log(BASE_URL); if (regexpDetail.test(url)) { getData(BASE_URL + URL_DETAIL, true); } else if (regexp.test(url)) { getData(BASE_URL + URL_LIST); }