1- import { renderGallery , setGalleryData } from './gallery.js' ;
2- import { showErrorMessage } from './utils.js' ;
1+ import { showError , showSuccess } from './dialogs.js' ;
32import { onCloseImageEditor } from './upload-picture.js' ;
3+ import { sendPhoto } from './api.js' ;
44
55const hashtagPattern = / ^ # [ a - z а - я ё 0 - 9 ] { 1 , 19 } $ / i;
66const MAX_HASHTAGS = 5 ;
77const MAX_DESCRIPTION_LEN = 140 ;
8- const imgUploadForm = document . querySelector ( '.img-upload__form' ) ;
9- const imgUploadSubmit = imgUploadForm . querySelector ( '.img-upload__submit' ) ;
10- const descriptionInput = imgUploadForm . querySelector ( '.text__description' ) ;
11- const hashtagsInput = imgUploadForm . querySelector ( '.text__hashtags' ) ;
12-
13- const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram' ;
14- const Route = {
15- GET_DATA : '/data' ,
16- SEND_DATA : '/' ,
17- } ;
18- const Method = {
19- GET : 'GET' ,
20- POST : 'POST' ,
21- } ;
22- const ErrorText = {
23- GET_DATA : 'Не удалось загрузить данные. Попробуйте обновить страницу' ,
24- SEND_DATA : 'Не удалось отправить форму. Попробуйте ещё раз' ,
8+ const errorMessage = {
9+ INVALID_HASHTAG : 'Введён невалидный хэштег' ,
10+ DESCRIPTION_TOO_LONG : 'Длина комментария больше 140 символов' ,
11+ HASHTAGS_TOO_MUCH : `Не более ${ MAX_HASHTAGS } хэштегов` ,
12+ HASHTAGS_UNIQUE :'Хэштеги повторяются.'
2513} ;
2614const SubmitButtonText = {
2715 IDLE : 'Сохранить' ,
2816 SENDING : 'Сохраняю...'
2917} ;
30-
31-
32- const load = ( route , errorText , method = Method . GET , body = null ) =>
33- fetch ( `${ BASE_URL } ${ route } ` , { method, body} )
34- . then ( ( response ) => {
35- if ( ! response . ok ) {
36- throw new Error ( ) ;
37- }
38- return response . json ( ) ;
39- } )
40- . catch ( ( ) => {
41- throw new Error ( errorText ) ;
42- } ) ;
43-
44- const getData = ( ) => load ( Route . GET_DATA , ErrorText . GET_DATA ) ;
45-
46- export const sendData = ( body ) => load ( Route . SEND_DATA , ErrorText . SEND_DATA , Method . POST , body ) ;
47-
48- getData ( )
49- . then ( ( gallery ) => {
50- renderGallery ( gallery ) ;
51- setGalleryData ( gallery ) ;
52- } )
53- . catch (
54- ( err ) => {
55- showErrorMessage ( err . message ) ;
56- }
57- ) ;
18+ const imgUploadForm = document . querySelector ( '.img-upload__form' ) ;
19+ const imgUploadSubmit = imgUploadForm . querySelector ( '.img-upload__submit' ) ;
20+ const descriptionInput = imgUploadForm . querySelector ( '.text__description' ) ;
21+ const hashtagsInput = imgUploadForm . querySelector ( '.text__hashtags' ) ;
5822
5923const toggleSubmitButton = ( disabled ) => {
6024 imgUploadSubmit . disabled = disabled ;
@@ -69,33 +33,31 @@ const pristine = new Pristine(imgUploadForm, {
6933 errorTextClass : 'img-upload__field-wrapper--error'
7034} ) ;
7135
72- const isHashtagsValid = ( value ) => {
36+ const getHashtags = ( value ) => {
7337 const trimmedValue = value . trim ( ) ;
38+ return trimmedValue === '' ? [ ] : trimmedValue . split ( / \s + / ) ;
39+ } ;
7440
75- if ( trimmedValue === '' ) {
76- return true ;
77- }
78-
79- const hashtagsArray = trimmedValue . split ( / \s + / ) ;
80-
81- if ( hashtagsArray . length > MAX_HASHTAGS ) {
82- return false ;
83- }
41+ const isHashtagsValid = ( value ) =>
42+ getHashtags ( value ) . every ( ( hashtag ) => hashtagPattern . test ( hashtag ) ) ;
8443
85- const uniqueHashtags = new Set ( hashtagsArray . map ( ( h ) => h . toLowerCase ( ) ) ) ;
44+ const isHashtagsCountValid = ( value ) =>
45+ getHashtags ( value ) . length <= MAX_HASHTAGS ;
8646
87- if ( uniqueHashtags . size !== hashtagsArray . length ) {
88- return false ;
89- }
47+ const isHashtagsUnique = ( value ) => {
48+ const hashtags = getHashtags ( value ) ;
49+ const uniqueHashtags = new Set ( hashtags . map ( ( h ) => h . toLowerCase ( ) ) ) ;
9050
91- return hashtagsArray . every ( ( hashtag ) => hashtagPattern . test ( hashtag ) ) ;
51+ return uniqueHashtags . size === hashtags . length ;
9252} ;
9353
9454const isDescriptionValid = ( value ) =>
9555 value . trim ( ) === '' || value . length <= MAX_DESCRIPTION_LEN ;
9656
97- pristine . addValidator ( hashtagsInput , isHashtagsValid , 'введён невалидный хэштег' ) ;
98- pristine . addValidator ( descriptionInput , isDescriptionValid , 'длина комментария больше 140 символов.' ) ;
57+ pristine . addValidator ( hashtagsInput , isHashtagsValid , errorMessage . INVALID_HASHTAG ) ;
58+ pristine . addValidator ( descriptionInput , isDescriptionValid , errorMessage . DESCRIPTION_TOO_LONG ) ;
59+ pristine . addValidator ( hashtagsInput , isHashtagsCountValid , errorMessage . HASHTAGS_TOO_MUCH ) ;
60+ pristine . addValidator ( hashtagsInput , isHashtagsUnique , errorMessage . HASHTAGS_UNIQUE ) ;
9961
10062imgUploadForm . addEventListener ( 'submit' , async ( evt ) => {
10163 evt . preventDefault ( ) ;
@@ -105,17 +67,13 @@ imgUploadForm.addEventListener('submit', async (evt) => {
10567 try {
10668 toggleSubmitButton ( true ) ;
10769
108- await sendData ( new FormData ( evt . target ) ) ;
70+ await sendPhoto ( new FormData ( evt . target ) ) ;
71+ showSuccess ( ) ;
10972 onCloseImageEditor ( ) ;
11073 } catch ( err ) {
111- showErrorMessage ( err . message ) ;
74+ showError ( err . message ) ;
11275 } finally {
11376 toggleSubmitButton ( false ) ;
11477 }
115- } else {
116- hashtagsInput . style . borderColor = 'red' ;
117-
118- hashtagsInput . focus ( ) ;
11978 }
12079} ) ;
121-
0 commit comments