-
Notifications
You must be signed in to change notification settings - Fork 35
Onboarding Project: Meezaan #49
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
base: master
Are you sure you want to change the base?
Conversation
app.ts
Outdated
| let searchResultDisplay = false; | ||
| let isSearchMode = false; | ||
| let searchResultIndexes: number[] = []; | ||
| let specialResizeOccurred = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try sticking to two global variables. And if you set a variable no need to give the type aswell
app.ts
Outdated
| handleSpecialResize(); | ||
| }; | ||
| // Global variables | ||
| const IMQS: string = "http://localhost:2050"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
camelCase please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ignore, as for global constant values we are supposed to always use all caps.
app.ts
Outdated
| let recordCount: number = await (await fetch(`${IMQS}/recordCount`)).json(); | ||
| totalRecordCount = recordCount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let recordCount: number = await (await fetch(`${IMQS}/recordCount`)).json(); | |
| totalRecordCount = recordCount; | |
| let recordCount: number = await fetch(`${IMQS}/recordCount`); | |
| totalRecordCount = JSON.parse(recordCount); |
DevinFledermaus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a space between the functions
app.ts
Outdated
| const IMQS = "http://localhost:2050"; | ||
| let totalRecordCount: number; | ||
| let totalPages: number; | ||
| let currentValueOfFirstRecord = 1; | ||
| let currentFirstRecordIndex = 1; | ||
| let currentPage: number; | ||
| let recordsPerPage: number; | ||
| let searchedIndex: number | null = null; | ||
| let resizeTimeout: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Way too many global variables, try for max two
app.ts
Outdated
| async function totalRecords(): Promise<void> { | ||
| try { | ||
| let recordCountResponse = await fetch(`${IMQS}/recordCount`); | ||
| let recordCountText = await recordCountResponse.text(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to await here
app.ts
Outdated
| async function displayColumns() { | ||
| try { | ||
| let columnsResponse = await fetch(`${IMQS}/columns`); | ||
| let columns = await columnsResponse.json(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to await here
app.ts
Outdated
| let columnsResponse = await fetch(`${IMQS}/columns`); | ||
| let columns = await columnsResponse.json(); | ||
| let tableHeaderRow = $("#tableHeaderRow"); | ||
| columns.forEach((columnName: string) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use a for..of loop instead
app.ts
Outdated
| let response = await fetch( | ||
| `${IMQS}/records?from=${fromRecord}&to=${fromRecord + maximumRecords - 1 | ||
| }` | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You making this call multiple times, try having it a function of it's own, similar to totalRecords, and that way displayData wont need to be wont need to be an async function
app.ts
Outdated
| throw (error) | ||
| }; | ||
| }; | ||
| async function updatePages(isNavigation = false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wont be an async when displayData is changed
app.ts
Outdated
| const recordsText = await response.text(); | ||
| const records = JSON.parse(recordsText); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const recordsText = await response.text(); | |
| const records = JSON.parse(recordsText); | |
| const records = JSON.parse(response.text()); |
app.ts
Outdated
| const searchPageIndex = searchedIndex % recordsPerPage; | ||
| currentPage = searchPage; | ||
| currentFirstRecordIndex = (searchPage - 1) * recordsPerPage; | ||
| await displayData(currentFirstRecordIndex, recordsPerPage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| await displayData(currentFirstRecordIndex, recordsPerPage); | |
| displayData(currentFirstRecordIndex, recordsPerPage); |
app.ts
Outdated
| await searchMethod(currentValueOfFirstRecord); | ||
| } | ||
| let fromRecord = currentFirstRecordIndex; | ||
| await displayData(fromRecord, recordsPerPage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| await displayData(fromRecord, recordsPerPage); | |
| displayData(fromRecord, recordsPerPage); |
app.ts
Outdated
| } catch (error) { | ||
| throw (error) | ||
| }; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No ; after the declaration of functions
anzelIMQS
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Partial review
app.ts
Outdated
| return recordCount; | ||
| }).catch((error) => { | ||
| throw error; | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| }) | |
| }); |
Missing semi-colon
app.ts
Outdated
| .then((dataText) => JSON.parse(dataText)) | ||
| .catch((error) => { | ||
| throw error; | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| }) | |
| }); |
Missing semi-colon
app.ts
Outdated
| .then((response) => response.text()) | ||
| .then((dataText) => JSON.parse(dataText)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just say
| .then((response) => response.text()) | |
| .then((dataText) => JSON.parse(dataText)) | |
| .then((response) => response.json()) |
Instead of first to text and then JSON.parse.
This is applicable to your other fetches as well.
If it is not valid json, the catch will throw your error at least.
app.ts
Outdated
| } | ||
|
|
||
| // Fetch column names and creates them as table headings | ||
| function fetchColumns() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a return type. You are returning a promise of something or void?
app.ts
Outdated
| updateScreen(); | ||
| }).catch((error) => { | ||
| throw error; | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| }) | |
| }); |
Missing semi-colon
app.ts
Outdated
| } | ||
|
|
||
| // Search for records by a given value and display them on the page. | ||
| async function searchMethod(searchValue: any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| async function searchMethod(searchValue: any) { | |
| async searchMethod(searchValue: number): Promise<fill in your return type> { |
Give return types and parameter type.
app.ts
Outdated
| searchValue = Math.min(searchValue, (await totalRecords()) - 1); | ||
| let targetPage = Math.ceil((searchValue + 1) / state.recordsPerPage); | ||
| targetPage = Math.min(targetPage, totalPages); | ||
| const lastRecordIndex = (await totalRecords()) - 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You really shouldn't have to call totalRecords multiple times in the same function. Call it once and store the value that the promise returns. Handle it.
app.ts
Outdated
| } else { | ||
| $("#nextPageButton").show(); | ||
| } | ||
| }).catch((error) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| }).catch((error) => { | |
| }) | |
| .catch((error) => { |
Put catches on the next line like the suggestion. The same like you did with the thens
app.ts
Outdated
| } | ||
| }) | ||
|
|
||
| $("#searchForm").submit(async function (e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| $("#searchForm").submit(async function (e) { | |
| $("#searchForm").submit(async (e) => { |
app.ts
Outdated
| await searchMethod(searchValue); | ||
| if (state.searchedIndex !== null) { | ||
| currentPage = Math.ceil((state.searchedIndex + 1) / state.recordsPerPage); | ||
| state.currentFirstRecordIndex = Math.max(state.searchedIndex - state.recordsPerPage + 1, 0); | ||
| }; | ||
| $("#loader").hide(); | ||
| $("#tableWrapper").show(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| await searchMethod(searchValue); | |
| if (state.searchedIndex !== null) { | |
| currentPage = Math.ceil((state.searchedIndex + 1) / state.recordsPerPage); | |
| state.currentFirstRecordIndex = Math.max(state.searchedIndex - state.recordsPerPage + 1, 0); | |
| }; | |
| $("#loader").hide(); | |
| $("#tableWrapper").show(); | |
| searchMethod(searchValue) | |
| .then(() => { | |
| if (state.searchedIndex !== null) { | |
| currentPage = Math.ceil((state.searchedIndex + 1) / state.recordsPerPage); | |
| state.currentFirstRecordIndex = Math.max(state.searchedIndex - state.recordsPerPage + 1, 0); | |
| } | |
| $("#loader").hide(); | |
| $("#tableWrapper").show(); | |
| }); |
Handle your promise rather. It feels cleaner than await. Excuse the indentation in the suggestion.
app.ts
Outdated
| let totalPages: number; | ||
| let currentPage: number; | ||
|
|
||
| const state = { | ||
| IMQS: "http://localhost:2050", | ||
| currentValueOfFirstRecord: 1, | ||
| currentFirstRecordIndex: 1, | ||
| recordsPerPage: 16, | ||
| searchedIndex: null as number | null, | ||
| isButtonDisabled: false | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Global variables are bad, please remove them.
I noticed that previous comments on the PR indicated a number of global variables to aim for. Please ignore that, as it was meant to be a worst case scenario in case the creator of the PR could not figure out how to do it.
I would recommend moving them to a class along with the methods that are depended on them.
app.ts
Outdated
| isButtonDisabled: false | ||
| } | ||
|
|
||
| // Fetch the total number of records from the server. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change this comment to match the javadoc style. This is unfortunately not currently in our style guide.
app.ts
Outdated
| // Fetch the total number of records from the server. | ||
| function totalRecords(): Promise<number> { | ||
| return fetch(`${state.IMQS}/recordCount`) | ||
| .then((recordCountResponse) => recordCountResponse.json()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the () around recordCountResponse or specify a type.
Also, you need to check if the response was good, as .catch is not going to catch that for you.
app.ts
Outdated
| }); | ||
| } | ||
|
|
||
| // Fetch column names and creates them as table headings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar javadoc style comment.
app.ts
Outdated
| // Fetch column names and creates them as table headings | ||
| function fetchColumns(): Promise<void> { | ||
| return fetch(`${state.IMQS}/columns`) | ||
| .then((columnsResponse) => columnsResponse.json()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either remove the () around columnsResponse or specify a type.
Also, you need to check the response was good, as .catch is not going to catch that for you.
index.html
Outdated
| <button id="prevPageButton">Previous Page</button> | ||
| <form id="searchForm"> | ||
| <div class="form-group"> | ||
| <input type="number" id="searchInput" placeholder="Search ID" min="1" required /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I can't search for the first record? Bit of an odd choice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use tabs as indentation.
style.css
Outdated
| /* Colors */ | ||
| :root { | ||
| --primary-color: white; | ||
| --secondary-color: #292929; | ||
| --tertiary-color: black; | ||
| --shadow-color: #e3e3e3; | ||
| --selected-color: #0d6efd; | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely want to set your variables first in this file. I would recommend moving it up to be the first thing in this file.
style.css
Outdated
| background: var(--selected-color); | ||
| box-shadow: none !important; | ||
| border: 1px solid var(--secondary-color); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting got a bit weird here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ensure an empty line between classes.
app.ts
Outdated
| currentValueOfFirstRecord: number = 0; | ||
| /** Index of the first record currently displayed on the page */ | ||
| currentFirstRecordIndex: number = 0; | ||
| /** Current page number (changes dynamically)*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /** Current page number (changes dynamically)*/ | |
| /** Current page number (changes dynamically) */ |
app.ts
Outdated
| recordsPerPage: number = 16; | ||
| /** Index of the record being searched for (null if not searching) */ | ||
| searchedIndex: number | null = null; | ||
| /** Checks if the button is enabled/disabled*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /** Checks if the button is enabled/disabled*/ | |
| /** Checks if the button is enabled/disabled */ |
app.ts
Outdated
| const loadApp = new Promise<void>((resolve, reject) => { | ||
| window.onload = () => { | ||
| try { | ||
| $("#loader").hide(); | ||
| $(window).on("resize", this.debounce(() => { | ||
| this.updateScreen(); | ||
| }, 250)); | ||
| this.fetchColumns(); | ||
| this.updateScreen(); | ||
| this.eventHandlers(); | ||
| resolve(); | ||
| } catch (error) { | ||
| reject(error); | ||
| } | ||
| }; | ||
| }); | ||
| loadApp | ||
| .catch(() => { | ||
| throw new Error('Error during window.onload:'); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do this somewhere else... a constructor should not be doing anything that can result in an error being thrown. And it should also definitely not be doing any asynchronous calls.
app.ts
Outdated
| if (!recordCountResponse.ok) { | ||
| throw new Error('Error trying to get recordCount'); | ||
| } | ||
| return recordCountResponse.json(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure /recordCount does not return a json... Pretty sure you want to return text() and then parse the text in the next .then()
app.ts
Outdated
| .then(columnsResponse => { | ||
| if (!columnsResponse.ok) { | ||
| throw new Error('Error trying to fetch the columns'); | ||
| return []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this return, as you should not have a return statement after a throw
app.ts
Outdated
| if (this.currentPage >= this.totalPages) { | ||
| const errorMessage = "Already on the last page"; | ||
| window.alert(errorMessage); | ||
| throw new Error(errorMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are the last line of defense, you can't throw an error here. But it looks like you are all ready handling the error with window.alert, so I would just replace this line with a return.
app.ts
Outdated
| $("#prevPageButton").hide(); | ||
| $("#tableWrapper").hide(); | ||
| $("#loader").show(); | ||
| let totalRecCount: number | void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value is not set anywhere... I think you meant to place it in the .then that handles the promise coming from totalRecords.
app.ts
Outdated
| return this.displayData(fromRecord, this.recordsPerPage); | ||
| }) | ||
| .catch((error) => { | ||
| throw error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are the last line of defense, you need to handle the error here.
app.ts
Outdated
| } | ||
|
|
||
| /** Initialize the app when the page loads */ | ||
| new InitializeApp(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels out of place without the window.onload = () => { ... }; mentioned in the readme of this project. Not saying your code has to look like that, but I would struggle to have something to comment about if you still used the example that we provided in the readme.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still relevant.
No description provided.