-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ee4ebf
commit c3f4877
Showing
7 changed files
with
96 additions
and
18 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 |
---|---|---|
@@ -1,4 +1,19 @@ | ||
/** | ||
* @abstract | ||
* @description Base abstract class defining the structure for page components, requiring implementation of lifecycle methods for mounting and destruction. | ||
*/ | ||
export default abstract class PageAbstract { | ||
/** | ||
* @abstract | ||
* @description Method to initialize and mount the page component. Should be implemented by subclasses to set up event listeners, render content, etc. | ||
* @returns {void} | ||
*/ | ||
abstract mount(): void; | ||
|
||
/** | ||
* @abstract | ||
* @description Method to clean up and perform necessary teardown operations before the page component is destroyed. Should be implemented by subclasses to remove event listeners, clear timers, etc. | ||
* @returns {void} | ||
*/ | ||
abstract beforeDestroy(): void; | ||
} |
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 |
---|---|---|
@@ -1,38 +1,72 @@ | ||
import api from '../api/RequestHandler'; | ||
|
||
export default class RouteHandler { | ||
/** | ||
* @constructor | ||
* @description Initializes the RouteHandler by setting the current route to 'home-page' | ||
* if no route is present. Sets up an event listener for the 'popstate' event | ||
* to handle route changes. | ||
*/ | ||
constructor() { | ||
if (!this.getCurrentRoute()) { | ||
this.setNewRoute('home-page'); | ||
} | ||
window.addEventListener('popstate', () => this.handleRouteChange()); | ||
window.addEventListener('popstate', () => this.#handleRouteChange()); | ||
} | ||
|
||
private getCurrentUrl(): URL { | ||
/** | ||
* @private | ||
* @returns {URL} The current URL object. | ||
* @description Retrieves the current URL of the window. | ||
*/ | ||
#getCurrentUrl(): URL { | ||
return new URL(window.location.href); | ||
} | ||
|
||
private getQueryParams(): URLSearchParams { | ||
return this.getCurrentUrl().searchParams; | ||
/** | ||
* @private | ||
* @returns {URLSearchParams} The URLSearchParams object containing the query parameters. | ||
* @description Retrieves the query parameters from the current URL. | ||
*/ | ||
#getQueryParams(): URLSearchParams { | ||
return this.#getCurrentUrl().searchParams; | ||
} | ||
|
||
/** | ||
* @public | ||
* @returns {string | null} The current route name, or null if not found. | ||
* @description Gets the current route from the query parameters. | ||
*/ | ||
public getCurrentRoute(): string | null { | ||
return this.getQueryParams().get('route'); | ||
return this.#getQueryParams().get('route'); | ||
} | ||
|
||
/** | ||
* @public | ||
* @param {string} newRoute - The new route name to set. | ||
* @description Sets a new route by updating the query parameters and pushing the new URL | ||
* to the browser's history. | ||
*/ | ||
public setNewRoute(newRoute: string): void { | ||
const queryParams = this.getQueryParams(); | ||
const queryParams = this.#getQueryParams(); | ||
queryParams.set('route', newRoute); | ||
|
||
const newUrl = `${this.getCurrentUrl().pathname}?${queryParams.toString()}`; | ||
const newUrl = `${this.#getCurrentUrl().pathname}?${queryParams.toString()}`; | ||
|
||
window.history.pushState(null, '', newUrl); | ||
} | ||
|
||
private handleRouteChange() { | ||
/** | ||
* @private | ||
* @async | ||
* @returns {Promise<void>} A promise that resolves when the request is complete. | ||
* @description Handles changes to the route by sending a POST request to the new route. | ||
* If the new route is not null, it makes a request using the api module. | ||
*/ | ||
async #handleRouteChange() { | ||
const newRoute = this.getCurrentRoute(); | ||
if (newRoute !== null) { | ||
api.post(newRoute, new FormData(), true); | ||
await api.post(newRoute, new FormData(), true); | ||
} | ||
} | ||
} |
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