-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RSS-ECOMM-2_28): add router component (#111)
* feat: add Router component * feat: add environment variables * feat: add EventMediator component
- Loading branch information
Showing
12 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
VITE_APP_DEFAULT_SEGMENT='/' | ||
VITE_APP_NEXT_SEGMENT=1 | ||
VITE_APP_PATH_SEGMENTS_TO_KEEP=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { PageInterface } from '@/shared/types/interfaces.ts'; | ||
|
||
import EventMediatorModel from '@/shared/EventMediator/model/EventMediatorModel.ts'; | ||
import { EVENT_NAMES, MEDIATOR_EVENTS, PAGES_IDS } from '@/shared/constants/enums.ts'; | ||
|
||
const DEFAULT_SEGMENT = import.meta.env.VITE_APP_DEFAULT_SEGMENT; | ||
const NEXT_SEGMENT = import.meta.env.VITE_APP_NEXT_SEGMENT; | ||
const PATH_SEGMENTS_TO_KEEP = import.meta.env.VITE_APP_PATH_SEGMENTS_TO_KEEP; | ||
|
||
class RouterModel { | ||
private eventMediator = EventMediatorModel.getInstance(); | ||
|
||
private pages: Map<string, PageInterface> = new Map(); | ||
|
||
constructor() { | ||
document.addEventListener(EVENT_NAMES.DOM_CONTENT_LOADED, () => { | ||
const currentPath = window.location.pathname | ||
.split(DEFAULT_SEGMENT) | ||
.slice(PATH_SEGMENTS_TO_KEEP + NEXT_SEGMENT) | ||
.join(DEFAULT_SEGMENT); | ||
this.handleRequest(currentPath); | ||
this.eventMediator.notify(MEDIATOR_EVENTS.CHANGE_PAGE, currentPath.split(DEFAULT_SEGMENT).join()); | ||
}); | ||
|
||
window.addEventListener(EVENT_NAMES.POPSTATE, () => { | ||
const currentPath = window.location.pathname | ||
.split(DEFAULT_SEGMENT) | ||
.slice(PATH_SEGMENTS_TO_KEEP + NEXT_SEGMENT) | ||
.join(DEFAULT_SEGMENT); | ||
this.handleRequest(currentPath); | ||
}); | ||
} | ||
|
||
private handleRequest(path: string): null | string { | ||
const pathParts = path.split(DEFAULT_SEGMENT); | ||
const hasRoute = this.pages.has(pathParts.join('')); | ||
if (!hasRoute) { | ||
this.navigateTo(PAGES_IDS.NOT_FOUND_PAGE); | ||
return null; | ||
} | ||
|
||
this.eventMediator.notify(MEDIATOR_EVENTS.CHANGE_PAGE, pathParts.join()); | ||
return pathParts.join(''); | ||
} | ||
|
||
public navigateTo(route: string): History { | ||
this.handleRequest(route); | ||
|
||
const pathnameApp = window.location.pathname | ||
.split(DEFAULT_SEGMENT) | ||
.slice(NEXT_SEGMENT, PATH_SEGMENTS_TO_KEEP + NEXT_SEGMENT) | ||
.join(DEFAULT_SEGMENT); | ||
const url = `/${pathnameApp}/${route}`; | ||
window.history.pushState({}, '', url); | ||
return window.history; | ||
} | ||
|
||
public setPages(pages: Map<string, PageInterface>): Map<string, PageInterface> { | ||
this.pages = pages; | ||
return this.pages; | ||
} | ||
} | ||
|
||
export default RouterModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { ListenerCallback } from '@/shared/types/types'; | ||
|
||
class EventMediatorModel<T> { | ||
private listeners: Map<string, Array<ListenerCallback<T>>> = new Map(); | ||
|
||
private static mediator = new EventMediatorModel(); | ||
|
||
public static getInstance(): EventMediatorModel<unknown> { | ||
return EventMediatorModel.mediator; | ||
} | ||
|
||
public notify(eventName: string, params: T): void { | ||
const eventListeners = this.listeners.get(eventName); | ||
if (eventListeners) { | ||
eventListeners.forEach((listener) => listener(params)); | ||
} | ||
} | ||
|
||
public subscribe(eventName: string, listener: ListenerCallback<T>): void { | ||
if (this.listeners.has(eventName)) { | ||
const listeners = this.listeners.get(eventName); | ||
listeners?.push(listener); | ||
} else { | ||
const newListeners = []; | ||
newListeners.push(listener); | ||
this.listeners.set(eventName, newListeners); | ||
} | ||
} | ||
|
||
public unsubscribe(eventName: string, listener: ListenerCallback<T>): void { | ||
if (this.listeners.has(eventName)) { | ||
const listeners = this.listeners.get(eventName); | ||
const index = listeners?.findIndex((l) => l.toString() === listener.toString()); | ||
|
||
if (index !== undefined && index !== -1) { | ||
listeners?.splice(index, 1); | ||
|
||
if (listeners) { | ||
this.listeners.set(eventName, listeners); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default EventMediatorModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters