Skip to content
Open
Show file tree
Hide file tree
Changes from 26 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
277 changes: 277 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
// Function To Get Number Of Rows That Can Be Displayed While Still Being Readable

const getNoOfRows = () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add return type

const height = window.innerHeight;

let number = height / 40;
let noOfRows = Math.floor(number);
return noOfRows;
};

// Variables

let paramOne = 0;
let paramTwo = paramOne + getNoOfRows();

//// Functions To Create/Clear The HTML

// Heading Row

const 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.

Rather remove the empty line between the variable and the comment.

const heading: any = document.querySelector("#heading");

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 only place you can have an any in the project is when handling data that comes from the back-end. And even then you can actually use the real type instead of any.


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

// Table Content

const createTableContent = (contentData: string) => {
const content: any = document.querySelector("#content");

let table = `<div id="row-${contentData[0]}" class="rows"></div>`;
content.innerHTML += table;

let rows: any = document.querySelector("#row-" + contentData[0] + ".rows");
for (let x = 0; x < contentData.length; x++) {
let rowCols = `<div class="row_cols">${contentData[x]}</div>`;
rows.innerHTML += rowCols;
}
};

// Clear Table Content

const clearTable = () => {
const content: any = document.querySelector("#content");
const clear = "";

content.innerHTML = clear;
};

//// Fetch Requests

// Heading Row (Getting the columns data)

const getHeadings = () => {
try {
fetch("http://localhost:2050/columns", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((res) => res.text())
.then((data) => {
data = JSON.parse(data);
let headingData = data;
for (let i = 0; i < headingData.length; i++) {
createHeadingRow(headingData[i]);
}
});
} catch (error) {
console.log(error);
}

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 meant, a catch on the then of the Promise.
e.g. then( () => {...}).catch( (error) => {})

Not a seperate try and catch block.

};

// Table Content (Getting the table's data)

const getTable = () => {
try {
fetch("http://localhost:2050/records?from=" + paramOne + "&to=" + paramTwo, {

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'd like you to change this function to accept the from and to variables as parameters. We don't like global variables if not necessary.

Also give these variables more descriptive names. Give paramOne and paramTwo names more meaning to what they really are.
A tip: from and to 😉

method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((res) => res.text())
.then((data) => {
data = JSON.parse(data);
let contentData = data;
for (let i = 0; i < contentData.length; i++) {
createTableContent(contentData[i]);
}
});
} catch (error) {
console.log(error);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as the above comment on the catch

};

// Displays The Current Results Being Shown

const stats = () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In general, this function should not have to do a recordCount API call. The recordCount is constant throughout in this case. You can probably just build your string in here and update it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What I mean by recordCount is constant is that you don't have to make an API call every time you call stats since the value is going to be the same for each call.

In this case, I recommend you create a global const variable that you assign the recordCount API response once on the initial load of your program. At the moment it is a hardcoded value. Let the backend determine it.

const pageStats: any = document.querySelector("#pageStats");

try {
fetch("http://localhost:2050/recordCount", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((res) => res.text())
.then((data) => {
data = JSON.parse(data);
let count = data;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Probably not necessary to do this over 2 lines.

let count = JSON.parse(data);

let currentStats = "Showing results from " + paramOne + " to " + paramTwo + " out of " + count + " results.";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Rather use Template Literals. ${}

pageStats.innerHTML = currentStats;
});
} catch (error) {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as the above comment on the catch

};

//// Debounce

const debounce = (fn: any, delay: number) => {
let timer: number;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You might need to add timer as a global variable. It's something I noticed with Ashton as well and he got it to work. At this stage, timer is only in the scope of this function.

return function () {
clearTimeout(timer);
timer = setTimeout(() => {
fn();
}, delay);
};
};

//// Sizing And Resizing

let resizing = () => {
let end = paramOne + getNoOfRows();

if (end > 999999) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't use 999999. You have recordCount for the max..

paramTwo = 999999;
paramOne = paramTwo - getNoOfRows();
} else {
paramOne;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove redundancy.

paramTwo = paramOne + getNoOfRows();
}
clearTable();
getTable();
stats();

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 can definitely see these 3 functions actually being in one as it always goes together logically. They are always repeated in that order and are not too complex to be split up. It can be all together in getTable()?

};

resizing = debounce(resizing, 500);

window.addEventListener("resize", resizing);

//// On Window Load

window.onload = function () {
getHeadings();
getTable();
stats();
};

//// Navigation

// Next
const nextButton: any = document.querySelector("#next");
let nextCount = 0;

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'm struggling to understand why you need these count variables? They look like they are kinda contradicting the point of the debouncing?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm using it to get the amount of times the button is being clicked so i can move next according to the amount


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

let next = () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The paramOne and paramTwo should be accepted as parameters to this function.

if (paramTwo === 999999) {
alert("You have reached the final page");
}

let nextAmount = paramTwo - paramOne + 1;
let nextCountAmount = nextAmount * nextCount;
paramOne = paramOne + nextCountAmount;
paramTwo = paramOne + getNoOfRows();

let end = paramOne + getNoOfRows();

if (end > 999999) {
paramTwo = 999999;
paramOne = paramTwo - getNoOfRows();
}

nextCount = 0;

clearTable();
getTable();
stats();
};

next = nextDebounce(next, 500);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You should not have to do this. Just pass the nextDebounce in directly to the eventListener in this case.


nextButton.addEventListener("click", next);

// Previous
const prevButton: any = document.querySelector("#prev");
let prevCount = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as the above comment for the next


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

let prev = () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as mentioned in the next.

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

let intOne = paramOne - prevCountAmount;

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

paramTwo = paramOne + getNoOfRows();

prevCount = 0;

clearTable();
getTable();
stats();
}
};

prev = prevDebounce(prev, 500);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same vibe as the above next


prevButton.addEventListener("click", prev);

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

let idJump = () => {
let currentID = paramOne;
let search = input.value;
let end = parseInt(search) + getNoOfRows();

if (search !== NaN && search !== "" && search < 1000000 && search >= 0) {
if (end > 999999) {
paramTwo = 999999;
paramOne = paramTwo - getNoOfRows();
} else {
paramOne = parseInt(search);
paramTwo = paramOne + getNoOfRows();
}
} else if (search === "") {
//pass
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Try to never implement an if-else statement that does nothing. Remove the last else and then you can change your logic for the last if-else like (search !== "") because that is in essence what the logic is for the last else in your case.

alert("Make Sure Your Desired ID Is Not A Negative Number Or Doesn't Exceed 999999");
paramOne = currentID;
paramTwo = paramOne + getNoOfRows();
input.value = "";
}

clearTable();
stats();
getTable();
};

idJump = debounce(idJump, 500);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same vibe as the above next.


window.addEventListener("input", idJump);
36 changes: 25 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>JS Onboard Project</title>
<script type="text/javascript" charset="utf-8" src="third_party/jquery-2.0.3.min.js"></script>
</head>

<body>
<p>Hello</p>
</body>

<html lang="en">
<head>
<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" defer></script>
</head>
<body>
<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