diff --git a/src/Components/ApplicantProfileForm/ApplicantProfileFormController.js b/src/Components/ApplicantProfileForm/ApplicantProfileFormController.js index 7fb5598..967219a 100644 --- a/src/Components/ApplicantProfileForm/ApplicantProfileFormController.js +++ b/src/Components/ApplicantProfileForm/ApplicantProfileFormController.js @@ -1,6 +1,7 @@ import { ComponentController } from '../../modules/Components/Component.js'; import eventBus from '../../modules/Events/EventBus.js'; -import { USER_UPDATED } from '../../modules/Events/Events.js'; +import { NOTIFICATION_OK, USER_UPDATED } from '../../modules/Events/Events.js'; +import { NOTIFICATION_TIMEOUT } from '../NotificationBox/NotificationBox.js'; export class ApplicantProfileFormController extends ComponentController { constructor(model, view, controller) { @@ -40,6 +41,10 @@ export class ApplicantProfileFormController extends ComponentController { return false; } eventBus.emit(USER_UPDATED); + eventBus.emit(NOTIFICATION_OK, { + message: 'Успешно сохранено', + timeout: NOTIFICATION_TIMEOUT.MEDIUM, + }); return true; } diff --git a/src/Components/ApplicantProfileForm/ApplicantProfileFormModel.js b/src/Components/ApplicantProfileForm/ApplicantProfileFormModel.js index e787aeb..e00096b 100644 --- a/src/Components/ApplicantProfileForm/ApplicantProfileFormModel.js +++ b/src/Components/ApplicantProfileForm/ApplicantProfileFormModel.js @@ -1,6 +1,7 @@ import { ComponentModel } from '../../modules/Components/Component.js'; import { Api } from '../../modules/Api/Api.js'; import { Applicant } from '../../modules/models/Applicant.js'; +import { catchStandardResponseError } from '../../modules/Api/Errors.js'; export class ApplicantProfileFormModel extends ComponentModel { #lastValidData; @@ -23,9 +24,14 @@ export class ApplicantProfileFormModel extends ComponentModel { async submit(formData) { formData.birthDate = new Date(formData.birthDate); formData.id = this.#userId; - if (await Api.updateApplicantProfile(formData)) { - this.#lastValidData = new Applicant(formData); + try { + await Api.updateApplicantProfile(formData); + const app = new Applicant(formData); + app.birthDate = app.birthDate.toISOString().split('T')[0]; + this.#lastValidData = app; return true; + } catch (err) { + catchStandardResponseError(err); } return false; } diff --git a/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormController.js b/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormController.js index 6f31206..fdc73d8 100644 --- a/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormController.js +++ b/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormController.js @@ -2,6 +2,9 @@ import { ComponentController } from '../../modules/Components/Component.js'; import { REGISTER_APPLICANT } from '../../modules/Events/Events.js'; import router from '/src/modules/Router/Router.js'; import { resolveUrl } from '../../modules/UrlUtils/UrlUtils.js'; +import eventBus from '../../modules/Events/EventBus.js'; +import { NOTIFICATION_OK } from '../../modules/Events/Events.js'; +import { NOTIFICATION_TIMEOUT } from '../NotificationBox/NotificationBox.js'; export class ApplicantRegistrationFormController extends ComponentController { constructor(model, view, controller) { @@ -20,14 +23,19 @@ export class ApplicantRegistrationFormController extends ComponentController { } this._model .register(formData) - .then(() => router.navigate(new URL(resolveUrl('vacancies')), true, true)) + .then(() => { + eventBus.emit(NOTIFICATION_OK, { + message: 'Успешно сохранено', + timeout: NOTIFICATION_TIMEOUT.MEDIUM, + }); + router.navigate(new URL(resolveUrl('vacancies')), true, true); + }) .catch((errorMsg) => { this._view.declineValidation(errorMsg); }); } _validate(formData) { - this._view.hideError(); const formValidationError = this._model.validate(formData); if (formValidationError) { this._view.declineValidation(formValidationError); diff --git a/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormModel.js b/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormModel.js index b26ad0d..08c262c 100644 --- a/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormModel.js +++ b/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormModel.js @@ -1,6 +1,8 @@ import state from '../../modules/AppState/AppState.js'; import { ComponentModel } from '../../modules/Components/Component.js'; import USER_TYPE from '../../modules/UserSession/UserType.js'; +import { USER_ALREADY_EXISTS_ERROR } from '../../modules/Api/Errors.js'; +import { TransportError, ResponseError } from '../../modules/Api/Api.js'; export class ApplicantRegistrationFormModel extends ComponentModel { validate(formData) { @@ -18,6 +20,15 @@ export class ApplicantRegistrationFormModel extends ComponentModel { } async register(formData) { - return state.userSession.register(USER_TYPE.APPLICANT, formData); + return state.userSession.register(USER_TYPE.APPLICANT, formData).catch((err) => { + if (err.toString() === USER_ALREADY_EXISTS_ERROR) { + return Promise.reject('Соискатель с таким адресом электронной почты уже зарегистрирован'); + } + if (err instanceof TransportError) { + return Promise.reject('Произошла сетевая ошибка, повторите позднее'); + } + if (err instanceof ResponseError) + return Promise.reject('Произошла непредвиденная ошибка, повторите позднее'); + }); } } diff --git a/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormView.js b/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormView.js index a09928d..83f7e9b 100644 --- a/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormView.js +++ b/src/Components/ApplicantRegistrationForm/ApplicantRegistrationFormView.js @@ -1,8 +1,10 @@ import { ComponentView } from '../../modules/Components/Component.js'; +import { NOTIFICATION_ERROR } from '../../modules/Events/Events.js'; import { addEventListeners } from '../../modules/Events/EventUtils.js'; import { getFormData } from '../../modules/FormUtils/FormUtils.js'; import eventBus from '/src/modules/Events/EventBus.js'; import { REGISTER_APPLICANT } from '/src/modules/Events/Events.js'; +import { NOTIFICATION_TIMEOUT } from '../NotificationBox/NotificationBox.js'; export class ApplicantRegistrationFormView extends ComponentView { constructor({ elementClass }, existingElement) { @@ -28,19 +30,16 @@ export class ApplicantRegistrationFormView extends ComponentView { this.repeatPasswordField = this._html.querySelector( '.applicant-registration-form__repeat-password', ); - this.error = this._html.querySelector('.applicant-registration-form__error'); } getData() { return getFormData(this._html); } - hideError() { - this.error.hidden = true; - } - declineValidation(errorMessage) { - this.error.innerText = errorMessage; - this.error.hidden = false; + eventBus.emit(NOTIFICATION_ERROR, { + message: errorMessage, + timeout: NOTIFICATION_TIMEOUT.MEDIUM, + }); } } diff --git a/src/Components/ApplicantRegistrationForm/applicant-registration-form.hbs b/src/Components/ApplicantRegistrationForm/applicant-registration-form.hbs index 34ea4b1..fcb996b 100644 --- a/src/Components/ApplicantRegistrationForm/applicant-registration-form.hbs +++ b/src/Components/ApplicantRegistrationForm/applicant-registration-form.hbs @@ -1,5 +1,4 @@
\ No newline at end of file diff --git a/src/Components/EmployerCompanyForm/EmployerProfileForm.js b/src/Components/EmployerCompanyForm/EmployerProfileForm.js deleted file mode 100644 index 2d8767d..0000000 --- a/src/Components/EmployerCompanyForm/EmployerProfileForm.js +++ /dev/null @@ -1,72 +0,0 @@ -import { Component } from '../../modules/Components/Component.js'; -import { EmployerProfileFormView } from './EmployerProfileFormView.js'; -import { EmployerProfileFormModel } from './EmployerProfileFormModel.js'; -import { EmployerProfileFormController } from './EmployerProfileFormController.js'; -import { LiteralInput } from '/src/Components/FormInputs/LiteralInput/LiteralInput.js'; -import { CityInput } from '/src/Components/FormInputs/CityInput/CityInput.js'; -import { ValidatedTextArea } from '../FormInputs/ValidatedTextArea/ValidatedTextArea.js'; - -export class EmployerProfileForm extends Component { - constructor({ userId, elementClass, existingElement }) { - super({ - modelClass: EmployerProfileFormModel, - viewClass: EmployerProfileFormView, - controllerClass: EmployerProfileFormController, - modelParams: { userId }, - existingElement, - viewParams: { elementClass }, - }); - this._firstNameField = new LiteralInput({ - existingElement: this._view.firstNameField, - selfValidate: true, - }); - this._secondNameField = new LiteralInput({ - existingElement: this._view.secondNameField, - selfValidate: true, - }); - this._cityField = new CityInput({ - existingElement: this._view.cityField, - selfValidate: true, - }); - this._contactsField = new ValidatedTextArea({ - existingElement: this._view.contactsField, - selfValidate: true, - }); - this._children.push( - this._firstNameField, - this._secondNameField, - this._cityField, - this._contactsField, - ); - - this.reset(); - } - - get view() { - return this._view; - } - - enable() { - [this._firstNameField, this._secondNameField, this._cityField, this._contactsField].forEach( - (field) => { - field.controller.enable(); - }, - ); - } - - disable() { - [this._firstNameField, this._secondNameField, this._cityField, this._contactsField].forEach( - (field) => { - field.controller.disable(); - }, - ); - } - - reset() { - return this._controller.reset(); - } - - submit() { - return this._controller.submit(); - } -} diff --git a/src/Components/EmployerCompanyForm/EmployerProfileFormController.js b/src/Components/EmployerCompanyForm/EmployerProfileFormController.js deleted file mode 100644 index 3375312..0000000 --- a/src/Components/EmployerCompanyForm/EmployerProfileFormController.js +++ /dev/null @@ -1,43 +0,0 @@ -import { ComponentController } from '../../modules/Components/Component.js'; -import eventBus from '../../modules/Events/EventBus.js'; -import { USER_UPDATED } from '../../modules/Events/Events.js'; - -export class EmployerProfileFormController extends ComponentController { - constructor(model, view, controller) { - super(model, view, controller); - } - - _validate() { - return [ - this._component._firstNameField.controller.validateInput({ - callerView: this._component._firstNameField._view, - }), - - this._component._secondNameField.controller.validateInput({ - callerView: this._component._secondNameField._view, - }), - - this._component._cityField.controller.validateInput({ - callerView: this._component._cityField._view, - }), - - this._component._contactsField.controller.validateInput({ - callerView: this._component._contactsField._view, - }), - ].every((val) => val); - } - - submit() { - if (!this._validate() || !this._model.submit(this._view.getData())) { - return false; - } - eventBus.emit(USER_UPDATED); - return true; - } - - async reset() { - const oldData = await this._model.lastValidData; - this._view.renderData(oldData); - return true; - } -} diff --git a/src/Components/EmployerCompanyForm/EmployerProfileFormModel.js b/src/Components/EmployerCompanyForm/EmployerProfileFormModel.js deleted file mode 100644 index 09df162..0000000 --- a/src/Components/EmployerCompanyForm/EmployerProfileFormModel.js +++ /dev/null @@ -1,23 +0,0 @@ -import { Api } from '../../modules/Api/Api.js'; -import { ComponentModel } from '../../modules/Components/Component.js'; - -export class EmployerProfileFormModel extends ComponentModel { - #lastValidData; - - constructor({ userId }) { - super(); - this.#lastValidData = Api.getApplicantById({ id: userId }); - } - - get lastValidData() { - return this.#lastValidData; - } - - async submit(formData) { - if (Api.updateEmployerProfile(formData)) { - this.#lastValidData = formData; - return true; - } - return false; - } -} diff --git a/src/Components/EmployerCompanyForm/EmployerProfileFormView.js b/src/Components/EmployerCompanyForm/EmployerProfileFormView.js deleted file mode 100644 index 0193739..0000000 --- a/src/Components/EmployerCompanyForm/EmployerProfileFormView.js +++ /dev/null @@ -1,31 +0,0 @@ -import { ComponentView } from '../../modules/Components/Component.js'; -import { getFormData } from '../../modules/FormUtils/FormUtils.js'; - -export class EmployerProfileFormView extends ComponentView { - constructor({ elementClass }, existingElement) { - super({ - renderParams: { elementClass }, - existingElement, - templateName: 'employer-profile-form.hbs', - }); - this.firstNameField = this._html.querySelector('.employer-profile-form__first-name'); - this.secondNameField = this._html.querySelector('.employer-profile-form__second-name'); - this.cityField = this._html.querySelector('.employer-profile-form__city'); - this.contactsField = this._html.querySelector('.employer-profile-form__contacts'); - } - - getData() { - return getFormData(this._html); - } - - getId() { - return 'employer-profile-form'; - } - - renderData({ firstName, secondName, city, contacts }) { - this.firstNameField.querySelector('.validated-input__input').value = firstName; - this.secondNameField.querySelector('.validated-input__input').value = secondName; - this.cityField.querySelector('.validated-input__input').value = city; - this.contactsField.querySelector('.validated-textarea__textarea').value = contacts; - } -} diff --git a/src/Components/EmployerCompanyForm/employer-company-form.hbs b/src/Components/EmployerCompanyForm/employer-company-form.hbs deleted file mode 100644 index 13bffdc..0000000 --- a/src/Components/EmployerCompanyForm/employer-company-form.hbs +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file diff --git a/src/Components/EmployerProfileForm/EmployerProfileFormController.js b/src/Components/EmployerProfileForm/EmployerProfileFormController.js index 7c7a1a1..4598626 100644 --- a/src/Components/EmployerProfileForm/EmployerProfileFormController.js +++ b/src/Components/EmployerProfileForm/EmployerProfileFormController.js @@ -1,6 +1,7 @@ import { ComponentController } from '../../modules/Components/Component.js'; import eventBus from '../../modules/Events/EventBus.js'; -import { USER_UPDATED } from '../../modules/Events/Events.js'; +import { NOTIFICATION_OK, USER_UPDATED } from '../../modules/Events/Events.js'; +import { NOTIFICATION_TIMEOUT } from '../NotificationBox/NotificationBox.js'; export class EmployerProfileFormController extends ComponentController { constructor(model, view, controller) { @@ -32,6 +33,10 @@ export class EmployerProfileFormController extends ComponentController { return false; } eventBus.emit(USER_UPDATED); + eventBus.emit(NOTIFICATION_OK, { + message: 'Успешно сохранено', + timeout: NOTIFICATION_TIMEOUT.MEDIUM, + }); return true; } diff --git a/src/Components/EmployerProfileForm/EmployerProfileFormModel.js b/src/Components/EmployerProfileForm/EmployerProfileFormModel.js index 0b025e2..fdbb93e 100644 --- a/src/Components/EmployerProfileForm/EmployerProfileFormModel.js +++ b/src/Components/EmployerProfileForm/EmployerProfileFormModel.js @@ -1,4 +1,5 @@ import { Api } from '../../modules/Api/Api.js'; +import { catchStandardResponseError } from '../../modules/Api/Errors.js'; import { ComponentModel } from '../../modules/Components/Component.js'; import { Employer } from '../../modules/models/Employer.js'; @@ -19,11 +20,13 @@ export class EmployerProfileFormModel extends ComponentModel { } async submit(formData) { - formData.birthDate = new Date(formData.birthDate); formData.id = this.#userId; - if (await Api.updateEmployerProfile(formData)) { - this.#lastValidData = new Employer(formData); + try { + const response = await Api.updateEmployerProfile(formData); + this.#lastValidData = new Employer(response); return true; + } catch (err) { + catchStandardResponseError(err); } return false; } diff --git a/src/Components/EmployerRegistrationForm/EmployerRegistrationFormController.js b/src/Components/EmployerRegistrationForm/EmployerRegistrationFormController.js index 7987227..29c938a 100644 --- a/src/Components/EmployerRegistrationForm/EmployerRegistrationFormController.js +++ b/src/Components/EmployerRegistrationForm/EmployerRegistrationFormController.js @@ -27,7 +27,6 @@ export class EmployerRegistrationFormController extends ComponentController { } _validate(formData) { - this._view.hideError(); const formValidationError = this._model.validate(formData); if (formValidationError) { this._view.declineValidation(formValidationError); diff --git a/src/Components/EmployerRegistrationForm/EmployerRegistrationFormModel.js b/src/Components/EmployerRegistrationForm/EmployerRegistrationFormModel.js index 535dd60..8ce5810 100644 --- a/src/Components/EmployerRegistrationForm/EmployerRegistrationFormModel.js +++ b/src/Components/EmployerRegistrationForm/EmployerRegistrationFormModel.js @@ -1,6 +1,8 @@ import { ComponentModel } from '../../modules/Components/Component.js'; import state from '../../modules/AppState/AppState.js'; import USER_TYPE from '../../modules/UserSession/UserType.js'; +import { USER_ALREADY_EXISTS_ERROR } from '../../modules/Api/Errors.js'; +import { ResponseError, TransportError } from '../../modules/Api/Api.js'; export class EmployerRegistrationFormModel extends ComponentModel { validate(formData) { @@ -18,6 +20,15 @@ export class EmployerRegistrationFormModel extends ComponentModel { } async register(formData) { - return state.userSession.register(USER_TYPE.EMPLOYER, formData); + return state.userSession.register(USER_TYPE.EMPLOYER, formData).catch((err) => { + if (err.toString() === USER_ALREADY_EXISTS_ERROR) { + return Promise.reject('Работодатель с таким адресом электронной почты уже зарегистрирован'); + } + if (err instanceof TransportError) { + return Promise.reject('Произошла сетевая ошибка, повторите позднее'); + } + if (err instanceof ResponseError) + return Promise.reject('Произошла непредвиденная ошибка, повторите позднее'); + }); } } diff --git a/src/Components/EmployerRegistrationForm/EmployerRegistrationFormView.js b/src/Components/EmployerRegistrationForm/EmployerRegistrationFormView.js index 6a383ad..e3d4962 100644 --- a/src/Components/EmployerRegistrationForm/EmployerRegistrationFormView.js +++ b/src/Components/EmployerRegistrationForm/EmployerRegistrationFormView.js @@ -1,6 +1,8 @@ import { ComponentView } from '../../modules/Components/Component.js'; +import { NOTIFICATION_ERROR } from '../../modules/Events/Events.js'; import { addEventListeners } from '../../modules/Events/EventUtils.js'; import { getFormData } from '../../modules/FormUtils/FormUtils.js'; +import { NOTIFICATION_TIMEOUT } from '../NotificationBox/NotificationBox.js'; import eventBus from '/src/modules/Events/EventBus.js'; import { REGISTER_EMPLOYER } from '/src/modules/Events/Events.js'; @@ -40,12 +42,10 @@ export class EmployerRegistrationFormView extends ComponentView { return getFormData(this._html); } - hideError() { - this.error.hidden = true; - } - declineValidation(errorMessage) { - this.error.innerText = errorMessage; - this.error.hidden = false; + eventBus.emit(NOTIFICATION_ERROR, { + message: errorMessage, + timeout: NOTIFICATION_TIMEOUT.MEDIUM, + }); } } diff --git a/src/Components/EmployerRegistrationForm/employer-registration-form.hbs b/src/Components/EmployerRegistrationForm/employer-registration-form.hbs index f04b5c1..c32152f 100644 --- a/src/Components/EmployerRegistrationForm/employer-registration-form.hbs +++ b/src/Components/EmployerRegistrationForm/employer-registration-form.hbs @@ -1,5 +1,4 @@