-
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
a3b7743
commit f1e8737
Showing
9 changed files
with
217 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,45 @@ | ||
// @ts-check | ||
import * as yup from 'yup'; | ||
import view from './scripts/view.js'; | ||
import strings from './utils/strings.js'; | ||
import { normalizeUrl } from './scripts/utilities.js'; | ||
import domElements from './utils/domElements.js'; | ||
|
||
const schema = yup.string().required('this is a required field').url(); | ||
|
||
export default () => { | ||
const element = document.getElementById('body'); | ||
console.log(element); | ||
const state = { | ||
formState: strings.formStates.init, | ||
feedList: [], | ||
feedback: null, | ||
}; | ||
|
||
const watchedState = view(state); | ||
|
||
domElements.form.submit.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
|
||
const normalizedValue = normalizeUrl(domElements.form.input.value); | ||
|
||
if (state.feedList.includes(normalizedValue)) { | ||
watchedState.formState = strings.formStates.invalid; | ||
watchedState.feedback = strings.feedback.exists; | ||
} else { | ||
schema | ||
.validate(normalizedValue) | ||
.then(() => { | ||
watchedState.formState = strings.formStates.sending; | ||
|
||
watchedState.feedList.push(domElements.form.input?.value); | ||
watchedState.feedback = strings.feedback.loaded; | ||
watchedState.formState = strings.formStates.init; | ||
|
||
domElements.form.input.value = ''; | ||
domElements.form.input?.focus(); | ||
}) | ||
.catch(() => { | ||
watchedState.formState = strings.formStates.invalid; | ||
watchedState.feedback = strings.feedback.invalidValidation; | ||
}); | ||
} | ||
}); | ||
}; |
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,5 @@ | ||
const normalizeUrl = (url) => url.trim().toLowerCase(); | ||
|
||
const normalizeError = (error) => error.toString().split(': ')[1]; | ||
|
||
export { normalizeUrl, normalizeError }; |
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,68 @@ | ||
// @ts-nocheck | ||
import onChange from 'on-change'; | ||
import strings from '../utils/strings.js'; | ||
import domElements from '../utils/domElements.js'; | ||
|
||
const renderFormState = (value) => { | ||
if (value === strings.formStates.sending) { | ||
domElements.form.submit?.setAttribute('disabled', ''); | ||
domElements.form.input?.setAttribute('disabled', ''); | ||
} else { | ||
domElements.form.submit?.removeAttribute('disabled'); | ||
domElements.form.input?.removeAttribute('disabled'); | ||
} | ||
|
||
if (value === strings.formStates.invalid) { | ||
domElements.form.input?.classList.add('is-invalid'); | ||
} else { | ||
domElements.form.input?.classList.remove('is-invalid'); | ||
} | ||
}; | ||
|
||
const renderFeedback = (value) => { | ||
switch (value) { | ||
case 'invalidValidation': | ||
domElements.form.feedback.textContent = 'Ссылка должна быть валидным URL'; | ||
break; | ||
case 'invalidRss': | ||
domElements.form.feedback.textContent = 'Ресурс не содержит валидный RSS'; | ||
break; | ||
case 'exists': | ||
domElements.form.feedback.textContent = 'RSS уже существует'; | ||
break; | ||
case 'loaded': | ||
domElements.form.feedback.textContent = 'RSS успешно загружен'; | ||
break; | ||
case null: | ||
domElements.form.feedback.textContent = ''; | ||
break; | ||
default: | ||
throw new Error(value); | ||
} | ||
|
||
if (value === 'loaded') { | ||
domElements.form.feedback.classList.replace('text-danger', 'text-success'); | ||
} else { | ||
domElements.form.feedback.classList.replace('text-success', 'text-danger'); | ||
} | ||
}; | ||
|
||
const renderFeedList = () => {}; | ||
|
||
const render = () => (path, value) => { | ||
switch (path) { | ||
case 'formState': | ||
renderFormState(value); | ||
break; | ||
case 'feedback': | ||
renderFeedback(value); | ||
break; | ||
case 'feedList': | ||
renderFeedList(); | ||
break; | ||
default: | ||
throw new Error(`Error: unresolved path: ${path}`); | ||
} | ||
}; | ||
|
||
export default (state) => onChange(state, render()); |
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,7 @@ | ||
export default { | ||
form: { | ||
input: document.getElementById('url-input'), | ||
submit: document.querySelector('button[type="submit"]'), | ||
feedback: document.getElementById('form-feedback'), | ||
}, | ||
}; |
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,14 @@ | ||
export default { | ||
formStates: { | ||
init: 'init', | ||
invalid: 'invalid', | ||
sending: 'sending', | ||
valid: 'valid', | ||
}, | ||
feedback: { | ||
invalidValidation: 'invalidValidation', | ||
invalidRss: 'invalidRss', | ||
exists: 'exists', | ||
loaded: 'loaded', | ||
}, | ||
}; |
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