mirror of
https://github.com/Nezumi-2711/javascript-training.git
synced 2026-09-23 04:09:58 +00:00
Fix MVC design pattern
This commit is contained in:
@@ -4,42 +4,20 @@ export default class RegisterController {
|
|||||||
this.service = service;
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
async controlRegister() {
|
hanlderCheckUserExist(email) {
|
||||||
try {
|
return this.service.userService.checkUserExist(email);
|
||||||
// Load spinner
|
}
|
||||||
this.registerView.toogleLoaderSpinner();
|
|
||||||
|
|
||||||
// Get data from form
|
handlerSaveUser(user) {
|
||||||
const user = this.registerView.getDataFromForm();
|
return this.service.userService.saveUser(user);
|
||||||
|
|
||||||
// Save user
|
|
||||||
if (user) {
|
|
||||||
// Check user exist
|
|
||||||
const userExist = await this.service.userService.checkUserExist(
|
|
||||||
user.email,
|
|
||||||
);
|
|
||||||
if (userExist) {
|
|
||||||
throw Error('User is exists! Please try another email!');
|
|
||||||
} else {
|
|
||||||
await this.service.userService.saveUser(user);
|
|
||||||
// Show popup success
|
|
||||||
this.registerView.initRegisterSuccessPopup();
|
|
||||||
this.registerView.tooglePopupForm();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Show popup error
|
|
||||||
this.registerView.initErrorPopup(error);
|
|
||||||
this.registerView.tooglePopupForm();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close spinner
|
|
||||||
this.registerView.toogleLoaderSpinner();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if (this.registerView.registerForm !== null) {
|
if (this.registerView.checkRegisterFormElExist()) {
|
||||||
this.registerView.addHandlerForm(this.controlRegister.bind(this));
|
this.registerView.addHandlerForm(
|
||||||
|
this.hanlderCheckUserExist.bind(this),
|
||||||
|
this.handlerSaveUser.bind(this),
|
||||||
|
);
|
||||||
this.registerView.addHandlerInputFormChange();
|
this.registerView.addHandlerInputFormChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,18 +31,6 @@ export default class CommonLoginRegisterView extends CommonView {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Add event listener for form input
|
|
||||||
* @param {Function} handler The function need to be set event
|
|
||||||
*/
|
|
||||||
addHandlerForm(handler) {
|
|
||||||
this.parentElement.addEventListener('submit', (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
this.clearErrorMessage();
|
|
||||||
handler();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show or hide style error input password
|
* Show or hide style error input password
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -32,13 +32,16 @@ export default class RegisterView extends CommonLoginRegisterView {
|
|||||||
/**
|
/**
|
||||||
* Implement register success popup in site
|
* Implement register success popup in site
|
||||||
*/
|
*/
|
||||||
initRegisterSuccessPopup() {
|
showRegisterSuccessPopup() {
|
||||||
const typePopup = TYPE_POPUP.success;
|
const typePopup = TYPE_POPUP.success;
|
||||||
const title = 'Register Commpleted';
|
const title = 'Register Commpleted';
|
||||||
const content = 'Please login to continue!';
|
const content = 'Please login to continue!';
|
||||||
const btnContent = 'OK';
|
const btnContent = 'OK';
|
||||||
|
|
||||||
this.initPopupContent(typePopup, title, content, btnContent);
|
this.initPopupContent(typePopup, title, content, btnContent);
|
||||||
|
|
||||||
|
// Show popup
|
||||||
|
this.tooglePopupForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,5 +54,53 @@ export default class RegisterView extends CommonLoginRegisterView {
|
|||||||
const btnContent = 'Got it!';
|
const btnContent = 'Got it!';
|
||||||
|
|
||||||
this.initPopupContent(typePopup, title, content, btnContent);
|
this.initPopupContent(typePopup, title, content, btnContent);
|
||||||
|
|
||||||
|
// Show popup
|
||||||
|
this.tooglePopupForm();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add event listener for form input
|
||||||
|
* @param {Function} handler The function need to be set event
|
||||||
|
*/
|
||||||
|
addHandlerForm(checkExistUser, saveUser) {
|
||||||
|
this.parentElement.addEventListener('submit', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
this.clearErrorMessage();
|
||||||
|
this.submitForm(checkExistUser, saveUser);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async submitForm(checkExistUser, saveUser) {
|
||||||
|
try {
|
||||||
|
// Load spinner
|
||||||
|
this.toogleLoaderSpinner();
|
||||||
|
|
||||||
|
// Get data from form
|
||||||
|
const user = this.getDataFromForm();
|
||||||
|
|
||||||
|
// Save user
|
||||||
|
if (user) {
|
||||||
|
// Check user exist
|
||||||
|
const userExist = await checkExistUser(user.email);
|
||||||
|
if (userExist) {
|
||||||
|
throw Error('User is exists! Please try another email!');
|
||||||
|
} else {
|
||||||
|
await saveUser(user);
|
||||||
|
// Show popup success
|
||||||
|
this.showRegisterSuccessPopup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Show popup error
|
||||||
|
this.initErrorPopup(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close spinner
|
||||||
|
this.toogleLoaderSpinner();
|
||||||
|
}
|
||||||
|
|
||||||
|
checkRegisterFormElExist() {
|
||||||
|
return this.registerForm !== null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user