forked from htmlacademy-javascript/144485-kekstagram-31
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e58131
commit 17a871f
Showing
7 changed files
with
168 additions
and
148 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,19 @@ | ||
export const dataErrorTemplate = document.querySelector('#data-error').content.querySelector('.data-error'); | ||
export const sendErrorTemplate = document.querySelector('#error').content.querySelector('.error'); | ||
export const sendFormErrorTemplate = document.querySelector('#success').content.querySelector('.success'); | ||
export const SHOW_ERROR_TIME = 5000; | ||
export const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagra'; | ||
export const Route = { | ||
GET_DATA: '/data', | ||
SEND_DATA: '/', | ||
}; | ||
export const submitButton = document.querySelector('.img-upload__submit'); | ||
export const submitButtonText = { | ||
IDLE: 'Опубликовать', | ||
SENDING: 'Загрузка...' | ||
}; | ||
|
||
export const Method = { | ||
GET: 'GET', | ||
POST: 'POST', | ||
}; |
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 @@ | ||
import {BASE_URL, Route, Method} from './get-data-variables'; | ||
|
||
const load = (route, 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(); | ||
}); | ||
|
||
const getData = () => load(Route.GET_DATA); | ||
const sendData = (body) => load(Route.SEND_DATA, Method.POST, body); | ||
|
||
export {getData, sendData}; |
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,92 @@ | ||
import {onCloseChangePhotoEsc} from '../upload-photo'; | ||
import {dataErrorTemplate, sendErrorTemplate, sendFormErrorTemplate, SHOW_ERROR_TIME, submitButtonText} from './get-data-variables'; | ||
|
||
const submitButton = document.querySelector('.img-upload__submit'); | ||
|
||
|
||
const onSendSuccessClose = () => { | ||
const successMessage = document.querySelector('.success'); | ||
successMessage.remove(); | ||
document.removeEventListener('click', onClickOutModalSuccess); | ||
}; | ||
|
||
const onSendErrorMessageClose = () => { | ||
const errorMessage = document.querySelector('.error'); | ||
errorMessage.remove(); | ||
document.removeEventListener('click', onClickOutModalError); | ||
document.addEventListener('keydown', onCloseChangePhotoEsc); | ||
document.removeEventListener('keydown', onSendErrorMessageCloseEsc); | ||
}; | ||
|
||
function onClickOutModalSuccess (evt) { | ||
if(evt.target.closest('.success__inner')){ | ||
return; | ||
} | ||
onSendSuccessClose(); | ||
} | ||
|
||
function onClickOutModalError (evt) { | ||
if(evt.target.closest('.error__inner')){ | ||
return; | ||
} | ||
onSendErrorMessageClose(); | ||
} | ||
|
||
function onSendErrorMessageCloseEsc (evt) { | ||
if (evt.key === 'Escape') { | ||
onSendErrorMessageClose(); | ||
} | ||
} | ||
|
||
function onSendSuccessMessageCloseEsc (evt) { | ||
if (evt.key === 'Escape') { | ||
onSendSuccessClose(); | ||
} | ||
} | ||
|
||
const onSendErrorMessage = () => { | ||
const errorFragmentSend = document.createDocumentFragment(); | ||
const errorItemSend = sendErrorTemplate.cloneNode(true); | ||
const sendButttonClose = errorItemSend.querySelector('.error__button'); | ||
sendButttonClose.addEventListener('click', onSendErrorMessageClose); | ||
errorFragmentSend.append(errorItemSend); | ||
document.body.append(errorFragmentSend); | ||
document.addEventListener('keydown', onSendErrorMessageCloseEsc); | ||
document.addEventListener('click', onClickOutModalError); | ||
document.removeEventListener('keydown', onCloseChangePhotoEsc); | ||
}; | ||
|
||
const onShowErrorGetData = () => { | ||
const errorFragment = document.createDocumentFragment(); | ||
const errorItem = dataErrorTemplate.cloneNode(true); | ||
errorFragment.append(errorItem); | ||
document.body.append(errorFragment); | ||
const dataError = document.querySelector('.data-error'); | ||
|
||
setTimeout(() => { | ||
dataError.remove(); | ||
}, SHOW_ERROR_TIME); | ||
}; | ||
|
||
const onSendSuccessMessage = () => { | ||
const errorFragmentSend = document.createDocumentFragment(); | ||
const errorItemSend = sendFormErrorTemplate.cloneNode(true); | ||
const closeButton = errorItemSend.querySelector('.success__button'); | ||
errorFragmentSend.append(errorItemSend); | ||
document.body.append(errorFragmentSend); | ||
closeButton.addEventListener('click', onSendSuccessClose); | ||
document.addEventListener('click', onClickOutModalSuccess); | ||
document.addEventListener('keydown', onSendSuccessMessageCloseEsc); | ||
}; | ||
|
||
const blockSubmitButton = () => { | ||
submitButton.disabled = true; | ||
submitButton.textContent = submitButtonText.SENDING; | ||
}; | ||
|
||
const unblockSubmitButton = () => { | ||
submitButton.disabled = false; | ||
submitButton.textContent = submitButtonText.IDLE; | ||
}; | ||
|
||
export {onSendErrorMessage, onShowErrorGetData, onSendSuccessMessage, onSendSuccessClose, blockSubmitButton, unblockSubmitButton}; |
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 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