-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (151 loc) · 6.45 KB
/
script.js
File metadata and controls
171 lines (151 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
document
.getElementById("loginForm")
.addEventListener("submit", function (event) {
event.preventDefault();
const email = document.getElementById("loginEmail").value;
const password = document.getElementById("loginPassword").value;
fetch("http://localhost:8080/api/Login/UserAuth", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
// Сохраняем email в localStorage для использования на следующих страницах
localStorage.setItem("userEmail", email);
window.location.href = "index.html";
} else {
document.getElementById("message").textContent =
"Пользователь с таким email не найден!";
document.getElementById("messageContainer").style.display = "block";
}
})
.catch((error) => {
console.error("Ошибка:", error);
document.getElementById("message").textContent =
"Произошла ошибка. Попробуйте позже.";
document.getElementById("messageContainer").style.display = "block";
});
});
document
.querySelector("#openRegistrationModal")
.addEventListener("click", function () {
// Закрыть предыдущее модальное окно (если оно открыто)
const loginModal = bootstrap.Modal.getInstance(
document.getElementById("loginModal")
);
if (loginModal) {
loginModal.hide();
}
// Открыть модальное окно регистрации
const registrationModal = new bootstrap.Modal(
document.getElementById("registrationModal")
);
registrationModal.show();
// Очистить поля формы регистрации
document.getElementById("registrationForm").reset();
});
document
.getElementById("registrationForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Предотвращаем стандартное действие отправки формы
const name = document.getElementById("name").value;
const surname = document.getElementById("surname").value;
const birthday = document.getElementById("birthday").value;
const email = document.getElementById("registerEmail").value;
const phone = document.getElementById("phone").value;
const address = document.getElementById("address").value;
const username = document.getElementById("username").value;
const password = document.getElementById("registerPassword").value;
const photo = document.getElementById("photo").value;
const userData = {
name,
surname,
birthday,
email,
phone,
address,
username,
password,
photo,
};
fetch("http://localhost:8080/api/Register/UserReg", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userData),
})
.then((response) => response.json())
.then((data) => {
if (data) {
// Закрываем окно регистрации перед показом окна успеха
const registrationModal = bootstrap.Modal.getInstance(
document.getElementById("registrationModal")
);
if (registrationModal) {
registrationModal.hide();
}
// Показываем модальное окно об успешной регистрации
const successModal = new bootstrap.Modal(
document.getElementById("successModal")
);
successModal.show();
// Очистить форму регистрации
document.getElementById("registrationForm").reset();
} else {
document.getElementById("message").textContent =
"Произошла ошибка при регистрации!";
}
document.getElementById("messageContainer").style.display = "block";
})
.catch((error) => {
console.error("Ошибка:", error);
document.getElementById("message").textContent =
"Произошла ошибка. Попробуйте позже.";
document.getElementById("messageContainer").style.display = "block";
});
});
// ------ для отображения username и кнопки login/exit -----------------------------------------------------------------------------
window.onload = function () {
// Получаем email из localStorage
const email = localStorage.getItem("userEmail");
if (email) {
const url = `http://localhost:8080/api/Profile/GetProfile/${email}`;
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error("Не удалось получить данные пользователя");
}
return response.json();
})
.then((data) => {
const userElement = document.getElementById("user");
userElement.textContent = `${data.name} ${data.surname} (${data.username})`;
// Показываем кнопку "Выход" и скрываем кнопку "Вход"
document.getElementById("exitButton").style.display = "inline-block";
document.getElementById("loginButton").style.display = "none";
})
.catch((error) => {
console.error("Ошибка:", error);
document.getElementById("greeting").textContent =
"Произошла ошибка при загрузке данных.";
});
} else {
// Если email не найден в localStorage, отображаем сообщение о неавторизованном доступе
const userElement = document.getElementById("user");
userElement.textContent = "Вход не выполнен";
document.getElementById("loginButton").style.display = "inline-block";
document.getElementById("exitButton").style.display = "none";
}
document.getElementById("exitButton").addEventListener("click", function () {
localStorage.removeItem("userEmail");
const userElement = document.getElementById("user");
userElement.textContent = "Вход не выполнен";
document.getElementById("loginButton").style.display = "inline-block";
document.getElementById("exitButton").style.display = "none";
});
};