-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathauth-page-component.js
More file actions
152 lines (129 loc) · 4.79 KB
/
auth-page-component.js
File metadata and controls
152 lines (129 loc) · 4.79 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
import { loginUser, registerUser } from "../api.js";
import { renderHeaderComponent } from "./header-component.js";
import { renderUploadImageComponent } from "./upload-image-component.js";
export function renderAuthPageComponent({ appEl, setUser }) {
let isLoginMode = true;
let imageUrl = "";
const renderForm = () => {
const appHtml = `
<div class="page-container">
<div class="header-container"></div>
<div class="form">
<h3 class="form-title">
${
isLoginMode
? "Вход в Instapro"
: "Регистрация в Instapro"
}
</h3>
<div class="form-inputs">
${
!isLoginMode
? `
<div class="upload-image-container"></div>
<input type="text" id="name-input" class="input" placeholder="Имя" />
`
: ""
}
<input type="text" id="login-input" class="input" placeholder="Логин" />
<input type="password" id="password-input" class="input" placeholder="Пароль" />
<div class="form-error"></div>
<button class="button" id="login-button">${
isLoginMode ? "Войти" : "Зарегистрироваться"
}</button>
</div>
<div class="form-footer">
<p class="form-footer-title">
${isLoginMode ? "Нет аккаунта?" : "Уже есть аккаунт?"}
<button class="link-button" id="toggle-button">
${isLoginMode ? "Зарегистрироваться." : "Войти."}
</button>
</p>
</div>
</div>
</div>
`;
appEl.innerHTML = appHtml;
// Не вызываем перерендер, чтобы не сбрасывалась заполненная форма
// Точечно обновляем кусочек дом дерева
const setError = (message) => {
appEl.querySelector(".form-error").textContent = message;
};
renderHeaderComponent({
element: document.querySelector(".header-container"),
});
const uploadImageContainer = appEl.querySelector(".upload-image-container");
if (uploadImageContainer) {
renderUploadImageComponent({
element: appEl.querySelector(".upload-image-container"),
onImageUrlChange(newImageUrl) {
imageUrl = newImageUrl;
},
});
}
document.getElementById("login-button").addEventListener("click", () => {
setError("");
if (isLoginMode) {
const login = document.getElementById("login-input").value;
const password = document.getElementById("password-input").value;
if (!login) {
alert("Введите логин");
return;
}
if (!password) {
alert("Введите пароль");
return;
}
loginUser({
login: login.replaceAll("<", "<").replaceAll(">", ">"),
password: password.replaceAll("<", "<").replaceAll(">", ">"),
})
.then((user) => {
setUser(user.user);
})
.catch((error) => {
console.warn(error);
setError(error.message);
});
} else {
const login = document.getElementById("login-input").value;
const name = document.getElementById("name-input").value;
const password = document.getElementById("password-input").value;
if (!name) {
alert("Введите имя");
return;
}
if (!login) {
alert("Введите логин");
return;
}
if (!password) {
alert("Введите пароль");
return;
}
if (!imageUrl) {
alert("Не выбрана фотография");
return;
}
registerUser({
login: login.replaceAll("<", "<").replaceAll(">", ">"),
password: password.replaceAll("<", "<").replaceAll(">", ">"),
name: name.replaceAll("<", "<").replaceAll(">", ">"),
imageUrl,
})
.then((user) => {
setUser(user.user);
})
.catch((error) => {
console.warn(error);
setError(error.message);
});
}
});
document.getElementById("toggle-button").addEventListener("click", () => {
isLoginMode = !isLoginMode;
renderForm();
});
};
renderForm();
}