Skip to content

Commit

Permalink
Fix modal close error, clear input after send message, fix changes av…
Browse files Browse the repository at this point in the history
…atar error, added BASE_URL const etc...
  • Loading branch information
Evgeny Talagaev committed Oct 18, 2023
1 parent d62cfff commit c902ad2
Show file tree
Hide file tree
Showing 24 changed files with 208 additions and 167 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@typescript-eslint/parser": "^6.7.0",
"concurrently": "^8.2.1",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"eslint": "^8.49.0",
"eslint-config-airbnb": "^19.0.4",
"express": "^4.18.2",
Expand Down
3 changes: 1 addition & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ app.use(cors());

app.use(express.static('build'));

app.get('*', (req, res) => {
console.log(req);
app.get('*', (_req, res) => {
res.sendFile(path.join(__dirname, 'build/index.html'))
})

Expand Down
1 change: 1 addition & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_BASE_URL=https://ya-praktikum.tech/api/v2/
12 changes: 5 additions & 7 deletions src/api/auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@ type SigninData = {
password: string,
}

const authAPIInstance = new HTTP();
const authAPIInstance = new HTTP(`${import.meta.env.VITE_BASE_URL}auth/`);

class AuthAPI extends BaseAPI {
private _baseURL = 'https://ya-praktikum.tech/api/v2/auth/';

signup(data: SignupData) {
return authAPIInstance.post(`${this._baseURL}signup`, { data })
return authAPIInstance.post(`signup`, { data })
}

signin(data: SigninData) {
return authAPIInstance.post(`${this._baseURL}signin`, { data })
return authAPIInstance.post(`signin`, { data })
}

logout() {
return authAPIInstance.post(`${this._baseURL}logout`)
return authAPIInstance.post(`logout`)
}

getUserInfo() {
return authAPIInstance.get(`${this._baseURL}user`)
return authAPIInstance.get(`user`)
}
}

Expand Down
30 changes: 14 additions & 16 deletions src/api/chats-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,59 @@ export type UsersData = {
users: number[],
}

const chatsAPIInstance = new HTTP();
const chatsAPIInstance = new HTTP(`${import.meta.env.VITE_BASE_URL}chats/`);

class ChatsAPI extends BaseAPI {
private _baseURL = 'https://ya-praktikum.tech/api/v2/chats/';

request() {
return chatsAPIInstance.get(`${this._baseURL}`);
return chatsAPIInstance.get();
}

create(data: CreateData) {
return chatsAPIInstance.post(`${this._baseURL}`, { data });
return chatsAPIInstance.post('', { data });
}

delete(data: IdData) {
return chatsAPIInstance.delete(`${this._baseURL}`, { data });
return chatsAPIInstance.delete('', { data });
}

getFiles(id: number) {
return chatsAPIInstance.get(`${this._baseURL}${id}/files`);
return chatsAPIInstance.get(`${id}/files`);
}

getArchive(parameters: { [key: string]: any }) {
return chatsAPIInstance.get(`${this._baseURL}archive${queryStringify(parameters)}`);
return chatsAPIInstance.get(`archive${queryStringify(parameters)}`);
}

archive(data: IdData) {
return chatsAPIInstance.post(`${this._baseURL}archive`, { data });
return chatsAPIInstance.post(`archive`, { data });
}

unarchive(data: IdData) {
return chatsAPIInstance.post(`${this._baseURL}unarchive`, { data });
return chatsAPIInstance.post(`unarchive`, { data });
}

getUsers(id: number, parameters = {}) {
return chatsAPIInstance.get(`${this._baseURL}${id}/users${queryStringify(parameters)}`);
return chatsAPIInstance.get(`${id}/users${queryStringify(parameters)}`);
}

getNewMessagesCount(id: number) {
return chatsAPIInstance.get(`${this._baseURL}new/${id}`);
return chatsAPIInstance.get(`new/${id}`);
}

uploadAvatar(data: FormData) {
return chatsAPIInstance.put(`${this._baseURL}avatar`, { data });
return chatsAPIInstance.put(`avatar`, { data });
}

addUsers(data: UsersData) {
return chatsAPIInstance.put(`${this._baseURL}users/`, { data });
return chatsAPIInstance.put(`users/`, { data });
}

deleteUsers(data: UsersData) {
return chatsAPIInstance.delete(`${this._baseURL}users/`, { data });
return chatsAPIInstance.delete(`users/`, { data });
}

getToken(id: number) {
return chatsAPIInstance.post(`${this._baseURL}token/${id}`);
return chatsAPIInstance.post(`token/${id}`);
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/api/users-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,27 @@ export type SearchData = {
login: string,
}

const usersAPIInstance = new HTTP();
const usersAPIInstance = new HTTP(`${import.meta.env.VITE_BASE_URL}user/`);

class UsersAPI extends BaseAPI {
private _baseURL = 'https://ya-praktikum.tech/api/v2/user/';

request(id: number) {
return usersAPIInstance.get(`${this._baseURL}${id}`);
return usersAPIInstance.get(`${id}`);
}

changeProfile(data: UserData) {
return usersAPIInstance.put(`${this._baseURL}profile`, { data });
return usersAPIInstance.put(`profile`, { data });
}

changeAvatar(data: FormData) {
const headers = new Map([['Content-Type', 'multipart/form-data']]);

return usersAPIInstance.put(`${this._baseURL}profile/avatar`, { data, headers });
return usersAPIInstance.put(`profile/avatar`, { data });
}

changePassword(data: PasswordData) {
return usersAPIInstance.put(`${this._baseURL}password`, { data });
return usersAPIInstance.put(`password`, { data });
}

search(data: SearchData) {
return usersAPIInstance.post(`${this._baseURL}search`, { data });
return usersAPIInstance.post(`search`, { data });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Avatar/Avatar.tmp.pug
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.avatar
img.avatar__img(src=src ? `https://ya-praktikum.tech/api/v2/resources${src}` : 'img/avatar-dummy-1.jpg', alt=alt, loading="lazy")
img.avatar__img(src=src ? `${baseURL}resources${src}` : 'img/avatar-dummy-1.jpg', alt=alt, loading="lazy")
2 changes: 1 addition & 1 deletion src/components/Avatar/Avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "./Avatar.scss";

export default class Avatar extends Block {
constructor(props?: object) {
super('div', props);
super('div', { ...props, baseURL: import.meta.env.VITE_BASE_URL });
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Message/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default class Message extends Block {
}

render() {
const { login, active_user_login } = this.props;
const { user_id, active_user_id } = this.props;

return this.compile(login === active_user_login ? templateInner : templateOuter, this.props);
return this.compile(user_id === active_user_id ? templateInner : templateOuter, this.props);
}
}
2 changes: 1 addition & 1 deletion src/components/Modal/Modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class Modal extends Block {
}

closeModal(event: any) {
const target = event;
const { target } = event;
if (
target.getAttribute('data-modal') === 'close' ||
!target.closest('.d-modal__content, [data-modal="open"]')
Expand Down
24 changes: 11 additions & 13 deletions src/controllers/AuthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import type { SignupData, SigninData } from "../api/auth-api";
class AuthController {
public signup(data: SignupData) {
AuthAPI.signup(data)
.then((xhr) => {
if (xhr.status === 200) {
.then((res) => {
if (res.status === 200) {
sessionStorage.setItem("inSystem", "true");
router.go('/messenger');

return;
}

const { reason } = JSON.parse(xhr.responseText);
Store.set('error', reason);
Store.set('error', res.response);
})
.catch((error) => {
console.error(`${error}`);
Expand All @@ -24,22 +23,22 @@ class AuthController {

public signin(data: SigninData) {
AuthAPI.signin(data)
.then((xhr) => {
if (xhr.status === 200) {
.then((res) => {
if (res.status === 200) {
sessionStorage.setItem("inSystem", "true");
router.go('/messenger');

return;
}

const { reason } = JSON.parse(xhr.responseText);
if (reason === 'User already in system') {
if (res.response === 'User already in system') {
sessionStorage.setItem("inSystem", "true");
router.go('/messenger');

return;
}
Store.set('error', reason);

Store.set('error', res.response);
})
.catch((error) => {
console.error(`${error}`);
Expand All @@ -59,10 +58,9 @@ class AuthController {

public getUserInfo() {
AuthAPI.getUserInfo()
.then((xhr) => {
if (xhr.status === 200) {
const userData = JSON.parse(xhr.responseText);
Store.set('user', userData);
.then((res) => {
if (res.status === 200) {
Store.set('user', res.response);
}
})
.catch((error) => {
Expand Down
Loading

0 comments on commit c902ad2

Please sign in to comment.