Skip to content

Commit

Permalink
Merge pull request #22 from frontend-park-mail-ru/NM-11
Browse files Browse the repository at this point in the history
NM-17: fsd architecture (login/auth)
  • Loading branch information
MatiXxD authored Oct 19, 2024
2 parents 96ec51f + 1e19858 commit 97b3131
Show file tree
Hide file tree
Showing 39 changed files with 982 additions and 218 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
4 changes: 4 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<script src="./widgets/player/ui/player.precompiled.js"></script>
<script src="./widgets/trackList/ui/trackList.precompiled.js"></script>
<script src="./widgets/artistList/ui/artistList.precompiled.js"></script>
<script src="./widgets/header/ui/Header.precompiled.js"></script>
<script src="./pages/sign-up/ui/SignUp.precompiled.js"></script>
<script src="./pages/sign-in/ui/SignIn.precompiled.js"></script>
<script src="./pages/error/ui/Error.precompiled.js"></script>

<script type="module" src="index.js"></script>
</body>
Expand Down
32 changes: 17 additions & 15 deletions src/app/routes.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { FeedPage } from '../pages/feed/index.js';
// import { SignInPage, RegisterPage } from 'pages/sign-in';
import { Header } from "../widgets/header/index.js";
import { FeedPage } from "../pages/feed/index.js";
import { SignUpPage } from "../pages/sign-up/index.js";
import { SignInPage } from "../pages/sign-in/index.js";

export const LAYOUT = [];
export const LAYOUT = [Header];

/**
* Define pages and their views
*/
export const PAGES = [
{
path: '/',
view: FeedPage,
},
// {
// path: '/signin',
// view: SignInPage,
// },
// {
// path: '/register',
// view: RegisterPage,
// },
{
path: "/",
view: FeedPage,
},
{
path: "/signin",
view: SignInPage,
},
{
path: "/signup",
view: SignUpPage,
},
];
32 changes: 32 additions & 0 deletions src/entities/user/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { API_ENDPOINTS } from "../../../shared/lib/index.js";
import { POST } from "../../../shared/api/index.js";

/**
* Sends sign in request to the server using user data
*
* @param {Object} user - user data (username and password)
* @returns {Promise<Object>} response from the server
*/
export const signInRequest = async (user) => {
return await POST(API_ENDPOINTS.SIGN_IN, { body: user });
};

/**
* Sends sign up request to server using user data
*
* @param {Object} user - user data to register (email, username, password)
* @returns {Promise<Object>} response from the server
*/
export const signUpRequest = async (user) => {
return await POST(API_ENDPOINTS.SIGN_UP, { body: user });
};

/**
* Sends sign out request to server using user data
*
* @returns {Promise<Object>} response from the server
*/
export const signOutRequest = async () => {
return await POST(API_ENDPOINTS.SIGN_OUT);
};

1 change: 1 addition & 0 deletions src/entities/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { signInRequest, signUpRequest, signOutRequest } from './api/api.js';
123 changes: 123 additions & 0 deletions src/entities/user/model/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { STATUS_CODES } from "../../../shared/lib/index.js";
import { eventBus } from "../../../shared/lib/index.js";
import { signInRequest, signUpRequest, signOutRequest } from "../api/api.js";

class UserStore {
constructor() {
this.storage = {
user: this.getUser() || {},
error: null,
};
}

getUser() {
const user = localStorage.getItem('user');
return user ? JSON.parse(user) : null;
}

saveUser(user) {
localStorage.setItem('user', JSON.stringify(user));
}

removeUser() {
localStorage.removeItem('user');
}

signIn = async (user) => {
try {
const response = await signInRequest(user);

switch (response.status) {
case STATUS_CODES.OK:
const userData = {
username: response.data.user.username,
email: response.data.user.email,
token: response.data.token,
isAuthorized: true,
};
this.storage.user = userData;
this.saveUser(userData);
this.storage.error = null;
eventBus.emit('signInSuccess', this.storage.user);
break;

case STATUS_CODES.UNAUTHORIZED:
this.storage.user = {
isAuthorized: false,
};
this.storage.error = response.error;
eventBus.emit('signInError', this.storage.error);
break;

default:
console.error('undefined status code:', response.status);
}
} catch (error) {
this.storage.error = error;
eventBus.emit('signInError', error);
console.error('unable to sign in: ', error);
}
};


signUp = async (user) => {
try {
const response = await signUpRequest(user);

switch (response.status) {
case STATUS_CODES.OK:
const userData = {
username: response.data.user.username,
email: response.data.user.email,
token: response.data.token,
isAuthorized: true,
};
this.storage.user = userData;
this.saveUser(userData);
this.storage.error = null;
eventBus.emit('signUpSuccess', this.storage.user);
break;

case STATUS_CODES.UNAUTHORIZED:
this.storage.user = {
isAuthorized: false,
};
this.storage.error = response.error;
eventBus.emit('signUpError', this.storage.error);
break;

default:
console.error('undefined status code:', response.status);
}
} catch (error) {
this.storage.error = error;
eventBus.emit('signUpError', error);
console.error('unable to sign up: ', error);
}
};


signOut = async () => {
try {
const response = await signOutRequest();

switch (response.status) {
case STATUS_CODES.OK:
this.storage.user = { isAuthorized: false };
this.removeUser();
this.storage.error = null;
eventBus.emit('signOutSuccess');
break;

default:
console.error('undefined status code:', response.status);
}
} catch (error) {
this.storage.error = error;
eventBus.emit('signOutError', error);
console.error('unable to connect to server: ', error);
}
};
};

export const userStore = new UserStore();
12 changes: 6 additions & 6 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
@import './widgets/player/ui/player.css';
@import './widgets/trackList/ui/trackList.css';
@import './widgets/artistList/ui/artistList.css';

body {
font-family: 'Sansation', sans-serif;
margin: 0;
padding: 0;
}
@import './widgets/header/ui/Header.css';
@import './pages/sign-up/ui/SignUp.css';
@import './pages/sign-in/ui/SignIn.css';

h1,
h2,
Expand Down Expand Up @@ -50,6 +47,9 @@ body {
rgba(168, 72, 10, 1) 0%,
rgba(44, 25, 17, 1) 100%);
background-attachment: fixed;
font-family: 'Sansation', sans-serif;
margin: 0;
padding: 0;
}

.message-success {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/error/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ErrorPage } from './ui/ErrorPage.js';
export { ErrorPage } from './ui/Error.js';
Empty file added src/pages/error/ui/Error.css
Empty file.
4 changes: 4 additions & 0 deletions src/pages/error/ui/Error.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="error-container">
<div class="error-title">Ошибка</div>
<div class="error-message">{{message}}</div>
</div>
12 changes: 12 additions & 0 deletions src/pages/error/ui/Error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { eventBus } from "../../../shared/lib/eventbus.js";

export class ErrorPage {
constructor() {
this.root = document.querySelector("#root");
}

render(message = "Что-то пошло не так") {
const template = Handlebars.templates["error.hbs"];
this.root.innerHTML = template({ message });
}
}
5 changes: 0 additions & 5 deletions src/pages/error/ui/ErrorPage.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/pages/sign-in/api/register.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/pages/sign-in/api/sign-in.js

This file was deleted.

6 changes: 2 additions & 4 deletions src/pages/sign-in/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export { RegisterPage } from "./ui/RegisterPage";
export { register } from "./api/register";
export { SignInPage } from "./ui/SignInPage";
export { signIn } from "./api/sign-in";
export { SignInPage } from "./ui/SignIn.js";

8 changes: 0 additions & 8 deletions src/pages/sign-in/ui/RegisterPage.js

This file was deleted.

Loading

0 comments on commit 97b3131

Please sign in to comment.