Skip to content

Commit

Permalink
Fix profile url
Browse files Browse the repository at this point in the history
  • Loading branch information
FireSpirit171 committed Oct 31, 2024
1 parent 079efd4 commit e984fba
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
16 changes: 8 additions & 8 deletions components/ProfileData/ProfileData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ class ProfileData {
if (response.ok) {
const data = await response.json();
this.#profileData.name = data.name;
this.#profileData.username = data.Username;
this.#profileData.email = data.Email;
this.#profileData.isHost = data.IsHost;
this.#profileData.avatar = data.Avatar;
this.#profileData.birthdate = data.Birthdate.slice(0, 10);
this.#profileData.username = data.username;
this.#profileData.email = data.email;
this.#profileData.isHost = data.isHost;
this.#profileData.avatar = data.avatar;
this.#profileData.birthdate = data.birthDate.slice(0, 10);
if (this.#profileData.birthdate != '0001-01-01')
this.#showBirthdate = true;
this.#profileData.address = data.Address;
this.#profileData.sex = this.#calculateSex(data.Sex);
this.#rememberSexValue(data.Sex);
this.#profileData.address = data.address;
this.#profileData.sex = this.#calculateSex(data.sex);
this.#rememberSexValue(data.sex);
} else if (response.status !== 401) {
console.error('Wrong response from server', response);
}
Expand Down
18 changes: 9 additions & 9 deletions components/ProfileInfo/ProfileInfo.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div id="profile" class="profile-container">
<div class="profile-container__center">
<div class="profile-container__photo">
<img src="{{avatar}}" class="profile-container__photo__img1">
<img src="{{data.avatar}}" class="profile-container__photo__img1">
</div>
<p class="profile-container__p1">{{name}}</p>
<p class="profile-container__p2">{{city}}</p>
<p class="profile-container__p1">{{data.name}}</p>
<p class="profile-container__p2">{{data.city}}</p>
<hr class="profile-container__hr">
</div>

Expand All @@ -13,29 +13,29 @@
<div class="profile-container__info">
<span class="profile-container__score-container">
<img class="profile-container__img2" src="../../images/star.png" alt="star">
<span class="profile-container__score">{{score}}</span>
<span class="profile-container__score">{{data.score}}</span>
</span>
</div>
</div>
<div class="profile-container__div">
<div class="profile-container__p">Пол:</div>
<p class="profile-container__info">{{sex}}</p>
<p class="profile-container__info">{{data.sex}}</p>
</div>
<div class="profile-container__div">
<div class="profile-container__p">Возраст:</div>
<p class="profile-container__info">{{age}}</p>
<p class="profile-container__info">{{#if isCorrectAge}} {{age}} {{else}}Не указано{{/if}}</p>
</div>
<div class="profile-container__div">
<div class="profile-container__p">Хост:</div>
<p class="profile-container__info">{{#if isHost}}Да{{else}}Нет{{/if}}</p>
<p class="profile-container__info">{{#if data.isHost}}Да{{else}}Нет{{/if}}</p>
</div>
<div class="profile-container__div">
<div class="profile-container__p">Адрес:</div>
<p class="profile-container__info">{{address}}</p>
<p class="profile-container__info">{{data.address}}</p>
</div>
<div class="profile-container__div">
<div class="profile-container__p">Серферы:</div>
<p class="profile-container__info">{{guestCount}}</p>
<p class="profile-container__info">{{data.guestCount}}</p>
</div>

<div class="profile-container__edit-container">
Expand Down
8 changes: 5 additions & 3 deletions components/ProfileInfo/ProfileInfo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

class ProfileInfo {
#data;
#data: object;
#showAge: boolean;

constructor(data: object){
constructor(data: object, showAge: boolean){
this.#data = data;
this.#showAge = showAge;
}

/**
Expand All @@ -13,7 +15,7 @@ class ProfileInfo {
*/
render(parent: HTMLElement){
const template = Handlebars.templates['ProfileInfo.hbs'];
parent.insertAdjacentHTML('afterbegin', template(this.#data));
parent.insertAdjacentHTML('afterbegin', template({data: this.#data, isCorrectAge: this.#showAge}));
}
}

Expand Down
38 changes: 25 additions & 13 deletions components/ProfilePage/ProfilePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ProfilePage{
#isHost: boolean | undefined;
#age: number | undefined;
#avatar: string | undefined;
#showAge: boolean;

#renderProfileInfoCallback;

Expand All @@ -28,6 +29,7 @@ class ProfilePage{
await this.#renderProfileInfo(profileInfoContainer);
}
};
this.#showAge = true;
}

/**
Expand All @@ -40,18 +42,18 @@ class ProfilePage{
const data = await response.json();

this.#name = data.name;
this.#username = data.Username;
this.#email = data.Email;
this.#guestCount = data.GuestCount;
this.#score = data.Score;
this.#isHost = data.IsHost;
this.#avatar = data.Avatar;
this.#birthdate = data.Birthdate;

this.#sex = this.#calculateSex(data.Sex);
this.#age = this.#calculateAge(data.Birthdate);

const splitedAddress = this.#splitAddress(data.Address);
this.#username = data.username;
this.#email = data.email;
this.#guestCount = data.guestCount;
this.#score = data.score;
this.#isHost = data.isHost;
this.#avatar = data.avatar;
this.#birthdate = data.birthDate;

this.#sex = this.#calculateSex(data.sex);
this.#age = this.#calculateAge(data.birthDate);

const splitedAddress = this.#splitAddress(data.address);
this.#city = splitedAddress.city;
this.#address = splitedAddress.address;
} else if (response.status !== 401) {
Expand All @@ -65,6 +67,10 @@ class ProfilePage{
* @returns {number}
*/
#calculateAge(birthdate: string): number {
if (birthdate.slice(0,10) === '0001-01-01') {
return -1;
}

const birthDate = new Date(birthdate);
const today = new Date();

Expand Down Expand Up @@ -119,6 +125,11 @@ class ProfilePage{
*/
async #renderProfileInfo(parent: HTMLElement) {
await this.#getProfileData();
if (this.#age == -1) {
this.#showAge = false;
} else {
this.#showAge = true;
}
const profileData = {
name: this.#name,
username: this.#username,
Expand All @@ -134,7 +145,7 @@ class ProfilePage{
avatar: this.#avatar,
};

const profileInfo = new ProfileInfo(profileData);
const profileInfo = new ProfileInfo(profileData, this.#showAge);
profileInfo.render(parent);
}

Expand All @@ -154,6 +165,7 @@ class ProfilePage{
* @param {HTMLElement} parent
*/
async render(parent: HTMLElement) {
parent.replaceChildren();
const profileContent = document.createElement('div');
profileContent.id = 'profile-content';

Expand Down
8 changes: 8 additions & 0 deletions modules/Ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class Ajax {
return this.#makeRequest({ method: 'POST', url, body });
}

/**
* @public
* @param {PostParams} postParams
*/
static put({ url, body }: PostParams) {
return this.#makeRequest({ method: 'PUT', url, body });
}

/**
* @public
* @param {string} url
Expand Down
7 changes: 5 additions & 2 deletions modules/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import Ajax from './Ajax';
import { RegisterParams, AdsFilters, LoginParams, EditParams } from './Types';
import { getCookie } from './Utils';

class APIClient {
BASE_URL = `http://${window.location.hostname}:8008/api`;
Expand Down Expand Up @@ -112,7 +113,8 @@ class APIClient {
}

async profile() {
const url = this.BASE_URL + '/getUserById';
const uuid = getCookie('session_id');
const url = this.BASE_URL + `/users/${uuid}`;
return Ajax.get(url);
}

Expand All @@ -127,7 +129,8 @@ class APIClient {
password,
avatar,
}: EditParams) {
const url = this.BASE_URL + '/putUser';
const uuid = getCookie('session_id');
const url = this.BASE_URL + `/users/${uuid}`;
const formData = new FormData();

const metadata = {
Expand Down

0 comments on commit e984fba

Please sign in to comment.