-
Notifications
You must be signed in to change notification settings - Fork 35
Riaan Onboard Project Review #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
riaanwastaken
wants to merge
27
commits into
IMQS:master
Choose a base branch
from
riaanwastaken:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d920bd5
Getting Started Steps Complete to "Hello world"
riaanwastaken adb6798
index.html script error changed from .js to .ts
riaanwastaken 5be4a0c
Changed Back to app.js script in index.html
riaanwastaken 89830dd
Added "npn run build" to package.json
riaanwastaken a2b438f
Table Index Values Load in Browser Success Rev 1.1
riaanwastaken 0e12f47
Program Rebuilt using Fetch API Rev 1.2
riaanwastaken 66fa84a
HTML Static Table Display on Browser Rev 1.3
riaanwastaken d37649e
Dynamic Table Generation Rev 1.4
riaanwastaken fec8cea
Pagination Complete Rev 1.5
riaanwastaken 054fa47
Dynamic Rows Rev 1.6
riaanwastaken 453d11c
Styling Applied Rev 1.6
riaanwastaken 86a108e
Applied Performance Changes Rev 1.7
riaanwastaken 26ac37a
Pagination Functionality Improvements Rev 1.8
riaanwastaken fd780c1
Styling and Edge Cases Fixed Rev 1.9
riaanwastaken 4f6d7e9
Added Classes to improve modularity Rev 1.10
riaanwastaken 131916b
Improved architecture Rev 1.11
riaanwastaken 3e7b865
Progress Update
riaanwastaken 6a8c40c
Improved architecture Rev 1.12
riaanwastaken ecb725a
Improved architecture Rev 1.13
riaanwastaken b74badf
Functionality Improvements Rev 1.14
riaanwastaken f7cc4b2
Error handling added Rev 1.15
riaanwastaken 7b635b8
Event listeners moved into Class Rev 1.16
riaanwastaken e9e9853
Comments and bug fix Rev 1.17
riaanwastaken 58836c3
Code Cleanup Rev 1.18
riaanwastaken 9e0c219
Added .js files to gitignore Rev 1.19
riaanwastaken c316caa
Progress Save Rev 1.20
riaanwastaken ff34019
Fritz Reviews Finished Rev 1.21
riaanwastaken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 +1,12 @@ | ||
| /node_modules | ||
| app.js | ||
| app.js.map | ||
| controllers.js | ||
| controllers.js.map | ||
| data.js | ||
| data.js.map | ||
| model.js | ||
| model.js.map | ||
| views.js | ||
| views.js.map | ||
| .vscode/settings.json |
This file contains hidden or 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,48 @@ | ||
| /** | ||
| * Project Overview - MVC Architecture: | ||
| * | ||
| * This project adheres to the Data-> Model-View-Controller (MVC) design pattern, ensuring a clear separation of concerns: | ||
| * | ||
| 1. **Data Source**: | ||
| * - The foundational source from which the application retrieves its raw data. This is | ||
| * API endpoints. | ||
| * | ||
| * 2. **Model** (`StateManager`): | ||
| * - Manages the state and data of the application. | ||
| * - Interfaces with the data source to handle data retrieval, filtering, pagination, and other data-related operations. | ||
| * | ||
| * 3. **View** (`TableRenderer`): | ||
| * - Responsible for rendering and updating the user interface. | ||
| * - Renders column headers, records, and manages the visual representation. | ||
| * | ||
| * 4. **Controllers**: | ||
| * - **WindowResizeHandler**: Detects window resize events and updates the table view accordingly. | ||
| * - **PaginationManager**: Manages page navigation, searching functionalities, and live input validation. | ||
| * | ||
| */ | ||
|
|
||
| /*** Main Script ***/ | ||
|
|
||
| window.onload = async () => { | ||
| // Initialize data.ts | ||
| const apiManager = new ApiManager(); | ||
|
|
||
| // Initialize model.ts | ||
| const stateManager = new StateManager(apiManager); | ||
| await stateManager.initializeState(); | ||
|
|
||
| // Initialize views.ts | ||
| const tableRenderer = new TableRenderer(stateManager); | ||
| await tableRenderer.initialRender(); | ||
|
|
||
| // Initialize controllers.ts | ||
| const paginationManager = new PaginationManager( | ||
| tableRenderer, | ||
| stateManager | ||
| ); | ||
| const windowResizeHandler = new WindowResizeHandler( | ||
| tableRenderer, | ||
| stateManager, | ||
| paginationManager | ||
| ); | ||
| }; |
This file contains hidden or 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,325 @@ | ||
| /*** Controllers ***/ | ||
|
|
||
| /** Handles window resize events to update the view of the application. */ | ||
| class WindowResizeHandler { | ||
| private debouncedUpdate: Function; | ||
| private paginationManager: PaginationManager; | ||
| private tableRenderer: TableRenderer; | ||
| private stateManager: StateManager; | ||
|
|
||
| /** | ||
| * @param {TableRenderer} tableRenderer - Used for re-rendering table data. | ||
| * @param {StateManager} stateManager - State control for retrieving/updating application data. | ||
| */ | ||
|
|
||
| constructor ( | ||
| tableRenderer: TableRenderer, | ||
| stateManager: StateManager, | ||
| paginationManager: PaginationManager | ||
| ) { | ||
| this.debouncedUpdate = this.debounce( | ||
| this.updateAfterResize.bind(this), | ||
| 250 | ||
| ); | ||
| this.paginationManager = paginationManager; | ||
| this.tableRenderer = tableRenderer; | ||
| this.stateManager = stateManager; | ||
|
|
||
| // Attach event listener for window resize. | ||
| this.setupEventListenersResize(); | ||
| } | ||
|
|
||
| private setupEventListenersResize(): void { | ||
| window.addEventListener("resize", () => this.handleResize()); | ||
| } | ||
|
|
||
| handleResize(): void { | ||
| try { | ||
| this.debouncedUpdate(); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error in handleResize: ${ | ||
| error instanceof Error ? error.message : error | ||
| }` | ||
| ); | ||
| alert("An error occurred while resizing. Please try again."); | ||
| } | ||
| } | ||
|
|
||
| // Debounce function to reduce the number of function calls while user is dragging the browser window. | ||
| debounce(func: Function, delay: number): Function { | ||
| let timeout: ReturnType<typeof setTimeout> | null = null; | ||
| return (...args: any[]) => { | ||
| const later = () => { | ||
| timeout = null; | ||
| func(...args); | ||
| }; | ||
| if (timeout !== null) { | ||
| clearTimeout(timeout); | ||
| } | ||
| timeout = setTimeout(later, delay); | ||
| }; | ||
| } | ||
|
|
||
| async updateAfterResize(): Promise<void> { | ||
| this.stateManager.adjustWindowSize(); | ||
| await this.stateManager.retrieveRecords().catch((error) => { | ||
| console.error( | ||
| `Error retrieving records: ${ | ||
| error instanceof Error ? error.message : error | ||
| }` | ||
| ); | ||
| alert( | ||
| "An error occurred while retrieving records. Please try again later." | ||
| ); | ||
| return; | ||
| }); | ||
|
|
||
| const records = this.stateManager.getRecords(); | ||
|
|
||
| if (records !== null) { | ||
| this.tableRenderer.renderRecords(records); | ||
| } | ||
| this.paginationManager.updateButtonStates(); | ||
| } | ||
| } | ||
|
|
||
| /** Handles pagination and search functionalities for the application's table view. */ | ||
| class PaginationManager { | ||
| // DOM elements required for pagination and search. | ||
| private prevButton: HTMLButtonElement | null = null; | ||
| private nextButton: HTMLButtonElement | null = null; | ||
| private searchButton: HTMLButtonElement | null = null; | ||
| private mainHeading: HTMLElement | null = null; | ||
| private filterInput: HTMLInputElement | null = null; | ||
| private errorMessage: HTMLElement | null = null; | ||
|
|
||
| /** | ||
| * @param {TableRenderer} tableRenderer - Used for re-rendering table data. | ||
| * @param {StateManager} stateManager - State control for retrieving/updating application data. | ||
| */ | ||
| constructor ( | ||
| private tableRenderer: TableRenderer, | ||
| private stateManager: StateManager | ||
| ) { | ||
| this.initializeDOMElements(); | ||
| // Attach event listeners for buttons and other UI elements. | ||
| this.setupEventListeners(); | ||
| } | ||
|
|
||
| private initializeDOMElements(): void { | ||
| this.prevButton = this.retrieveElement( | ||
| "prevPage", | ||
| "button" | ||
| ) as HTMLButtonElement; | ||
| this.nextButton = this.retrieveElement( | ||
| "nextPage", | ||
| "button" | ||
| ) as HTMLButtonElement; | ||
| this.searchButton = this.retrieveElement( | ||
| "searchButton", | ||
| "button" | ||
| ) as HTMLButtonElement; | ||
| this.mainHeading = this.retrieveElement( | ||
| "main-heading", | ||
| "heading" | ||
| ) as HTMLElement; | ||
| this.filterInput = this.retrieveElement( | ||
| "filterInput", | ||
| "input box" | ||
| ) as HTMLInputElement; | ||
| this.errorMessage = this.retrieveElement( | ||
| "errorMessage", | ||
| "error message" | ||
| ) as HTMLElement; | ||
| } | ||
|
|
||
| private retrieveElement( | ||
| id: string, | ||
| description?: string | ||
| ): HTMLElement | null { | ||
| const element = document.getElementById(id); | ||
| if (!element) { | ||
| console.error(`Element with ID '${id}' not found`); | ||
| if (description) { | ||
| alert( | ||
| `A critical ${description} is missing on the page. Some functionalities might not work as expected.` | ||
| ); | ||
| } | ||
| } | ||
| return element; | ||
| } | ||
|
|
||
| /** Attaches event listeners to the relevant DOM elements to handle user interactions. */ | ||
| private setupEventListeners(): void { | ||
| if (this.prevButton) { | ||
| this.prevButton.addEventListener("click", () => | ||
| this.decrementPage() | ||
| ); | ||
| } | ||
|
|
||
| if (this.nextButton) { | ||
riaanwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.nextButton.addEventListener("click", () => | ||
| this.incrementPage() | ||
| ); | ||
| } | ||
|
|
||
| if (this.searchButton) { | ||
riaanwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.searchButton.addEventListener("click", () => | ||
| this.searchById() | ||
| ); | ||
| } | ||
|
|
||
| if (this.filterInput) { | ||
riaanwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.filterInput.addEventListener("keyup", (event) => { | ||
| if (event.key === "Enter") { | ||
| this.searchById(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (this.mainHeading) { | ||
riaanwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.mainHeading.addEventListener("click", () => | ||
| this.navigateToHome() | ||
| ); | ||
| } | ||
|
|
||
| if (this.filterInput && this.errorMessage) { | ||
riaanwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.setupLiveValidation(); | ||
| } | ||
| } | ||
|
|
||
| /** Navigates to the home page by reloading the window.*/ | ||
| navigateToHome(): void { | ||
| try { | ||
| window.location.reload(); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error while navigating to home: ${ | ||
| error instanceof Error ? error.message : error | ||
| }` | ||
| ); | ||
| alert("Failed to reload the page. Please try again."); | ||
| } | ||
| } | ||
|
|
||
| /** Fetches the next set of records and updates the view. */ | ||
| async incrementPage(): Promise<void> { | ||
| this.stateManager.goToNextPage(); | ||
|
|
||
| await this.stateManager.retrieveRecords().catch((error) => { | ||
| console.error( | ||
| `Error in retrieveRecords while incrementing page: ${ | ||
| error instanceof Error ? error.message : error | ||
| }` | ||
| ); | ||
| alert("Failed to increment the page. Please contact support."); | ||
| }); | ||
|
|
||
| const records = this.stateManager.getRecords(); | ||
|
|
||
| if (records !== null) { | ||
| this.tableRenderer.renderRecords(records); | ||
| } | ||
| this.updateButtonStates(); | ||
| } | ||
|
|
||
| /** Fetches the previous set of records and updates the view. */ | ||
| async decrementPage(): Promise<void> { | ||
| this.stateManager.goToPreviousPage(); | ||
|
|
||
| await this.stateManager.retrieveRecords().catch((error) => { | ||
| console.error( | ||
| `Error in retrieveRecords while decrementing page: ${ | ||
| error instanceof Error ? error.message : error | ||
| }` | ||
| ); | ||
| alert("Failed to decrement the page. Please contact support."); | ||
| }); | ||
|
|
||
| const records = this.stateManager.getRecords(); | ||
|
|
||
| if (records !== null) { | ||
| this.tableRenderer.renderRecords(records); | ||
| } | ||
|
|
||
| this.updateButtonStates(); | ||
| } | ||
|
|
||
| /** Searches for a record by its ID and updates the view. */ | ||
| async searchById(): Promise<void> { | ||
| if (!this.filterInput) { | ||
| alert("Filter input element is missing"); | ||
| return; | ||
| } | ||
|
|
||
| const searchValue = parseInt(this.filterInput.value, 10); | ||
| if (isNaN(searchValue)) { | ||
| alert("Invalid search value or none"); | ||
| return; | ||
| } | ||
|
|
||
| this.stateManager.setHighlightedId(searchValue); | ||
|
|
||
| await this.stateManager | ||
| .searchByIdStateChange(searchValue) | ||
| .catch((error) => { | ||
| console.error( | ||
| `Error in searchByIdStateChange: ${ | ||
| error instanceof Error ? error.message : error | ||
| }` | ||
| ); | ||
| alert("A serious error occurred, please try again later"); | ||
| return; | ||
| }); | ||
|
|
||
| const records = this.stateManager.getRecords(); | ||
|
|
||
| if (records !== null) { | ||
| this.tableRenderer.renderRecords(records, searchValue); | ||
| } | ||
|
|
||
| this.updateButtonStates(); | ||
| } | ||
|
|
||
| /** Validates input for the search bar in real-time. */ | ||
| setupLiveValidation(): void { | ||
| if (!this.filterInput || !this.errorMessage) { | ||
riaanwastaken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.error( | ||
| "Live validation setup failed: Required elements not found." | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| this.filterInput.addEventListener("input", () => { | ||
| const inputValue = this.filterInput!.value; // The "!" here asserts non-null, because I already checked for null above. | ||
| const maxValue = this.stateManager.getTotalRecordCount() - 1; | ||
| if (inputValue.length === 0) { | ||
| this.errorMessage!.textContent = ""; | ||
| } else if ( | ||
| inputValue.length < 1 || | ||
| inputValue.length > 6 || | ||
| !/^\d+$/.test(inputValue) | ||
| ) { | ||
| this.errorMessage!.textContent = `Invalid input. Please enter a number between 0 and ${maxValue}.`; | ||
| } else { | ||
| this.errorMessage!.textContent = ""; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** Updates the state of the pagination buttons based on the current view. */ | ||
| public updateButtonStates(): void { | ||
| if (!this.prevButton || !this.nextButton) { | ||
| alert("Button elements are missing"); | ||
| return; | ||
| } | ||
|
|
||
| const from = this.stateManager.getFrom(); | ||
| const to = this.stateManager.getTo(); | ||
| const totalRecordCount = this.stateManager.getTotalRecordCount(); | ||
|
|
||
| this.prevButton.disabled = from === 0; | ||
| this.nextButton.disabled = to === totalRecordCount - 1; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.