Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
46ba625
feat: add class router
Sviatl Aug 13, 2023
b064b63
Merge branches 'router' and 'login-registration-main' of https://gith…
Sviatl Aug 14, 2023
2ac4ae6
feat: merged with login-registration-main
Sviatl Aug 15, 2023
6ff4b87
feat: merged with login-registration-main
Sviatl Aug 15, 2023
390b70f
feat: routing login/signup/main
Sviatl Aug 15, 2023
4964013
refactor: change type of routes: Record<string, string>
Sviatl Aug 15, 2023
861cdf3
refactor: change AS to instanceof
Sviatl Aug 15, 2023
c49f3b7
refactor: use foreach nstead of let i = 0...
Sviatl Aug 15, 2023
698abff
feat: add laizy loading for routing views
Sviatl Aug 15, 2023
0b0ec5e
feat: change main view for routing
Sviatl Aug 15, 2023
ff89f83
Merge branch 'login-registration-main' of https://github.com/akrutsko…
Sviatl Aug 15, 2023
f2836b1
feat: add 404 to main view and router
Sviatl Aug 15, 2023
b488c67
feat: add 404 to main view and router
Sviatl Aug 15, 2023
0549709
refactor: remove unuser async
Sviatl Aug 15, 2023
ffe1075
refactor: remove unuser async
Sviatl Aug 15, 2023
cbd487d
refactor: remove unuser async
Sviatl Aug 15, 2023
8bd5c51
refactor: saparate list of routers
Sviatl Aug 15, 2023
d387a05
feat: add hashValue in router
Sviatl Aug 15, 2023
37a0bf3
feat: add hashValue in router
Sviatl Aug 15, 2023
3ce0d2f
fix: sintax link ice-adventures
Sviatl Aug 15, 2023
5642dfc
refactor: delete unused navigateToLogin
Sviatl Aug 15, 2023
0f0aa73
refactor: change click event on mouseover in header categories
Sviatl Aug 15, 2023
e5f7072
refactor: change click event on mouseover in header categories
Sviatl Aug 15, 2023
862d908
Merge branch 'login-registration-main' into router
Sviatl Aug 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Consumer } from './components/consumer/consumer';
import { Header } from './components/header/header';
import { Footer } from './components/footer/footer';
import { Main } from './components/main/main';
import { Registration } from './components/registration/registration';
import { Router } from './router/router';

export default class App {
header: Header;
Expand All @@ -13,17 +13,22 @@ export default class App {

consumer: Consumer;

router: Router;

constructor() {
this.router = new Router();
this.consumer = new Consumer();
this.header = new Header(this.consumer);
this.main = new Main();
this.footer = new Footer();

this.router.subscribe(this.main);
this.router.handleLocation();
}

init(): void {
document.body.append(this.header.getElement());
document.body.append(this.main.getElement());
this.main.getElement().append(new Registration().getElement());
document.body.append(this.main.getView());
document.body.append(this.footer.getElement());

this.consumer.subscribe(this.header);
Expand Down
8 changes: 4 additions & 4 deletions src/app/components/absent/absent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export class Absent {
constructor() {
this.notFoundView = new ElementCreator({ tag: 'div', classes: 'bg-[#F1EFEF] rounded-xl w-full flex-1 p-5 md:p-10 relative' });
this.homeButton = new ElementAnchorCreator({ href: '/', classes: 'primary-button', text: 'Home' }).getElement();
this.backButton = new ElementButtonCreator({ classes: 'secondary-button', text: 'Go back' })
.setHandler('click', window.history.back)
.getElement();

this.backButton = new ElementButtonCreator({ classes: 'secondary-button', text: 'Go back' }).getElement();
this.backButton.addEventListener('click', () => {
window.history.back();
});
this.createView();
}

Expand Down
35 changes: 25 additions & 10 deletions src/app/components/header/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class Header implements Observer {
.getElement();

this.createView();
this.handleButtons();
}

createView(): void {
Expand All @@ -43,7 +42,7 @@ export class Header implements Observer {
burger.appendNode(spanBurger1, spanBurger2, spanBurger3);

const nav = new ElementCreator({ tag: 'nav', classes: 'w-full flex items-center justify-between py-5 gap-8' });
const logo = new ElementAnchorCreator({ href: '#', html: logotype });
const logo = new ElementAnchorCreator({ href: '/', html: logotype });
const mobileMenu = new ElementCreator({
tag: 'div',
classes: 'mobile-menu md:w-full md:max-w-full max-w-[390px] hidden justify-between md:flex gap-8',
Expand All @@ -52,28 +51,40 @@ export class Header implements Observer {
nav.appendNode(logo, mobileMenu, burger, bg);

const liHome = new ElementCreator({ tag: 'li' });
const aHome = new ElementAnchorCreator({ href: '#', text: 'Home', classes: 'h4 hover:text-primary-color' });
const aHome = new ElementAnchorCreator({ href: '/', text: 'Home', classes: 'h4 hover:text-primary-color' });
liHome.appendNode(aHome);

const liAboutUs = new ElementCreator({ tag: 'li' });
const aAboutUs = new ElementAnchorCreator({ href: '#', text: 'About us', classes: 'h4 hover:text-primary-color' });
const aAboutUs = new ElementAnchorCreator({ href: '/aboutus', text: 'About us', classes: 'h4 hover:text-primary-color' });
liAboutUs.appendNode(aAboutUs);

const liSummerTime = new ElementCreator({ tag: 'li' });
const aSummerTime = new ElementAnchorCreator({ href: '#', classes: 'h5 hover:text-primary-color', text: 'Summer time' });
const aSummerTime = new ElementAnchorCreator({
href: '/categories#summer-time',
classes: 'h5 hover:text-primary-color',
text: 'Summer time',
});
liSummerTime.appendNode(aSummerTime);

const liPeakClimber = new ElementCreator({ tag: 'li' });
const aPeakClimber = new ElementAnchorCreator({ href: '#', classes: 'h5 hover:text-primary-color', text: 'Peak climber' });
const aPeakClimber = new ElementAnchorCreator({
href: '/categories#peak-climber',
classes: 'h5 hover:text-primary-color',
text: 'Peak climber',
});
liPeakClimber.appendNode(aPeakClimber);

const liBallGames = new ElementCreator({ tag: 'li' });
const aBallGames = new ElementAnchorCreator({ href: '#', classes: 'h5 hover:text-primary-color', text: 'Ball games' });
const aBallGames = new ElementAnchorCreator({
href: '/categories#ball-games',
classes: 'h5 hover:text-primary-color',
text: 'Ball games',
});
liBallGames.appendNode(aBallGames);

const liIceAdventures = new ElementCreator({ tag: 'li' });
const aIceAdventures = new ElementAnchorCreator({
href: '#',
href: '/categories#ice-adventures',
classes: 'h5 hover:text-primary-color',
text: 'Ice adventures',
});
Expand All @@ -83,7 +94,11 @@ export class Header implements Observer {
submenu.appendNode(liSummerTime, liPeakClimber, liBallGames, liIceAdventures);

const tab = new ElementCreator({ tag: 'li', classes: 'relative group tab' });
const aCategories = new ElementAnchorCreator({ href: '#', text: 'Categories', classes: 'h4 hover:text-primary-color' });
const aCategories = new ElementAnchorCreator({
href: '/categories',
text: 'Categories',
classes: 'h4 hover:text-primary-color',
});
tab.appendNode(aCategories, submenu);

const linksList = new ElementCreator({ tag: 'ul', classes: 'items-center justify-between flex gap-5' });
Expand All @@ -108,7 +123,7 @@ export class Header implements Observer {
bg.toggleClass('active');
document.body.classList.toggle('active');
});
tab.getElement().addEventListener('click', () => {
tab.getElement().addEventListener('mouseover', () => {
submenu.addClass('active');
});
submenu.getElement().addEventListener('mouseleave', () => {
Expand Down
53 changes: 47 additions & 6 deletions src/app/components/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
import { ElementCreator } from '../../utils/element-creator/element-creator';

export class Main {
mainView: ElementCreator<HTMLElement>;
export class Main implements Observer {
mainView: HTMLElement;

constructor() {
this.mainView = new ElementCreator({
tag: 'main',
classes: 'container flex flex-col justify-center items-center h-full my-5 md:my-10',
text: 'main',
});
}).getElement();
}

getView(): ElementCreator<HTMLElement> {
getView(): HTMLElement {
return this.mainView;
}

getElement(): HTMLElement {
return this.mainView.getElement();
update(data?: string, secondaryData?: string): void {
this.mainView.textContent = '';
switch (data) {
case 'main':
this.showMain();
break;
case 'login':
this.showLogin();
break;
case 'signup':
this.showSignup();
break;
case 'categories':
this.showCategories(secondaryData);
break;
default:
this.show404();
}
}

showMain(): void {
// TODO add future main context
}

async showLogin(): Promise<void> {
const { Login } = await import('../login/login');
this.mainView.append(new Login().getElement());
}

showCategories(secondaryData?: string): void {
if (secondaryData) {
this.mainView.textContent = secondaryData;
}
}

async showSignup(): Promise<void> {
const { Registration } = await import('../registration/registration');
this.mainView.append(new Registration().getElement());
}

async show404(): Promise<void> {
const { Absent } = await import('../absent/absent');
this.mainView.append(new Absent().getElement());
}
}
2 changes: 1 addition & 1 deletion src/app/interfaces/observer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
interface Observer {
update(): void;
update(primaryData?: string, secondaryData?: string): void;
}
33 changes: 33 additions & 0 deletions src/app/router/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { routes } from './routes';

export class Router implements Observable {
private observers: Observer[] = [];

constructor() {
window.onpopstate = this.handleLocation;
}

subscribe(observer: Observer): void {
if (this.observers.includes(observer)) return;
this.observers.push(observer);
}

unsubscribe(observer: Observer): void {
const index = this.observers.indexOf(observer);
if (index === -1) return;
this.observers.slice(index, 1);
}

notify(primaryData?: string, secondaryData?: string): void {
if (primaryData) {
this.observers.forEach((observer) => observer.update(primaryData, secondaryData));
}
}

public handleLocation = (): void => {
const path = window.location.pathname;
const route = routes[path] || routes[404];
const hashValue = window.location.hash;
this.notify(route, hashValue);
};
}
10 changes: 10 additions & 0 deletions src/app/router/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const routes: Record<string, string> = {
404: '404',
'/': 'main',
'/main': 'main',
'/login': 'login',
'/signup': 'signup',
'/signout': 'signout',
'/aboutus': 'aboutus',
'/categories': 'categories',
};
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"module": "es2022",
"target": "ES2022",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
Expand Down