Skip to content

Commit

Permalink
feat: add redirect to main page on login
Browse files Browse the repository at this point in the history
  • Loading branch information
Kleostro committed May 2, 2024
1 parent 9846749 commit cfc8632
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/App/model/AppModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AppModel {
private initPages(): Map<string, PageInterface> {
const root = this.getHTML();
const loginPage = new LoginPageModel(root, this.router);
const mainPage = new MainPageModel(root);
const mainPage = new MainPageModel(root, this.router);
const registrationPage = new RegistrationPageModel(root, this.router);
const notFoundPage = new NotFoundPageModel(root);
const pages: Map<string, PageInterface> = new Map(
Expand Down
1 change: 1 addition & 0 deletions src/pages/LoginPage/view/loginPageView.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.authWrapper {
margin: 0 auto;
border-bottom: 10px solid var(--steam-green-800);
border-radius: var(--medium-br);
padding: calc(var(--extra-large-offset) / 2) var(--extra-large-offset);
max-width: 500px;
background-color: var(--white);
Expand Down
15 changes: 14 additions & 1 deletion src/pages/MainPage/model/MainPageModel.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import type RouterModel from '@/app/Router/model/RouterModel.ts';
import type { PageInterface } from '@/shared/types/interfaces.ts';

import EventMediatorModel from '@/shared/EventMediator/model/EventMediatorModel.ts';
import observeStore, { selectCurrentUser } from '@/shared/Store/observer.ts';
import { MEDIATOR_EVENTS, PAGES_IDS } from '@/shared/constants/enums.ts';

import MainPageView from '../view/MainPageView.ts';

class MainPageModel implements PageInterface {
private eventMediator = EventMediatorModel.getInstance();

private router: RouterModel;

private view: MainPageView;

constructor(parent: HTMLDivElement) {
constructor(parent: HTMLDivElement, router: RouterModel) {
this.router = router;
this.view = new MainPageView(parent);
this.init();
}

private init(): void {
this.subscribeToEventMediator();
this.subscribeToStore();
}

private subscribeToEventMediator(): void {
this.eventMediator.subscribe(MEDIATOR_EVENTS.CHANGE_PAGE, (route) => this.switchPageVisibility(route));
}

private subscribeToStore(): boolean {
observeStore(selectCurrentUser, () => {
this.router.navigateTo(PAGES_IDS.MAIN_PAGE);
});
return true;
}

private switchPageVisibility(route: unknown): boolean {
if (route === PAGES_IDS.MAIN_PAGE || route === PAGES_IDS.DEFAULT_PAGE) {
this.view.show();
Expand Down
20 changes: 10 additions & 10 deletions src/shared/Store/observer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { Action, State } from './reducer.ts';
import type { ReduxStore } from './types';
import type { UserInterface } from '../types/interfaces.ts';
import type { State } from './reducer.ts';

function observeStore<T>(
store: ReduxStore<State, Action>,
select: (state: State) => T,
onChange: (selectedState: T) => void,
): VoidFunction {
let currentState = select(store.getState());
import getStore from './Store.ts';

function observeStore<T>(select: (state: State) => T, onChange: (selectedState: T) => void): VoidFunction {
let currentState = select(getStore().getState());

function handleChange(): void {
const nextState = select(store.getState());
const nextState = select(getStore().getState());
if (nextState !== currentState) {
currentState = nextState;
onChange(currentState);
}
}

const unsubscribe = store.subscribe(handleChange);
const unsubscribe = getStore().subscribe(handleChange);
return unsubscribe;
}

export const selectCurrentUser = (state: State): UserInterface | null => state.currentUser;

export default observeStore;
14 changes: 14 additions & 0 deletions src/shared/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,17 @@ export const COUNTRIES: Record<string, string> = {
Zambia: 'ZM',
Zimbabwe: 'ZW',
} as const;

const ERROR_MESSAGE_ANIMATE_PARAMS = [
{ transform: 'translateX(110%)' },
{ transform: 'translateX(-10%)' },
{ transform: 'translateX(-10%)' },
{ opacity: 1, transform: 'translateX(-10%)' },
{ opacity: 0, transform: 'translate(-10%, -110%)' },
];

export const ERROR_MESSAGE_ANIMATE_DETAILS = {
duration: 5500,
easing: 'cubic-bezier(0, 0.2, 0.58, 0.7)',
params: ERROR_MESSAGE_ANIMATE_PARAMS,
};

0 comments on commit cfc8632

Please sign in to comment.