-
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
Showing
34 changed files
with
648 additions
and
34 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
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
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,59 @@ | ||
import { Component } from '../../modules/Components/Component.js'; | ||
import { LiteralInput } from '/src/Components/FormInputs/LiteralInput/LiteralInput.js'; | ||
import { ValidatedTextArea } from '../FormInputs/ValidatedTextArea/ValidatedTextArea.js'; | ||
import { CvFormModel } from './CvFormModel.js'; | ||
import { CvFormView } from './CvFormView.js'; | ||
import { CvFormController } from './CvFormController.js'; | ||
|
||
export class CvForm extends Component { | ||
#isNew; | ||
constructor({ cvId = null, elementClass, existingElement }) { | ||
super({ | ||
modelClass: CvFormModel, | ||
viewClass: CvFormView, | ||
controllerClass: CvFormController, | ||
modelParams: { cvId }, | ||
existingElement, | ||
viewParams: { elementClass, isNew: !cvId }, | ||
}); | ||
this.#isNew = !cvId; | ||
this._positionRuField = new LiteralInput({ | ||
existingElement: this._view.positionRuField, | ||
selfValidate: true, | ||
}); | ||
this._positionEnField = new LiteralInput({ | ||
existingElement: this._view.positionEnField, | ||
selfValidate: true, | ||
}); | ||
this._jobSearchStatusField = new LiteralInput({ | ||
existingElement: this._view.jobSearchStatusField, | ||
selfValidate: true, | ||
}); | ||
this._descriptionField = new ValidatedTextArea({ | ||
existingElement: this._view.descriptionField, | ||
selfValidate: true, | ||
}); | ||
this._workingExperienceField = new ValidatedTextArea({ | ||
existingElement: this._view.workingExperienceField, | ||
selfValidate: true, | ||
}); | ||
this._children.push( | ||
this._positionEnField, | ||
this._positionRuField, | ||
this._jobSearchStatusField, | ||
this._descriptionField, | ||
this._workingExperienceField, | ||
); | ||
if (!this.#isNew) { | ||
this.reset(); | ||
} | ||
} | ||
|
||
get view() { | ||
return this._view; | ||
} | ||
|
||
reset() { | ||
return this._controller.reset(); | ||
} | ||
} |
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,64 @@ | ||
import { ComponentController } from '../../modules/Components/Component.js'; | ||
import { REDIRECT_TO, SUBMIT_FORM } from '../../modules/Events/Events.js'; | ||
import { CvPage } from '../../Pages/CvPage/CvPage.js'; | ||
import { Cv } from '../../modules/models/Cv.js'; | ||
import { resolveUrl } from '../../modules/UrlUtils/UrlUtils.js'; | ||
import eventBus from '../../modules/Events/EventBus.js'; | ||
|
||
export class CvFormController extends ComponentController { | ||
constructor(model, view, controller) { | ||
super(model, view, controller); | ||
this.setHandlers([ | ||
{ | ||
event: SUBMIT_FORM, | ||
handler: this.submit.bind(this), | ||
}, | ||
]); | ||
} | ||
|
||
_validate() { | ||
const errorMessage = this._model.validate(this._view.getData()); | ||
if (errorMessage) { | ||
return false; | ||
} | ||
return [ | ||
this._component._positionRuField.controller.validateInput({ | ||
callerView: this._component._positionRuField._view, | ||
}), | ||
this._component._positionEnField.controller.validateInput({ | ||
callerView: this._component._positionEnField._view, | ||
}), | ||
this._component._jobSearchStatusField.controller.validateInput({ | ||
callerView: this._component._jobSearchStatusField._view, | ||
}), | ||
this._component._workingExperienceField.controller.validateInput({ | ||
callerView: this._component._workingExperienceField._view, | ||
}), | ||
this._component._descriptionField.controller.validateInput({ | ||
callerView: this._component._descriptionField._view, | ||
}), | ||
].every((val) => val); | ||
} | ||
|
||
async submit({ caller }) { | ||
if (!Object.is(caller, this._view)) { | ||
return; | ||
} | ||
if (!this._validate()) { | ||
return; | ||
} | ||
const cv = await this._model.submit(new Cv(this._view.getData())); | ||
if (!cv) { | ||
return; | ||
} | ||
const query = {}; | ||
query[CvPage.CV_ID_PARAM] = cv.id; | ||
eventBus.emit(REDIRECT_TO, { redirectUrl: resolveUrl('cv', query) }); | ||
} | ||
|
||
async reset() { | ||
const oldData = await this._model.getLastValidData(); | ||
this._view.renderData(oldData); | ||
return true; | ||
} | ||
} |
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,53 @@ | ||
import { Api } from '../../modules/Api/Api.js'; | ||
import { ComponentModel } from '../../modules/Components/Component.js'; | ||
import { Cv } from '../../modules/models/Cv.js'; | ||
import { resolveUrl } from '../../modules/UrlUtils/UrlUtils.js'; | ||
import { zip } from '../../modules/ObjectUtils/Zip.js'; | ||
import eventBus from '../../modules/Events/EventBus.js'; | ||
import { REDIRECT_TO } from '../../modules/Events/Events.js'; | ||
|
||
export class CvFormModel extends ComponentModel { | ||
#lastValidData; | ||
#cvId; | ||
#isNew; | ||
|
||
constructor({ cvId = null }) { | ||
super(); | ||
this.#cvId = cvId; | ||
this.#isNew = !this.#cvId; | ||
this.#lastValidData = this.#cvId | ||
? Api.getCvById({ id: this.#cvId }).then( | ||
(cv) => new Cv(cv), | ||
() => { | ||
eventBus.emit(REDIRECT_TO, { redirectUrl: resolveUrl('createCv') }); | ||
}, | ||
) | ||
: null; | ||
} | ||
|
||
async getLastValidData() { | ||
return this.#lastValidData; | ||
} | ||
|
||
async submit(formData) { | ||
const cv = this.#isNew | ||
? await Api.createCv(formData) | ||
: await Api.updateCvById(zip({ id: this.#cvId }, formData)); | ||
if (cv) { | ||
this.#lastValidData = formData; | ||
return cv; | ||
} | ||
return null; | ||
} | ||
|
||
validate(formData) { | ||
const hasEmptyRequiredFields = [formData.positionRu, formData.jobSearchStatus].some( | ||
(fieldValue) => { | ||
return !fieldValue.trim(); | ||
}, | ||
); | ||
if (hasEmptyRequiredFields) { | ||
return 'Заполните обязательные поля'; | ||
} | ||
} | ||
} |
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,47 @@ | ||
import { ComponentView } from '../../modules/Components/Component.js'; | ||
import eventBus from '../../modules/Events/EventBus.js'; | ||
import { SUBMIT_FORM } from '../../modules/Events/Events.js'; | ||
import { addEventListeners } from '../../modules/Events/EventUtils.js'; | ||
import { getFormData } from '../../modules/FormUtils/FormUtils.js'; | ||
|
||
export class CvFormView extends ComponentView { | ||
constructor({ elementClass, isNew }, existingElement) { | ||
super({ | ||
renderParams: { elementClass, isNew }, | ||
existingElement, | ||
templateName: 'cv-form.hbs', | ||
}); | ||
this.positionRuField = this._html.querySelector('.cv-form__position-ru'); | ||
this.positionEnField = this._html.querySelector('.cv-form__position-en'); | ||
this.jobSearchStatusField = this._html.querySelector('.cv-form__job-search-status'); | ||
this.descriptionField = this._html.querySelector('.cv-form__description'); | ||
this.workingExperienceField = this._html.querySelector('.cv-form__working-experience'); | ||
|
||
this._eventListeners.push({ | ||
event: 'submit', | ||
object: this._html, | ||
listener: function (ev) { | ||
ev.preventDefault(); | ||
eventBus.emit(SUBMIT_FORM, { caller: this }); | ||
}.bind(this), | ||
}); | ||
addEventListeners(this._eventListeners); | ||
} | ||
|
||
getData() { | ||
return getFormData(this._html); | ||
} | ||
|
||
getId() { | ||
return 'cv-form'; | ||
} | ||
|
||
renderData({ positionRu, positionEn, jobSearchStatus, description, workingExperience }) { | ||
this.positionRuField.querySelector('.validated-input__input').value = positionRu; | ||
this.positionEnField.querySelector('.validated-input__input').value = positionEn; | ||
this.jobSearchStatusField.querySelector('.validated-input__input').value = jobSearchStatus; | ||
this.descriptionField.querySelector('.validated-textarea__textarea').value = description; | ||
this.workingExperienceField.querySelector('.validated-textarea__textarea').value = | ||
workingExperience; | ||
} | ||
} |
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 @@ | ||
<form class="{{elementClass}} cv-form" id="cv-form" method="POST" novalidate> | ||
{{> validated-input formName="cv-form" elementName="position-ru" inputName="positionRu" inputCaption="Должность" inputType="text"}} | ||
{{> validated-input formName="cv-form" elementName="position-en" inputName="positionEn" inputCaption="Перевод должности на английский" inputType="text"}} | ||
{{> validated-input formName="cv-form" elementName="job-search-status" inputName="jobSearchStatus" inputCaption="Статус поиска работы" inputType="text"}} | ||
{{> validated-textarea formName="cv-form" elementName="description" inputName="description" inputCaption="Описание и достижения"}} | ||
{{> validated-textarea formName="cv-form" elementName="working-experience" inputName="workingExperience" inputCaption="Опыт работы"}} | ||
<div class="cv-form__button-container"> | ||
{{#if isNew}} | ||
<button type="submit" class="cv-form__submit-button button button_main-primary"> | ||
Создать | ||
</button> | ||
{{else}} | ||
<button type="submit" class="cv-form__submit-button button button_main-primary"> | ||
Сохранить | ||
</button> | ||
{{/if}} | ||
<a class="cv-form__go-back-button button button_danger-tertiary" href="{{url "myProfile" ""}}">В профиль</a> | ||
</div> | ||
</form> |
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
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
Oops, something went wrong.