-
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 lenapokrovskaya/module11-task1
- Loading branch information
Showing
8 changed files
with
180 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
import {createPosts} from './data.js'; | ||
import {openUploadForm, closeUploadForm} from './upload-photo-form.js'; | ||
import {setUserFormSubmit} from './form-validation.js'; | ||
import {getData} from './api.js'; | ||
import {createFragment} from './thumbnails.js'; | ||
import {renderModal} from './modal.js'; | ||
import {openUploadForm} from './upload-photo-form.js'; | ||
import {showDataError} from './notifications.js'; | ||
|
||
const ERROR_DISPLAY_TIME = 5000; | ||
const templateDataErrorElement = document.querySelector('#data-error').content.querySelector('.data-error'); | ||
const dataErrorElement = templateDataErrorElement.cloneNode(true); | ||
|
||
const posts = createPosts(); | ||
createFragment(posts); | ||
renderModal(posts); | ||
openUploadForm(); | ||
|
||
getData() | ||
.then((posts) => { | ||
createFragment(posts); | ||
renderModal(posts); | ||
}) | ||
.catch(() => { | ||
showDataError(dataErrorElement); | ||
setTimeout(() => { | ||
dataErrorElement.remove(); | ||
}, ERROR_DISPLAY_TIME); | ||
}); | ||
|
||
setUserFormSubmit(closeUploadForm); |
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,64 @@ | ||
import {isEscapeKey} from './util.js'; | ||
|
||
const bodyElement = document.body; | ||
const fragmentElement = document.createDocumentFragment(); | ||
const templateErrorElement = bodyElement.querySelector('#error').content.querySelector('.error'); | ||
const errorElement = templateErrorElement.cloneNode(true); | ||
const errorButtonElement = errorElement.querySelector('.error__button'); | ||
const errorBlockElement = errorElement.querySelector('.error__inner'); | ||
|
||
const templateSuccessElement = bodyElement.querySelector('#success').content.querySelector('.success'); | ||
const successElement = templateSuccessElement.cloneNode(true); | ||
const successButtonElement = successElement.querySelector('.success__button'); | ||
const successBlockElement = successElement.querySelector('.success__inner'); | ||
|
||
const showDataError = (error) => { | ||
fragmentElement.appendChild(error); | ||
bodyElement.append(fragmentElement); | ||
}; | ||
|
||
// Функция для удаления сообщения | ||
const closeMessageBox = (element) => { | ||
if (element) { | ||
element.remove(); // Удаляем элемент | ||
} | ||
}; | ||
|
||
// Обработчик закрытия окна сообщения по клику | ||
const onClickCloseMessage = (element) => { | ||
closeMessageBox(element); | ||
}; | ||
|
||
// Обработчик закрытия окна сообщения по Esc | ||
const onEscCloseMessage = (evt, element) => { | ||
if(isEscapeKey(evt)) { | ||
closeMessageBox(element); | ||
} | ||
}; | ||
|
||
// Обработчик закрытия окна сообщения по нажатию на оверлэй | ||
const onOverlayClick = (evt, messageBlock, element) => { | ||
if(!messageBlock.contains(evt.target)) { | ||
closeMessageBox(element); | ||
} | ||
}; | ||
|
||
//Показ сообщения об ошибке отправки данных | ||
const showError = () => { | ||
fragmentElement.appendChild(errorElement); | ||
bodyElement.append(fragmentElement); | ||
errorButtonElement.addEventListener('click', () => onClickCloseMessage(errorElement)); | ||
document.addEventListener('keydown', (evt) => onEscCloseMessage(evt, errorElement)); | ||
document.addEventListener('click', (evt) => onOverlayClick(evt, errorBlockElement, errorElement)); | ||
}; | ||
|
||
//Показ сообщения об успехе отправки данных | ||
const showSuccess = () => { | ||
fragmentElement.appendChild(successElement); | ||
bodyElement.append(fragmentElement); | ||
successButtonElement.addEventListener('click', () => onClickCloseMessage(successElement)); | ||
document.addEventListener('keydown', (evt) => onEscCloseMessage(evt, successElement)); | ||
document.addEventListener('click', (evt) => onOverlayClick(evt, successBlockElement, successElement)); | ||
}; | ||
|
||
export {showDataError, showError, showSuccess}; |
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
Oops, something went wrong.