Skip to content
Open
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
88a0c43
final push
FledermausDevin Apr 12, 2022
69a305f
final push
FledermausDevin Apr 12, 2022
95cabc6
final push
FledermausDevin Apr 12, 2022
5f81214
final push
FledermausDevin Apr 12, 2022
75bff11
final push
FledermausDevin Apr 12, 2022
3b59290
final push
FledermausDevin Apr 12, 2022
6de1c55
revised code attempt 1
FledermausDevin Apr 13, 2022
7bd7bc8
revised code attempt 1
FledermausDevin Apr 13, 2022
7991e74
next and prev buttons fixed
FledermausDevin Apr 14, 2022
c54940f
next and prev buttons fixed
FledermausDevin Apr 14, 2022
3e5eae5
next and prev buttons fixed
FledermausDevin Apr 14, 2022
b028cc4
revised code
FledermausDevin Apr 20, 2022
dfc1c75
revised code
FledermausDevin Apr 20, 2022
6c53b8d
window resizing
FledermausDevin Apr 21, 2022
6a9233b
window resizing
FledermausDevin Apr 21, 2022
708034d
window resizing
FledermausDevin Apr 22, 2022
027f290
resize
FledermausDevin Apr 25, 2022
c4039ef
debounce implimented
FledermausDevin Apr 25, 2022
ecceaf0
debounce implimented
FledermausDevin Apr 25, 2022
5ff3563
debounce implimented
FledermausDevin Apr 25, 2022
405592d
column aligning
FledermausDevin Apr 26, 2022
c67502c
column aligning
FledermausDevin Apr 26, 2022
7cf9e78
final testing complete
FledermausDevin Apr 26, 2022
708ef31
final testing complete
FledermausDevin Apr 28, 2022
1256e0c
revision after code review
FledermausDevin Apr 29, 2022
93e2511
revision after code review
FledermausDevin Apr 29, 2022
0b1c2e6
03 May code review revision
FledermausDevin May 3, 2022
876307b
03 May code review revision
FledermausDevin May 3, 2022
d2cf1df
03 May code review revision
FledermausDevin May 3, 2022
e31c2d3
03 May code review revision
FledermausDevin May 3, 2022
f5fdd2d
03 May code review revision
FledermausDevin May 3, 2022
951158c
4 May Code Review Implimented
FledermausDevin May 4, 2022
46fc0eb
05 May code revisied
FledermausDevin May 5, 2022
b581516
05 May code revisied
FledermausDevin May 5, 2022
4d5c1a5
05 May code revisied
FledermausDevin May 5, 2022
f2457da
06 May code revisied
FledermausDevin May 6, 2022
ade9748
06 May code revisied
FledermausDevin May 6, 2022
cdaf8aa
09 May code review
FledermausDevin May 9, 2022
b72ecd9
09 May code review
FledermausDevin May 9, 2022
7b4e7a6
09 May code review
FledermausDevin May 9, 2022
62417f3
11 May update
FledermausDevin May 11, 2022
d3ad477
11 May update
FledermausDevin May 11, 2022
e82ac48
11 May update
FledermausDevin May 11, 2022
4bdde48
12 May - Final Changes
FledermausDevin May 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 292 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
let fromParameter = 0;
let timer: number;
let recordCount: number;

//// On Window Load
window.onload = function () {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use an arrow function.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bump

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bump

const next = new Next(); // next class
const prev = new Prev(); // prev class
window.addEventListener("input", debounce(idJump, 500));
window.addEventListener("resize", debounce(resizing, 500));
getRecordCount()
.then(() => {
return getHeadings();
})
.then(() => {
return getTable();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The classes and functions that you are using here should be defined before you use them.

};

// Gets The 2nd paramater from the 1st parameter
function getParameters(fromParameter: number): number {
let toParameter: number;
let noOfRows = getNoOfRows();

toParameter = fromParameter + noOfRows;
return toParameter;
}

// Gets the number of rows on the screen
function getNoOfRows(): number {
const height = window.innerHeight;
let noOfRows = Math.floor(height / 40);
return noOfRows;
}

//// Functions To Create The HTML
// Heading Row
function createHeadingRow(headingData: string) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Based on how you are using this function in your code, you should probably rename it to createHeadingColumn.

const heading: HTMLElement | null = document.getElementById("heading");

let headings = `<div class="headings" id="headings">${headingData}</div>`;
if (heading !== null) {
heading.innerHTML += headings;
}
}

// Table Content
function createTableContent(contentData: any) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please specify the type of contentData. If you can use it int a for-loop then you at the very least know its an array.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Based on how you are using this function, you should probably rename it to appendTableContent, or addRow, or appendTableRow.

const content: HTMLElement | null = document.getElementById("content");

let table = `<div id="row-${contentData[0]}" class="rows"></div>`;
if (content !== null) {
content.innerHTML += table;
} else {
alert("ERROR");
}
Comment on lines +54 to +40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You probably also want to return in this else as the rest of your code doesn't really work if the content does not exist.


let rows: HTMLElement | null = document.getElementById("row-" + contentData[0]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should probablly just call this variable row, as it is the element for only one row.

for (let x of contentData) {
let rowCols = `<div class="row_cols">${x}</div>`;
if (rows !== null) {
rows.innerHTML += rowCols;
}
}
}

//// Fetch Requests
// Response Error Handling
function handleErrors(response: Response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}

// Heading Row (Getting the columns data)
function getHeadings(): Promise<void> {
return fetch("http://localhost:2050/columns", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then(handleErrors)
.then((response: Response) => response.json())
.then((data: string) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure how this is a string when you have a for-loop over it in the next few lines...

let headingData = data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just rename data to headingData, no need for this step

for (let heading of headingData) {
createHeadingRow(heading);
}
})
.catch((error: Error) => {
console.log(error);
});
}

// Table Content (Getting the table's data)
function getTable(): Promise<void> {
const content: HTMLElement | null = document.getElementById("content");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think there is a rule about how to use document.getElementById in the Javascript Style Guide.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry man, I just saw that, I'm removing that section from the style guide :P Had a discussion with cobus a while back, and we preferred the more pure route.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No problem!!

let toParameter = getParameters(fromParameter);
const pageStats: HTMLElement | null = document.getElementById("pageStats");

// Clears Table
if (content !== null) {
content.innerHTML = "";
}

// Displays The Current Results Being Shown
let currentStats = `Showing results from ${fromParameter} to ${toParameter} out of ${recordCount} results.`;
if (pageStats !== null) {
pageStats.innerHTML = currentStats;
}

return fetch(`http://localhost:2050/records?from=${fromParameter}&to=${toParameter}`, {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then(handleErrors)
.then((res: Response) => res.json())
.then((data: string) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure how this is a string when you have a for-loop over it in the next few lines...

let contentData = data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just rename data to contentData, no need to have this step.

for (let content of contentData) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You all ready have a variable named content, you should call this something else, or rename the previous one to something else.

createTableContent(content);
}
})
.catch((error: Error) => {
console.log(error);
});
}

// Gets Total Of All Records
function getRecordCount(): Promise<void> {
return fetch("http://localhost:2050/recordCount", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then(handleErrors)
.then((res: Response) => res.json())
.then((data: number) => {
recordCount = data;
})
.catch((error: Error) => {
console.log(error);
});
}

//// Debounce
function debounce(fn: any, delay: number) {
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
fn();
}, delay);
};
}

//// Sizing And Resizing
function resizing() {
let end = fromParameter + getNoOfRows();
let toParameter: number;
let maxRecordsID = recordCount - 1;

if (end > maxRecordsID) {
toParameter = maxRecordsID;
fromParameter = toParameter - getNoOfRows();
}
getTable();
}

//// Navigation
// Next
class Next {
nextCount: number;
nextButton: HTMLElement | null;

constructor() {
this.nextCount = 0;
this.nextButton = document.getElementById("next");

const nextDebounce = (fn: any, delay: number) => {
return () => {
this.nextCount++;
clearTimeout(timer);
timer = setTimeout(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should add this timer (with a different name) to the class, then you are less depended on the global variables.

fn();
}, delay);
};
};

let next = () => {
let toParameter = getParameters(fromParameter);
let maxRecordsID = recordCount - 1;

if (toParameter === maxRecordsID) {
alert("You have reached the final page");
}

let nextAmount = toParameter - fromParameter + 1;
let nextCountAmount = nextAmount * this.nextCount;
fromParameter = fromParameter + nextCountAmount;
toParameter = fromParameter + getNoOfRows();

let end = fromParameter + getNoOfRows();

if (end > maxRecordsID) {
toParameter = maxRecordsID;
fromParameter = toParameter - getNoOfRows();
}

this.nextCount = 0;

getTable();
};

if (this.nextButton) {
this.nextButton.addEventListener("click", nextDebounce(next, 500));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you are dead set on debouncing this button, then you should rather just call the next function here, and put your debounce inside the next function on the getTable function. Then you can get rid of the nextCount logic

}
}
}

// Previous
class Prev {
prevCount: number;
prevButton: HTMLElement | null;

constructor() {
this.prevCount = 0;
this.prevButton = document.getElementById("prev");

const prevDebounce = (fn: any, delay: number) => {
return () => {
this.prevCount++;
clearTimeout(timer);
timer = setTimeout(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same comment about the timer I made in the Next class

fn();
}, delay);
};
};
Comment on lines +206 to +208

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you should rather change this to not return a function, that way you don't have to have that weird () on line 231.


let prev = () => {
let toParameter = getParameters(fromParameter);

if (fromParameter === 0) {
alert("You Are On The First Page");
} else {
let prevAmount = toParameter - fromParameter + 1;
let prevCountAmount = prevAmount * this.prevCount;

let intOne = fromParameter - prevCountAmount;

if (intOne < 0) {
fromParameter = 0;
} else {
fromParameter = intOne;
}

this.prevCount = 0;

getTable();
}
};

if (this.prevButton) {
this.prevButton.addEventListener("click", prevDebounce(prev, 500));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same comment about the debounce that I made on the Next class, should help you get rid of the prevCount.

}
}
}

// ID Jump
function idJump() {
const input: any = document.querySelector("input");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please don't use any.

let toParameter = getParameters(fromParameter);
let currentID = fromParameter;
let search = input.value;
let end = parseInt(search) + getNoOfRows();
let maxRecordsID = recordCount - 1;

if (search !== NaN && search !== "" && search <= maxRecordsID && search >= 0) {
if (end > maxRecordsID) {
toParameter = maxRecordsID;
fromParameter = toParameter - getNoOfRows();
} else {
fromParameter = parseInt(search);
toParameter = fromParameter + getNoOfRows();
}
} else if (search !== "") {
alert("Make Sure Your Desired ID Is Not A Negative Number Or Doesn't Exceed 999999");
fromParameter = currentID;
toParameter = fromParameter + getNoOfRows();
input.value = "";
}

getTable();
}
25 changes: 21 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
<!DOCTYPE html>
<html>
<html lang="en">

<head>
<title>JS Onboard Project</title>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Onboarding JavaScript Task</title>
<script type="text/javascript" charset="utf-8" src="third_party/jquery-2.0.3.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>

<body>
<p>Hello</p>
<div id="nav">
<label id="pageStats"></label>
<div id="jumpID">
<input type="number" id="idJump" placeholder="Input A Starting ID" />
<!-- <button>Jump to ID</button> -->
</div>
<div id="btns">
<button class="prev" id="prev">Prev</button>
<button class="next" id="next">Next</button>
</div>
</div>
<div id="heading"></div>
<div id="content"></div>
</body>

</html>

28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "onboard-javascript",
"version": "1.0.0",
"description": "This is a JavaScript project for all new developers to complete before venturing into our web frontend codebase.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc --build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Koumori97/onboard-javascript.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Koumori97/onboard-javascript/issues"
},
"homepage": "https://github.com/Koumori97/onboard-javascript#readme",
"dependencies": {
"typescript": "^4.6.3"
},
"devDependencies": {
"@types/jquery": "^3.5.14"
}
}
Loading