Skip to content

Conversation

@MaxTF141
Copy link

No description provided.

app.ts Outdated
let lastPage = firstPage + 9
let currentFromID = 1;
let currentToID = 20;
let difference = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to only make use of 2 global variables and give them a type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to give the Type if you set the variable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not have any global variables.

And you only have to set the type if they where class variables or if they were normal variables that were not assigned a value in the same line. And these variables are neither.

app.ts Outdated
let difference = 0


// This function will handle retrieving the records from the api

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for so much space between the code, we all love each other here

app.ts Outdated
async function getColumns(): Promise<Array<string>> {
try {
const data = await fetch(`${api}columns`);
const columns: Array<string> = await data.json();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const columns: Array<string> = await data.json();
const columns: string[]= JSON.parse(data);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always make use of .json() instead of JSON.parse() when able. But the string[] is still the correct comment.

app.ts Outdated
return columns;

} catch (error) {
console.error(error);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to console log

app.ts Outdated
firstPage = lastPage - 9;
$(".pagination").empty();
await pageNumbers(firstPage, lastPage);
// isFunctionRunning = false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to comment this out, looks like it could break if you press prev and then next

app.ts Outdated
}
if (firstPage === 1) {
$(".prev").css({ display: "none" });
} else $(".prev").css({ display: "block" });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no {} for else?

Copy link

@DevinFledermaus DevinFledermaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of you functions make use of the same variables that you keep having to set, maybe get them all in a central place. will make lots of your functions way smaller

apiManager.js Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add your .js files to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add your .js.map files to this PR.

Comment on lines 2 to 3
private mainUrl: string;
constructor(mainUrl: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave an empty line between your class variables and constructor

apiManager.ts Outdated
Comment on lines 8 to 13
return fetch(mainUrl)
.then(res => res.text())
.then(data => JSON.parse(data))
.catch((err) => {
throw err
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if your fetch was successful, as that does not get catch'd by the .catch.

Please use res.json() instead of res.text() and then JSON.parse(). (I know someone else asked you to do it this way, and I apologize that you have to revert your code now again, but this is the right way)

Also, please add a ; to the throw and at the end.

apiManager.ts Outdated
return fetch(mainUrl)
.then(res => res.text())
.then(data => JSON.parse(data))
.catch((err) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either remove the () around err or specify a type

style.css Outdated
Comment on lines 87 to 88
text-overflow: ellipsis;
white-space: nowrap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

style.css Outdated
padding-left: 10px;
}

input:focus{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space before the {

style.css Outdated
margin: 3px;
background-color: #ffffff36;
box-shadow: inset 0px 0px 20px -10px rgba(255, 255, 255, 0.438);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this one empty line.

Comment on lines 152 to 165
.header {
height: 5vh;
}
.heading {
height: 5vh;
}
.table-container {
height: 80vh;
}
.wrapper {
height: 10vh;
display: flex;
align-items: center;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave an empty line between classes.

style.css Outdated
@keyframes rot {
100% {
transform: rotate(360deg);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this one empty line.

Copy link

@anzelIMQS anzelIMQS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave a very partial review. Will get back to you if necessary.

app.ts Outdated
}
})
.catch(error => {
throw new Error(`Failed to retrieve columns: ${error}`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(`Failed to retrieve columns: ${error}`)
throw new Error(`Failed to retrieve columns: ${error}`);

Missing semi-colon

app.ts Outdated
Comment on lines 67 to 68
}
else if (this.currentPage === 1) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
else if (this.currentPage === 1) {
} else if (this.currentPage === 1) {

On the same line please

app.ts Outdated
this.paginationEnd = pageEnd;
}
} else {
throw new Error("Please provide a valid integer.")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error("Please provide a valid integer.")
throw new Error("Please provide a valid integer.");

app.ts Outdated
return this.apiManager.getRecordCount()
.then(count => {
let inputNumber = this.input();
let inputNumberInt: any = parseInt(inputNumber);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it always a number being returned?

apiManager.ts Outdated

/** Retrieves the number of records there are */
getRecordCount(): Promise<number> {
let test = `${this.mainUrl}recordCount`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove stray

apiManager.ts Outdated
Comment on lines 8 to 9
private fetchJson(mainUrl: string): Promise<any> {
return fetch(mainUrl)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private fetchJson(mainUrl: string): Promise<any> {
return fetch(mainUrl)
private fetchJson(url: string): Promise<any> {
return fetch(url)

Feels incorrect calling it the main one since in this scope it is only one and can be anything. Main or not.

Comment on lines 1 to 2
class ApiManager {
private mainUrl: string;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inbetween.

}
$("#records-table-body").append(`</tr>`);
}
this.isFunctionRunning = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know that there is a chance that your request doesn't get this far since it broke somewhere above. That would mean your isFunctionRunning will forever be true and causing no new requests to come in for showRecords.

Ideally, make use of the finally-block instead. It will always run irrespective of the request failing.

app.ts Outdated
Comment on lines 301 to 304
let screenHeight = <number>($(window).height());
if (screenHeight < 68) {
screenHeight = 68;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eish, then why not have a min-height prop on your window?

@MaxTF141 MaxTF141 requested a review from anzelIMQS September 20, 2023 08:41
Copy link

@anzelIMQS anzelIMQS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to see how you can make use of more shared code. I still see some repetitiveness. Try to reduce it by seeing a pattern in your code.

app.ts Outdated
}

/** Fetching the records and rendering it on the DOM in the table*/
showRecords(fromID: number, toID: number): Promise<any> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
showRecords(fromID: number, toID: number): Promise<any> {
showRecords(fromID: number, toID: number): Promise<void> {

Change back to void

app.ts Outdated

constructor() {
this.isFunctionRunning = false;
this.apiManager = new ApiManager("http://localhost:2050/");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.apiManager = new ApiManager("http://localhost:2050/");
this.apiManager = new ApiManager("http://localhost:2050");

apiManager.ts Outdated

/** Retrieves records from the api */
getRecords(fromID: number, toID: number): Promise<string[][]> {
return this.fetchJson(`${this.mainUrl}records?from=${fromID}&to=${toID}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return this.fetchJson(`${this.mainUrl}records?from=${fromID}&to=${toID}`);
return this.fetchJson(`${this.mainUrl}/records?from=${fromID}&to=${toID}`);

apiManager.ts Outdated

/** Retrieves columns from the api */
getColumns(): Promise<string[]> {
return this.fetchJson(`${this.mainUrl}columns`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return this.fetchJson(`${this.mainUrl}columns`);
return this.fetchJson(`${this.mainUrl}/columns`);

apiManager.ts Outdated

/** Retrieves the number of records there are */
getRecordCount(): Promise<number> {
return this.fetchJson(`${this.mainUrl}recordCount`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return this.fetchJson(`${this.mainUrl}recordCount`);
return this.fetchJson(`${this.mainUrl}/recordCount`);

app.ts Outdated
$(".prev").on("click", () => {
this.paginationEnd = this.paginationStart - 1;
this.paginationStart = this.paginationEnd - 9;
$(".pagination").empty();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are already emptying in pageNumbers.
Please remove.

app.ts Outdated
start = end - maxRecords;
}
this.currentPage = Math.floor(end / maxRecords);
if (inputNumberInt < 1000000 && inputNumberInt > 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (inputNumberInt < 1000000 && inputNumberInt > 0) {
if (inputNumberInt < 1000000 && inputNumberInt > 0) {

You have your count. You went all the way to fetch it, use it and not 1000000.

app.ts Outdated
/** Fetching the records and rendering it on the DOM in the table*/
showRecords(fromID: number, toID: number): Promise<any> {
if (this.isFunctionRunning) {
return Promise.resolve(undefined);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Promise.resolve(undefined);
return new Promise<void>(() => { });

app.ts Outdated
// element's attribute value.
$(".pagination").on("click", ".pagination-item", (event) => {
$('.pagination-item').prop('disabled', true);
return this.apiManager.getRecordCount()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to return here. Just run it.

Suggested change
return this.apiManager.getRecordCount()
this.apiManager.getRecordCount()

app.ts Outdated
$(".pagination-item").removeClass("active");
$(event.target).addClass("active");
const self = this;
$(".pagination-item").each(function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this can't be an arrow function?

@MaxTF141 MaxTF141 requested a review from anzelIMQS September 26, 2023 09:13
app.ts Outdated
@@ -0,0 +1,408 @@
class DataHandler {
/** It tracks if the showRecords function is running and would the value would be false if it's not. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** It tracks if the showRecords function is running and would the value would be false if it's not. */
/** It tracks if the showRecords function is running. Is false if it's not. */

apiManager.ts Outdated
return fetch(url)
.then(res => {
if (res.ok) {
return res.json()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing ;

apiManager.ts Outdated
if (res.ok) {
return res.json()
} else {
throw new Error(`HTTP error! Status: ${res.status}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing ;

Comment on lines 34 to 36
getRecordCount(): Promise<number> {
return this.fetchJson(`${this.mainUrl}/recordCount`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So /recordCount returns a number as text... not a json... this feels misleading. You should probably do a different fetch to handle this case.

app.ts Outdated
class DataHandler {
/** It tracks if the showRecords function is running and would the value would be false if it's not. */
private isFunctionRunning: boolean;
/** This is an instance of the ApiManager class and passing a string when being used. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably mean that you provide the url to the server during initialization. The way you have it written here it sounds like every function on this class takes a string as a parameter.

app.ts Outdated
}
this.currentToID = this.currentFromID + maxRecords;
this.currentPage = newCurrentPage;
let originalID = ((this.currentPage * maxRecords) + (this.currentPage - 1)) - maxRecords;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just change this to be

(this.currentPage - 1) * (maxRecords + 1)

It just makes it a bit clearer that you are calculating the fromID of the previous page.

app.ts Outdated
this.currentFromID = (this.currentPage - 1) * maxRecords + 1;
}
if (this.currentFromID === 0) {
let divisor = screenHeight < 136 ? maxRecords : maxRecords - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this hard coded value... 136. You definitely should not need it. Something must be wrong with the way you calculate maxRecords if this is necessary. Please remove it.

app.ts Outdated
clearTimeout(this.resizeTimer);
}
this.resizeTimer = setTimeout(() => {
return this.adjustDisplayedRecords()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning here does not make a lot of sens. setTimeout can't do anything with your promise.

Copy link
Contributor

@FritzOnFire FritzOnFire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only handle errors as late as possible

apiManager.ts Outdated
Comment on lines 36 to 48
.then(res => {
if(res.ok) {
return res.text();
} else {
throw new Error(`HTTP error? Status: ${res.status}`);
}
})
.then (recordCount => {
return parseInt(recordCount);
})
.catch(error => {
throw error;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing some indentation.

apiManager.ts Outdated
getRecordCount(): Promise<number> {
return fetch(`${this.mainUrl}/recordCount`)
.then(res => {
if(res.ok) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(res.ok) {
if (res.ok) {

apiManager.ts Outdated
throw new Error(`HTTP error? Status: ${res.status}`);
}
})
.then (recordCount => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.then (recordCount => {
.then(recordCount => {

apiManager.ts Outdated
}
})
.then (recordCount => {
return parseInt(recordCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer it you checked for NaN here... but for this project it is probably fine.

app.ts Outdated
Comment on lines 182 to 183
alert("An error occurred while trying to load the page. Please try again.");
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Why are you handling the error and then throwing the error anyway?

In this scenario there is no one to catch that error, which is bad, you should remove the throw.

app.ts Outdated
// on to the DOM.
$(".search-input").on("input", (e: any) => {
e.preventDefault();
return this.getRecordCount()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure the handler function jquery is looking for does not return anything. Please remove the return.

app.ts Outdated
Comment on lines 265 to 266
alert("An error occurred while trying to search. Please try again.");
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment about handling the error and then throwing it anyway.

app.ts Outdated
Comment on lines 310 to 312
alert("An error occurred while trying to search. Please try again.");
console.error("Failed when clicking on the results: ", error);
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment about handling the error and then throwing it anyway.

app.ts Outdated
let endID: number;
let pageEnd: number;
let pageStart: number;
return this.getRecordCount()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure the handler function jquery is looking for does not return anything. Please remove the return.

app.ts Outdated
Comment on lines 359 to 360
alert("An error occurred while trying to adjust the records. Please try again.");
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

So you handle the error... and then you throw it anyway meaning that you have to handle it again later?

Please do not handle the error here, as who ever is calling adjustDisplayedRecords needs to know that we failed to adjustDisplayedRecords and they need to react accordingly.

app.ts Outdated
Comment on lines 373 to 374
alert("An error occurred while trying to resize the window. Please try again.");
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't throw here, there is no one left to catch it.

app.ts Outdated
})
.catch(error => {
alert("An error occurred while trying to load your page. Please try again.");
throw new Error(`An error occurred when loading your page: ${error}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't throw here, there is no one left to catch it.

@MaxTF141 MaxTF141 requested a review from FritzOnFire October 2, 2023 06:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants