Skip to content

Commit

Permalink
Merge pull request #14
Browse files Browse the repository at this point in the history
Routes
  • Loading branch information
oFem1m authored Apr 2, 2024
2 parents 2ec9398 + 775032a commit ac66ac3
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 86 deletions.
5 changes: 5 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ app.use(express.static(path.join(__dirname, '..', 'src')));
app.use(express.static(path.join(__dirname, '..', 'uploads')));
app.use(cookie());

// Обработка всех остальных запросов
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, '..', 'src', 'index.html'));
});

const port = process.env.PORT || 3000;

app.listen(port, function () {
Expand Down
8 changes: 3 additions & 5 deletions src/Components/ChatList/ChatList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BaseComponent } from '../BaseComponent.js';
import ChatListItem from '../ChatListItem/ChatListItem.js';
import { goToPage } from '../../utils/goToPage.js';
import ProfilePage from '../../Pages/ProfilePage.js';
import ContactsPage from '../../Pages/ContactsPage.js';
import { goToPage } from '../../utils/router.js';

/**
* Рендерит компоненты боковой панели: заголовок, поиск, список чатов, пользователь и выйти
Expand All @@ -20,12 +18,12 @@ export default class ChatList extends BaseComponent {
this.getParent()
.querySelector('#profile_btn')
.addEventListener('click', () => {
goToPage(ProfilePage);
goToPage('/profile');
});
this.getParent()
.querySelector('#contacts_btn')
.addEventListener('click', () => {
goToPage(ContactsPage);
goToPage('/contacts');
});
}

Expand Down
5 changes: 2 additions & 3 deletions src/Components/Contacts/Contacts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ChatPage from '../../Pages/ChatPage.js';
import { goToPage } from '../../utils/goToPage.js';
import { goToPage } from '../../utils/router.js';
import { BaseComponent } from '../BaseComponent.js';
import ContactItem from '../ContactItem/ContactItem.js';

Expand All @@ -17,7 +16,7 @@ export default class Contacts extends BaseComponent {
.querySelector('#backButton')
.addEventListener('click', () => {
// TODO: Возврат назад
goToPage(ChatPage);
goToPage('/chat');
});
}

Expand Down
11 changes: 4 additions & 7 deletions src/Components/Profile/Profile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { BaseComponent } from '../BaseComponent.js';
import { goToPage } from '../../utils/goToPage.js';
import ChatPage from '../../Pages/ChatPage.js';
import ProfileEditPage from '../../Pages/ProfileEditPage.js';
import ChangePasswordPage from '../../Pages/ChangePasswordPage.js';
import { goToPage } from '../../utils/router.js';
/**
* Рендерит профиль
* @class Класс компонента профиля
Expand All @@ -17,17 +14,17 @@ export default class Profile extends BaseComponent {
.querySelector('#backButton')
.addEventListener('click', () => {
// TODO: Возврат назад
goToPage(ChatPage);
goToPage('/chat');
});
this.getParent()
.querySelector('#editButton')
.addEventListener('click', () => {
goToPage(ProfileEditPage);
goToPage('/edit');
});
this.getParent()
.querySelector('#changePasswordButton')
.addEventListener('click', () => {
goToPage(ChangePasswordPage);
goToPage('/password');
});
}
}
12 changes: 5 additions & 7 deletions src/Pages/ChangePasswordPage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Form from '../Components/Form/Form.js';
import { goToPage } from '../utils/goToPage.js';
import { validatePassword } from '../utils/valid.js';
import ChatPage from './ChatPage.js';
import ProfilePage from './ProfilePage.js';
import { ProfileAPI } from '../utils/API/ProfileAPI.js';
import { goToPage } from '../utils/router.js';

/**
* Рендерит страницу изменения пароля
Expand All @@ -23,11 +21,11 @@ export default class ChangePasswordPage {
const error = event.target.querySelector('#error-message');
error.textContent = '';

if (oldPassword.length == 0) {
if (oldPassword.length === 0) {
error.textContent = 'Заполните поле Старый пароль';
return;
}
if (newPassword.length == 0) {
if (newPassword.length === 0) {
error.textContent = 'Заполните поле Новый пароль';
return;
}
Expand All @@ -43,7 +41,7 @@ export default class ChangePasswordPage {
.then((data) => {
if (data.status === 200) {
// Обработка успешной авторизации
goToPage(ChatPage);
goToPage('/chat');
} else {
error.textContent = data.body.error;
}
Expand All @@ -59,7 +57,7 @@ export default class ChangePasswordPage {
header: 'Изменение пароля',
onSubmit: this.formCallback,
onAdditionButtonClick: () => {
goToPage(ProfilePage);
goToPage('/profile');
},
inputs: [
{
Expand Down
28 changes: 18 additions & 10 deletions src/Pages/ChatPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChatAPI } from '../utils/API/ChatAPI.js';
import { AuthAPI } from '../utils/API/AuthAPI.js';
import { goToPage } from '../utils/goToPage.js';
import LoginPage from './LoginPage.js';
import { goToPage } from '../utils/router.js';
import Chat from '../Components/Chat/Chat.js';
import ChatList from '../Components/ChatList/ChatList.js';
import Message from '../Components/Message/Message.js';
Expand All @@ -18,8 +17,9 @@ export default class ChatPage {
#messageDrafts = {};
#currentChatId;

constructor(parent) {
constructor(parent, urlParams) {
this.#parent = parent;
this.#currentChatId = parseInt(urlParams.get('id'));
}

render() {
Expand All @@ -37,21 +37,29 @@ export default class ChatPage {
this.#chat.render();

this.#parent.appendChild(wrapper);
let checkChatId = false;

const chatAPI = new ChatAPI();
chatAPI
.getChats()
.then((chats) => {
chats.body.chats.forEach((chatConfig) => {
//
if (chatConfig.id === this.#currentChatId) {
checkChatId = true;
this.displayActiveChat(chatConfig);
}
this.#chatList.addChat(chatConfig, () => {
this.#currentChatId = chatConfig.id;
this.#chat.setInputMessageValue(
this.#messageDrafts[chatConfig.id] || '',
);
this.displayActiveChat(chatConfig);
goToPage('/chat?id=' + chatConfig.id);
});
});
if (!checkChatId && !isNaN(this.#currentChatId)) {
goToPage('/chat');
this.#currentChatId = null;
}
})
.catch((error) => {
console.error('Ошибка при получении чатов:', error);
Expand All @@ -60,11 +68,11 @@ export default class ChatPage {
const profileAPI = new ProfileAPI();
profileAPI
.getProfile()
.then((responce) => {
if (responce.status != 200) {
.then((response) => {
if (response.status !== 200) {
throw new Error('Пришел не 200 статус');
}
const profile = responce.body.user;
const profile = response.body.user;
this.#chatList.setUserName(
`${profile.name} ${profile.surname}`,
);
Expand Down Expand Up @@ -123,13 +131,13 @@ export default class ChatPage {
if (data.status === 200) {
// Обработка успешной авторизации
console.log('Successfully logged out');
goToPage(LoginPage);
goToPage('/login');
} else {
console.log('Error logged out');
}
})
.catch((error) => {
console.error('Login failed:', error);
console.error('Logout failed:', error);
});
}
}
16 changes: 8 additions & 8 deletions src/Pages/LoginPage.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { goToPage } from '../utils/goToPage.js';
import { AuthAPI } from '../utils/API/AuthAPI.js';
import RegisterPage from './RegisterPage.js';
import ChatPage from './ChatPage.js';
import { goToPage } from '../utils/router.js';
import Form from '../Components/Form/Form.js';
import { enableRedirect } from '../utils/API/common.js';

/**
* Рендерит страницу авторизации
* @class Класс страницы авторизации
*/
export default class LoginPage {
#parent;
#errorMessage;
// #errorMessage;
#signinForm;

constructor(parent) {
Expand All @@ -24,11 +23,11 @@ export default class LoginPage {
const error = event.target.querySelector('#error-message');
error.textContent = '';

if (username.length == 0) {
if (username.length === 0) {
error.textContent = 'Заполните поле Имя пользователя';
return;
}
if (password.length == 0) {
if (password.length === 0) {
error.textContent = 'Заполните поле Пароль';
return;
}
Expand All @@ -39,7 +38,8 @@ export default class LoginPage {
.then((data) => {
if (data.status === 200) {
// Обработка успешной авторизации
goToPage(ChatPage);
enableRedirect(true);
goToPage('/chat');
} else {
error.textContent = data.body.error;
}
Expand All @@ -55,7 +55,7 @@ export default class LoginPage {
header: 'Авторизация',
onSubmit: this.formCallback,
onAdditionButtonClick: () => {
goToPage(RegisterPage);
goToPage('/register');
},
inputs: [
{
Expand Down
11 changes: 5 additions & 6 deletions src/Pages/ProfileEditPage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Form from '../Components/Form/Form.js';
import { ProfileAPI } from '../utils/API/ProfileAPI.js';
import { goToPage } from '../utils/goToPage.js';
import ProfilePage from './ProfilePage.js';
import { validateUsername, validateEmail } from '../utils/valid.js';
import { goToPage } from '../utils/router.js';

/**
* Рендерит страницу редактирования профиля
Expand Down Expand Up @@ -35,7 +34,7 @@ export default class LoginPage {
.then((data) => {
if (data.status === 200) {
// Обработка успешной авторизации
goToPage(ProfilePage);
goToPage('/profile');
} else {
error.textContent = data.body.error;
}
Expand Down Expand Up @@ -69,7 +68,7 @@ export default class LoginPage {
}
}, 0);

if (editedFieldCnt == 0) {
if (editedFieldCnt === 0) {
return;
}
// Отправка данных на сервер
Expand All @@ -78,7 +77,7 @@ export default class LoginPage {
.then((data) => {
if (data.status === 200) {
// Обработка успешной авторизации
goToPage(ProfilePage);
goToPage('/profile');
} else {
error.textContent = data.body.error;
}
Expand All @@ -94,7 +93,7 @@ export default class LoginPage {
header: 'Редактировать профиль',
onSubmit: this.formCallback,
onAdditionButtonClick: () => {
goToPage(ProfilePage);
goToPage('/profile');
},
inputs: [
{
Expand Down
6 changes: 3 additions & 3 deletions src/Pages/ProfilePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default class ProfilePage {

profileAPI
.getProfile()
.then((responce) => {
if (responce.status != 200) {
.then((response) => {
if (response.status !== 200) {
throw new Error('Пришел не 200 статус');
}

const profileConfig = responce.body.user;
const profileConfig = response.body.user;
const profile = new Profile(this.#parent, profileConfig);
profile.render();
})
Expand Down
13 changes: 5 additions & 8 deletions src/Pages/RegisterPage.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { validatePassword, validateUsername } from '../utils/valid.js';
import { goToPage } from '../utils/goToPage.js';
import { goToPage } from '../utils/router.js';
import { AuthAPI } from '../utils/API/AuthAPI.js';
import LoginPage from './LoginPage.js';
import Form from '../Components/Form/Form.js';
import ChatPage from './ChatPage.js';

/**
* Рендерит страницу регистрации
* @class Класс страницы регистрации
*/
export default class RegisterPage {
#parent;
#errorMessage;
// #errorMessage;
#signupForm;

constructor(parent) {
Expand All @@ -28,7 +26,7 @@ export default class RegisterPage {
error.textContent = '';

// Валидация данных
if (password != confirmPassword) {
if (password !== confirmPassword) {
error.textContent = 'Пароли не совпадают';
return;
}
Expand All @@ -49,8 +47,7 @@ export default class RegisterPage {
.then((data) => {
if (data.status === 200) {
// Обработка успешной авторизации
console.log('Successfully logged in');
goToPage(ChatPage);
goToPage('/chat');
} else {
error.textContent = data.body.error;
}
Expand All @@ -66,7 +63,7 @@ export default class RegisterPage {
header: 'Регистрация',
onSubmit: this.formCallback,
onAdditionButtonClick: () => {
goToPage(LoginPage);
goToPage('/login');
},
inputs: [
{
Expand Down
Loading

0 comments on commit ac66ac3

Please sign in to comment.