Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Надо подкачаться #12

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions js/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const getData = () =>
fetch('https://31.javascript.htmlacademy.pro/kekstagram/data')
.then((response) => {

Check failure on line 3 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 4 spaces but found 2
if(!response.ok){

Check failure on line 4 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 6 spaces but found 4
throw new Error()

Check failure on line 5 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 8 spaces but found 4

Check failure on line 5 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon
}

Check failure on line 6 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 6 spaces but found 2
return response.json()

Check failure on line 7 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 6 spaces but found 0

Check failure on line 7 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon
});

Check failure on line 8 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 4 spaces but found 0

export const sendData = (body) =>
fetch('https://31.javascript.htmlacademy.pro/kekstagram/', {

Check failure on line 11 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 2 spaces but found 0
body,

Check failure on line 12 in js/api.js

View workflow job for this annotation

GitHub Actions / Check

Expected indentation of 4 spaces but found 2
method: 'POST'
})
10 changes: 10 additions & 0 deletions js/constans.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ export const EffectsSetting = {
};

export const DEFAULT_EFFECT = EFFECTS.NONE;

export const POPUPS = {
SUCCESS: 'success',
ERROR: 'error'
}

export const SUBMIT_TEXT = {
IDLE: 'Опубликовать',
SENDING: 'Публикую...'
}
75 changes: 0 additions & 75 deletions js/data.js

This file was deleted.

33 changes: 29 additions & 4 deletions js/form.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { isValid, reset as resetValidation } from './validation.js';
import {reset as resetScale} from './scale.js';
import {reset as resetEffects} from './effect.js';
import { reset as resetScale } from './scale.js';
import { reset as resetEffects } from './effect.js';
import { showPopup } from './popup.js';
import { POPUPS, SUBMIT_TEXT } from './constans.js';
import { sendData } from './api.js';

const uploadForm = document.querySelector('.img-upload__form');
const uploadOverlay = uploadForm.querySelector('.img-upload__overlay');
const body = document.body;
const uploadCancel = uploadForm.querySelector('.img-upload__cancel');
const uploadFile = uploadForm.querySelector('#upload-file');
const submitButton = uploadForm.querySelector('.img-upload__submit')

const openForm = () => {
uploadOverlay.classList.remove('hidden');
Expand Down Expand Up @@ -39,9 +43,30 @@ document.addEventListener('keydown', (evt) => {
}
});

const blockSubmitButton = (isBlocked = true) => {
submitButton.disabled = isBlocked;
submitButton.textContent = isBlocked ? SUBMIT_TEXT.SENDING : SUBMIT_TEXT.IDLE;
}

const onFormSubmit = (evt) => {
if (!isValid()) {
evt.preventDefault();
evt.preventDefault();
if (isValid()) {

blockSubmitButton();
sendData(new FormData(uploadForm))
.then((response) => {
if (!response.ok) {
throw new Error()
}
closeForm();
showPopup(POPUPS.SUCCESS);
})
.catch(() => {
showPopup(POPUPS.ERROR);
})
.finally(() => {
blockSubmitButton(false);
})
}
};

Expand Down
68 changes: 0 additions & 68 deletions js/function.js

This file was deleted.

16 changes: 13 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import {getDescriptionPhoto} from './data.js';
// import {getDescriptionPhoto} from './data.js';
import {render} from './render-thumbnail.js';
import './form.js';
import { showErrorMessage } from './util.js';
import { getData } from './api.js';

const data = getDescriptionPhoto();
// const data = getDescriptionPhoto();

// render(data);

render(data);

getData()
.then((data)=>{
render(data)
})
.catch(()=>{
showErrorMessage()
})


20 changes: 20 additions & 0 deletions js/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { POPUPS } from "./constans.js";

const successTemplate = document.querySelector('#success').content.querySelector('.success');
const errorTemplate = document.querySelector('#error').content.querySelector('.error');
const body = document.body;

const templates = {
[POPUPS.SUCCESS]: successTemplate,
[POPUPS.ERROR]: errorTemplate
}

export const showPopup = (type) => {
const newPopup = templates[type].cloneNode(true);
body.append(newPopup);
newPopup.addEventListener('click', ({target}) => {
if(target.classList.contains(type) || target.classList.contains(`${type}__button`)){
newPopup.remove();
};
});
}
24 changes: 17 additions & 7 deletions js/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
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 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);
// };

export {getRandomInteger};
// export {getRandomInteger};
const dataErrorTemplate = document.querySelector('#data-error').content.querySelector('.data-error');
const body = document.body;
const SHOW_TIME = 5000;

export const showErrorMessage = () => {
const newDataError = dataErrorTemplate.cloneNode(true);
body.append(newDataError);
setTimeout(() => {
newDataError.remove();
}, SHOW_TIME)
}
Loading