-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from NikitaKir98/module11-task1
- Loading branch information
Showing
11 changed files
with
195 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { isEscapeKey } from './utils.js'; | ||
|
||
const alertTemplateError = document.querySelector('#error').content; | ||
|
||
const removeAlert = () => { | ||
const currentAlert = document.querySelector('.error'); | ||
if (currentAlert) { | ||
document.body.removeChild(currentAlert); | ||
document.removeEventListener('keydown', onDocumentKeydown); | ||
} | ||
}; | ||
|
||
function onDocumentKeydown (evt) { | ||
if (isEscapeKey(evt)) { | ||
evt.preventDefault(); | ||
removeAlert(); | ||
} | ||
} | ||
|
||
const showAlertError = (message) => { | ||
const alert = alertTemplateError.cloneNode(true); | ||
|
||
const alertTitle = alert.querySelector('.error__title'); | ||
alertTitle.textContent = message; | ||
|
||
const alertButton = alert.querySelector('button'); | ||
|
||
alertButton.addEventListener('click', (evt) => { | ||
evt.preventDefault(); | ||
removeAlert(); | ||
}); | ||
|
||
const alertSection = alert.querySelector('section'); | ||
|
||
alertSection.addEventListener('click', () => { | ||
removeAlert(); | ||
}); | ||
|
||
document.addEventListener('keydown', onDocumentKeydown); | ||
document.body.append(alert); | ||
}; | ||
|
||
export {showAlertError}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { isEscapeKey } from './utils.js'; | ||
|
||
const alertTemplateSuccess = document.querySelector('#success').content; | ||
|
||
const removeAlert = () => { | ||
const currentAlert = document.querySelector('.success'); | ||
if (currentAlert) { | ||
document.body.removeChild(currentAlert); | ||
document.removeEventListener('keydown', onDocumentKeydown); | ||
} | ||
}; | ||
|
||
function onDocumentKeydown (evt) { | ||
if (isEscapeKey(evt)) { | ||
evt.preventDefault(); | ||
removeAlert(); | ||
} | ||
} | ||
|
||
const showAlertSucces = () => { | ||
const alert = alertTemplateSuccess.cloneNode(true); | ||
|
||
const alertButton = alert.querySelector('.success__button'); | ||
|
||
alertButton.addEventListener('click', (evt) => { | ||
evt.preventDefault(); | ||
removeAlert(); | ||
}); | ||
|
||
const alertSection = alert.querySelector('.success'); | ||
|
||
alertSection.addEventListener('click', () => { | ||
removeAlert(); | ||
}); | ||
|
||
document.addEventListener('keydown', onDocumentKeydown); | ||
document.body.append(alert); | ||
}; | ||
|
||
export { showAlertSucces }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const BASE_URL = 'https://31.Javascript.htmlacademy.pro/kekstagram'; | ||
const Route = { | ||
GET_DATA: '/data', | ||
SEND_DATA: '/', | ||
}; | ||
const Method = { | ||
GET: 'GET', | ||
POST: 'POST', | ||
}; | ||
const ErrorText = { | ||
GET_DATA: 'Не удалось загрузить данные. Попробуйте обновить страницу', | ||
SEND_DATA: 'Не удалось отправить форму', | ||
}; | ||
|
||
const load = (route, errorText, method = Method.GET, body = null) => | ||
fetch(`${BASE_URL}${route}`, {method, body}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error(); | ||
} | ||
return response.json(); | ||
}) | ||
.catch(() => { | ||
throw new Error(errorText); | ||
}); | ||
|
||
const getData = () => load(Route.GET_DATA, ErrorText.GET_DATA); | ||
|
||
const sendData = (body) => load(Route.SEND_DATA, ErrorText.SEND_DATA, Method.POST, body); | ||
|
||
export {getData, sendData}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import {similarDescriptionPhoto} from './create-pictures-description.js'; | ||
import {createThumbnail} from './rendering-pictures.js'; | ||
import {getData} from './api.js'; | ||
import {showPopup} from './popup-error.js'; | ||
|
||
const pictures = document.querySelector('.pictures'); | ||
const picturesContainer = document.querySelector('.pictures'); | ||
const fragment = document.createDocumentFragment(); | ||
|
||
similarDescriptionPhoto.forEach((photo) => { | ||
fragment.append(createThumbnail(photo)); | ||
pictures.append(fragment); | ||
}); | ||
const insertThumbnails = (pictures) => { | ||
pictures.forEach((photo) => { | ||
fragment.append(createThumbnail(photo)); | ||
picturesContainer.append(fragment); | ||
}); | ||
}; | ||
|
||
export {similarDescriptionPhoto}; | ||
getData() | ||
.then((pictures)=> insertThumbnails(pictures)) | ||
.catch((err) => showPopup(err.message)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import {similarDescriptionPhoto} from './insert-thumbnails.js'; | ||
import './insert-thumbnails.js'; | ||
import './form-upload.js'; | ||
import './filters.js'; | ||
|
||
similarDescriptionPhoto(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const popupErroreTemplate = document.querySelector('#data-error').content; | ||
|
||
const showPopup = (message) => { | ||
const popupError = popupErroreTemplate.cloneNode(true); | ||
|
||
const popupTitle = popupError.querySelector('.data-error__title'); | ||
popupTitle.textContent = message; | ||
|
||
document.body.append(popupError); | ||
|
||
setTimeout(removePopup, 5000); | ||
}; | ||
|
||
function removePopup(){ | ||
document.querySelector('.data-error').remove(); | ||
} | ||
|
||
export { showPopup }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,4 @@ | ||
// возвращает рандомное число в диапазоне от a до b | ||
const getRandomInteger = (a, b) => { | ||
const lower = Math.ceil(Math.min(a, b)); | ||
const upper = Math.floor(Math.max(a, b)); | ||
const result = Math.random() * (upper - lower + 1) + lower; | ||
return Math.floor(result); | ||
}; | ||
|
||
// возвращает рандомный элемент переданного массива | ||
const getRandomArrayElement = (elements) => elements[getRandomInteger(0, elements.length - 1)]; | ||
|
||
//возвращает id по порядку с 1 | ||
function createIdGenerator () { | ||
let lastGeneratedId = 0; | ||
return function () { | ||
lastGeneratedId += 1; | ||
return lastGeneratedId; | ||
}; | ||
} | ||
|
||
//Проверка на escape | ||
const isEscapeKey = (evt) => evt.key === 'Escape'; | ||
|
||
export { | ||
getRandomInteger, | ||
getRandomArrayElement, | ||
createIdGenerator, | ||
isEscapeKey | ||
}; | ||
export {isEscapeKey}; |