11import { REGEXP } from "../../utils/constants.js" ;
22import { loginUser } from "./loginModel.js" ;
33
4+ //===================================================================================================================
45export function loginController ( loginForm ) {
56
7+ //-------------------------------------------------------------------------------------------------------------------
68 loginForm . addEventListener ( "submit" , ( event ) => {
79 event . preventDefault ( ) ;
810
@@ -16,57 +18,79 @@ export function loginController(loginForm) {
1618
1719 if ( emailRegExp . test ( userEmail ) ) {
1820
19- handleLoginUser ( userEmail , password , loginForm )
21+ handleLoginUser ( userEmail , password )
2022
2123 } else {
2224
23- dispatchLoginError ( loginForm , 'Incorrect mail format' ) ;
25+ dispatchNotification ( 'login-error' , 'Incorrect mail format' )
26+ //dispatchLoginError(loginForm, 'Incorrect mail format');
2427 }
2528 } )
2629
27- async function handleLoginUser ( userEmail , password , loginForm ) {
30+ //-------------------------------------------------------------------------------------------------------------------
31+ async function handleLoginUser ( userEmail , password ) {
2832
2933 try {
3034
35+ /*
3136 const existingToken = localStorage.getItem("accessToken");
3237
3338 if (existingToken) {
3439 throw new Error("There is already an active session. Log out before logging in with another user.");
35- }
40+ }
41+ */
3642
43+ //----------------------------------------------------
44+ const event = new CustomEvent ( "load-posts-started" ) ;
45+ loginForm . dispatchEvent ( event ) ;
46+ //----------------------------------------------------
47+
48+ //====================================================
3749 const token = await loginUser ( userEmail , password ) ;
50+ //====================================================
3851
39- dispatchLoginSuccess ( loginForm , 'You have successfully logged in.' ) ;
52+ dispatchNotification ( 'login-ok' , {
53+ message : "You have successfully logged in." ,
54+ type : 'success'
55+ } )
56+ //dispatchLoginSuccess(loginForm, 'You have successfully logged in.');
4057
4158 localStorage . setItem ( "accessToken" , token )
4259
43- setTimeout ( ( ) => {
44- window . location = '/index.html'
45- } , 500 ) ;
46-
60+ setTimeout ( ( ) => { window . location = '/index.html' } , 500 ) ;
4761
4862 } catch ( error ) {
4963
50- dispatchLoginError ( loginForm , error . message ) ;
51-
64+ dispatchNotification ( 'login-error' , error . message )
65+ //dispatchLoginError(loginForm, error.message);
66+ } finally {
67+ //----------------------------------------------------
68+ const event = new CustomEvent ( "load-posts-finished" )
69+ loginForm . dispatchEvent ( event )
70+ //----------------------------------------------------
5271 }
5372 }
54-
55- function dispatchLoginSuccess ( loginForm , successMessage ) {
56- const event = new CustomEvent ( "login-ok" , {
57- detail : {
58- message : successMessage ,
59- type : 'success'
60- }
61- } ) ;
62- loginForm . dispatchEvent ( event ) ;
63- }
64-
65- function dispatchLoginError ( loginForm , errorMessage ) {
66- const event = new CustomEvent ( "login-error" , {
67- detail : errorMessage
68- } ) ;
69- loginForm . dispatchEvent ( event ) ;
73+ //-------------------------------------------------------------------------------------------------------------------
74+ function dispatchNotification ( eventType , message ) {
75+ const event = new CustomEvent ( eventType , { detail : message } )
76+ loginForm . dispatchEvent ( event )
7077 }
78+ //-------------------------------------------------------------------------------------------------------------------
79+ /* function dispatchLoginSuccess(loginForm, successMessage) {
80+ const event = new CustomEvent("login-ok", {
81+ detail: {
82+ message: successMessage,
83+ type: 'success'
84+ }
85+ });
86+ loginForm.dispatchEvent(event);
87+ }
88+
89+ function dispatchLoginError(loginForm, errorMessage) {
90+ const event = new CustomEvent("login-error", {
91+ detail: errorMessage
92+ });
93+ loginForm.dispatchEvent(event);
94+ } */
7195}
7296
0 commit comments