Skip to content

Commit

Permalink
Merge pull request #94 from frontend-park-mail-ru/NM-93
Browse files Browse the repository at this point in the history
NM-93: fix csat error
  • Loading branch information
daronenko authored Dec 15, 2024
2 parents 898e850 + 9f924c5 commit 785d1ee
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 195 deletions.
58 changes: 27 additions & 31 deletions src/app/app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import { Router } from '../shared/lib/router.js';
import { LAYOUT, PAGES } from './routes.js';
import { userStore } from '../entities/user/index.js';
import { Router } from "../shared/lib/router.js";
import { LAYOUT, PAGES } from "./routes.js";

export class App {
constructor() {
this.router = new Router();
this.registerRoutes();
this.registerLayout();
}
constructor() {
this.router = new Router();
this.registerRoutes();
this.registerLayout();
}

/**
* Starts the application by making the router listen for path changes.
*/
async run() {
if (userStore.isAuth() && !userStore.csrfToken) {
await userStore.getCSRFToken();
}
this.router.listen();
}
/**
* Starts the application by making the router listen for path changes.
*/
async run() {
this.router.listen();
}

/**
* Registers all the defined pages in the application with the router.
*/
registerRoutes() {
PAGES.forEach(({ path, view, updateLayout }) => {
this.router.registerPath(path, view, updateLayout);
});
}
/**
* Registers the application's layout views with the router.
*/
registerLayout() {
LAYOUT.forEach((view) => this.router.registerLayout(view));
}
/**
* Registers all the defined pages in the application with the router.
*/
registerRoutes() {
PAGES.forEach(({ path, view, updateLayout }) => {
this.router.registerPath(path, view, updateLayout);
});
}
/**
* Registers the application's layout views with the router.
*/
registerLayout() {
LAYOUT.forEach((view) => this.router.registerLayout(view));
}
}
1 change: 1 addition & 0 deletions src/entities/csat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { csatStore } from './model/store.js';
22 changes: 22 additions & 0 deletions src/entities/csat/model/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Storage } from '../../../shared/lib/storage.js';

class CSATStore {
constructor() {
this.storage = {
csat: Storage.load('csat') || {},
};
}

submitted = () => {
return this.storage.csat?.submitted;
};

submit = () => {
this.storage.csat = {
submitted: true,
};
Storage.save('csat', this.storage.csat);
};
}

export const csatStore = new CSATStore();
5 changes: 5 additions & 0 deletions src/entities/user/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ export const signOutRequest = async () => {
export const getCSRFTokenRequest = async () => {
return await GET(API_ENDPOINTS.GET_CSRF_TOKEN);
};

export const checkAuthRequest = async () => {
const url = API_ENDPOINTS.IS_AUTH;
return await GET(url);
};
42 changes: 40 additions & 2 deletions src/entities/user/model/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
signUpRequest,
signOutRequest,
getCSRFTokenRequest,
checkAuthRequest,
} from '../api/auth.js';
import {
getUserRequest,
Expand All @@ -26,9 +27,44 @@ class UserStore {
this.csrfToken = null;
}

isAuth() {
isAuth = () => {
return this.storage.user?.isAuthorized;
}
};

checkAuth = async () => {
try {
const response = await checkAuthRequest();

switch (response.status) {
case HTTP_STATUS.OK:
this.storage.error = null;

await this.getCSRFToken();

eventBus.emit('checkAuthSuccess');
break;

case HTTP_STATUS.UNAUTHORIZED:
this.storage.user = {
isAuthorized: false,
};
Storage.save('user', this.storage.user);
this.storage.error = PUBLIC_ERRORS.UNAUTHORIZED;
eventBus.emit('navigate', '/signin');
break;

default:
this.storage.error = PUBLIC_ERRORS.UNKNOWN;
eventBus.emit('checkAuthError', this.storage.error);
console.error('undefined status code:', response.status);
break;
}
} catch (error) {
this.storage.error = PUBLIC_ERRORS.UNKNOWN;
eventBus.emit('checkAuthError', this.storage.error);
console.error('unable to check authorization: ', error);
}
};

signIn = async (user) => {
try {
Expand Down Expand Up @@ -57,6 +93,7 @@ class UserStore {
this.storage.user = {
isAuthorized: false,
};
Storage.save('user', this.storage.user);
this.storage.error = PUBLIC_ERRORS.INVALID_USERNAME_OR_PASSWORD;
eventBus.emit('signInError', this.storage.error);
break;
Expand Down Expand Up @@ -100,6 +137,7 @@ class UserStore {
this.storage.user = {
isAuthorized: false,
};
Storage.save('user', this.storage.user);
this.storage.error = PUBLIC_ERRORS.USER_EXISTS;
eventBus.emit('signUpError', this.storage.error);
break;
Expand Down
30 changes: 15 additions & 15 deletions src/pages/edit-profile/ui/editProfile.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { UploadAvatarView } from '../../../widgets/uploadAvatar/index.js';
import { EditUserView } from '../../../widgets/editUser/index.js';
import { userStore } from '../../../entities/user/index.js';
import { PUBLIC_ERRORS } from '../../../shared/lib/index.js';
import { eventBus } from '../../../shared/lib/index.js';
import { UploadAvatarView } from "../../../widgets/uploadAvatar/index.js";
import { EditUserView } from "../../../widgets/editUser/index.js";
import { userStore } from "../../../entities/user/index.js";
import { PUBLIC_ERRORS } from "../../../shared/lib/index.js";
import { eventBus } from "../../../shared/lib/index.js";

export class EditProfilePage {
parent;
username;

constructor(params) {
this.parent = document.querySelector('#root');
this.username = params['username'];
this.parent = document.querySelector("#root");
this.username = params["username"];
}

async render() {
this.parent.innerHTML = '';
this.parent.innerHTML = "";

await userStore.checkAuth();
if (!userStore.isAuth()) {
console.error(PUBLIC_ERRORS.UNAUTHORIZED);
eventBus.emit('navigate', '/');
return;
}

const user = userStore.storage.user;
const user = userStore.storage.user;

if (user.username != this.username) {
console.error(PUBLIC_ERRORS.FORBIDDEN);
eventBus.emit('navigate', '/');
eventBus.emit("navigate", "/");
return;
}

this.pageContent = document.createElement('div');
this.pageContent.classList.add('page_content');
this.parent.appendChild(this.pageContent);
this.pageContent = document.createElement("div");
this.pageContent.classList.add("page_content");
this.parent.appendChild(this.pageContent);

const uploadAvatarView = new UploadAvatarView(this.pageContent, user.id);
await uploadAvatarView.render();

const editUserView = new EditUserView(this.pageContent, user.id);
await editUserView.render();

eventBus.emit('hidePlayer');
eventBus.emit("hidePlayer");
}
}
9 changes: 6 additions & 3 deletions src/pages/feed/ui/Feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { player } from '../../../shared/player/model/store.js';
import { CSATWindow } from '../../../widgets/csatWindow/ui/csatWindow.js';
import { userStore } from '../../../entities/user/index.js';
import { eventBus } from '../../../shared/lib/eventbus.js';
import { csatStore } from '../../../entities/csat/index.js';

export class FeedPage {
/**
Expand All @@ -21,15 +22,17 @@ export class FeedPage {
}

async render() {
await userStore.checkAuth();
if (!userStore.isAuth()) {
eventBus.emit('navigate', '/signin');
return;
}

this.parent.innerHTML = '';

const iframe = new CSATWindow();
await iframe.render();
if (!csatStore.submitted()) {
const iframe = new CSATWindow();
await iframe.render();
}

this.pageContent = document.createElement('div');
this.pageContent.classList.add('page_content');
Expand Down
Loading

0 comments on commit 785d1ee

Please sign in to comment.