Skip to content
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
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
312 changes: 312 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
let fromNumber = 0;
let recordNumberTotal: number;
let count = 0;
let timeout = 0;

window.onload = () => {
fromNumber = 0;
};

function checkResponseError(response: Response) {

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 a return type.

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

if (!response.ok) {
throw Error(response.statusText);
}
return response;
}

let debounce = (func: any, delay: number) => {
return function () {
clearTimeout(timeout);

timeout = setTimeout(() => {
func();
}, delay);
};
};

function createNavigation() {
let recordNav: HTMLElement | null = document.getElementById("record-navigation-container"); // Navigation area
if (recordNav !== null) {
recordNav.innerHTML = `
<div id="navigation-btns">
<button value="first" id="first-page-btn">First Page</button>
<button value="previous" id="previous-records-btn">Previous</button>
<button value="next" id="next-records-btn">Next</button>
<button value="last" id="last-page-btn">Last Page</button>
<button id="confirmation-btn">Get Record</button>
</div>
<div class="current-page-container">
<p id=current-page></p>
</div>
`;
}
}

function getRecords(fromNumber: number, toNumber: number) {

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 a return type

return fetch(`http://localhost:2050/records?from=${fromNumber}&to=${toNumber}`, {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then(checkResponseError)
.then((response: Response) => response.text())

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 rather use .json(). Then you don't have to parse the result and check if it successfully parsed in the next section.

.then((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.

Either remove the () or specify the type, as the compiler currently thinks the type is any.

let columnDataList = JSON.parse(data);
let infoColumns: HTMLElement | null = document.getElementById("info-columns-container"); // Information

console.log("Hello");

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.

Stray console log.

(infoColumns as HTMLDivElement).innerHTML = "";

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 need to check if infoColumns is null, and handle it appropriately. Instead of hard casting the value.

for (let i = 0; i < columnDataList.length; i++) {

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 change this to a for-of loop seeing as that you are not using the counter.

dynamicGrid(columnDataList[i]);
}

let currentPage: HTMLElement | null = document.getElementById("current-page");
(currentPage as HTMLParagraphElement).innerHTML = `${fromNumber} / ${toNumber}.`;

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 as before, you need to check if the value is null

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

function recordSelection() {
let recordNav: HTMLElement | null = document.getElementById("record-navigation-container"); // Navigation area
if (recordNav !== null) {

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 do a

if (!recordNav) {
    // Error/Return/Alert what happened.
}

let singleRecordSelection = `
<button id="return-btn">Return</button>
<div id="user-input-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.

You should put the button and the div on the same indentation, otherwise it looks like the div is inside the button.

<div class="navigation-input-area-id" id="id">
<label class="record-labels" for="record-id"
>Enter record ID :
</label>
<input
type="text"
name="record-id"
id="record-id"
class="navigation-input"
value="0"
/>
</div>
<p class="amount-of-records"></p>
</div>
<button id="get-record-btn">See Record</button>

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 as before, but here is looks like your closing a button.

`;

recordNav.innerHTML = singleRecordSelection;

let returnBtn: HTMLElement | null = document.getElementById("return-btn");
let recordIdInput: HTMLElement | null = document.getElementById("record-id");
let numberOfRows = Math.floor(window.innerHeight / 50);
let getSingleRecord: HTMLElement | null = document.getElementById("get-record-btn");

returnBtn?.addEventListener("click", () => {

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 ? is nice for when you want to do optional things that don't need to be there. But seeing as that you just made the dom elements, you definitly expect them to be there, so you should check if any of them are null and handle the errors accordingly.

console.log("hello");

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.

Stray console log.

createNavigation();
fromNumber = 0;
let toNumber = fromNumber + numberOfRows;
getRecords(fromNumber, toNumber);

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.

Add a comment saying that you are resetting the grid back to the starting page and remove the let toNumber line and just pass the numberOfRows as an argument to the getRecords function.

});

getSingleRecord?.addEventListener("click", () => {

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 ?

let recordIdValue = (recordIdInput as HTMLInputElement).value;

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.

Check for null

fromNumber = Number(recordIdValue);

console.log(fromNumber);

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.

Stray console log


let toNumber = fromNumber + numberOfRows;
let finalRecord = recordNumberTotal - 1;

if (toNumber > finalRecord) {
toNumber = finalRecord;
fromNumber = toNumber - numberOfRows;
}

let check = ["undefined", "string", ""];

if (check.includes(typeof fromNumber) || fromNumber < 0) {
alert("Does not exists");
recordIdValue = "0";
} else if (typeof fromNumber === "number" && fromNumber >= 0) {
getRecords(fromNumber, toNumber);
}
});
}
}

function createHeadingGrid(headings: 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 real type here.

let headingColumns: HTMLElement | null = document.getElementById("column-headings-container"); // Headings
let headingsData = `<h1 class="column-heading">${headings}</h1>`;

(headingColumns as HTMLDivElement).innerHTML += headingsData;

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.

Check for null

}

function recordCount() {

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 add a return type

return fetch("http://localhost:2050/recordCount", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then(checkResponseError)
.then((response: Response) => response.text())

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 rather use .json() to avoid parsing the json in the next section.

.then((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.

Please remove the () or specify the type, as the compiler currently thinks that the type is any

recordNumberTotal = JSON.parse(data);
})
.catch((error: Error) => {
console.log(error);
});
}

function headingRowCreation() {

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 return type.

return fetch("http://localhost:2050/columns", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then(checkResponseError)
.then((response: Response) => response.text())

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 rather use .json() to avoid parsing the json in the next section

.then((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.

Please remove the () or specify the type, as the compiler currently thinks that the type is any

let headingDataList = JSON.parse(data);
let headings: string;

for (let i = 0; i < headingDataList.length; i++) {

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.

Change this to a for-of loop.

headings = headingDataList[i];
createHeadingGrid(headings);
}
resizeScreenData();
})
.catch((error: Error) => {
console.log(error);
});
}

function dynamicGrid(columnData: 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.

Change the type for columnData to its actual type.

let infoColumns: HTMLElement | null = document.getElementById("info-columns-container"); // Information
// Creates the row that the info will display and adds it to the infoColumnsArea.
let infoDataRow = `<div id="info-row-${columnData[0]}" class="info-rows"></div>`;

if (infoColumns !== null) {

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 probably error or something if this happens, otherwise the user will not know whats wrong.

infoColumns.innerHTML += infoDataRow;
// Gets the created rows.
let finalInfoDataRow = document.getElementById("info-row-" + columnData[0]);
if (finalInfoDataRow !== null) {
// Loops through
for (let x = 0; x < columnData.length; x++) {
let infoData = `<p class="info-row-data">${columnData[x]}</p>`;
finalInfoDataRow.innerHTML += infoData;
}
}
}
}

function resizeScreenData() {
let toNumber: number;
let recordNav: HTMLElement | null = document.getElementById("record-navigation-container");
let nextBtn: HTMLElement | null = document.getElementById("next-records-btn");
let previousBtn: HTMLElement | null = document.getElementById("previous-records-btn");
let navBtns = document.getElementById("navigation-btns");
if (recordNav !== null) {

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 probably error or something if this happens, otherwise the user will not know whats wrong.

if (recordNav.contains(navBtns as HTMLDivElement)) {
let numberOfRows = Math.floor(window.innerHeight / 50);

(nextBtn as HTMLButtonElement).disabled = false;
(previousBtn as HTMLButtonElement).disabled = false;

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 check for nulls.


let finalRecord = recordNumberTotal - 1;

if (fromNumber + numberOfRows >= finalRecord) {
fromNumber = finalRecord - numberOfRows;
(nextBtn as HTMLButtonElement).disabled = true;
(previousBtn as HTMLButtonElement).disabled = false;
} else if (fromNumber <= 0) {
(nextBtn as HTMLButtonElement).disabled = false;
(previousBtn as HTMLButtonElement).disabled = true;
fromNumber = 0;
}

toNumber = fromNumber + numberOfRows;

getRecords(fromNumber, toNumber);
}
}
}

recordCount();
window?.addEventListener("resize", debounce(resizeScreenData, 500));
createNavigation();
headingRowCreation();

{
let confirmationBtn: HTMLElement | null = document.getElementById("confirmation-btn");
confirmationBtn?.addEventListener("click", recordSelection);

let nextBtn: HTMLElement | null = document.getElementById("next-records-btn");
let previousBtn: HTMLElement | null = document.getElementById("previous-records-btn");
let firstPageBtn: HTMLElement | null = document.getElementById("first-page-btn");
let lastPageBtn: HTMLElement | null = document.getElementById("last-page-btn");

let nextPage = () => {
console.log(fromNumber);
let numberOfRows = Math.floor(window.innerHeight / 50);
fromNumber = fromNumber + numberOfRows * count;
let toNumber = fromNumber + numberOfRows;

let finalRecord = recordNumberTotal - 1;

(previousBtn as HTMLButtonElement).disabled = false;

if (toNumber >= finalRecord) {
(nextBtn as HTMLButtonElement).disabled = true;
fromNumber = finalRecord - numberOfRows;
}

getRecords(fromNumber, toNumber);
count = 0;
};
nextBtn?.addEventListener("click", () => {
count++;
nextPage = debounce(nextPage, 500);
nextPage();
});

let previousPage = () => {
let numberOfRows = Math.floor(window.innerHeight / 50);
fromNumber = fromNumber - numberOfRows * count;
let toNumber = fromNumber + numberOfRows;
(nextBtn as HTMLButtonElement).disabled = false;

if (fromNumber <= 0) {
(previousBtn as HTMLButtonElement).disabled = true;
fromNumber = 0;
}

getRecords(fromNumber, toNumber);
count = 0;
};
previousBtn?.addEventListener("click", () => {
count++;
previousPage = debounce(previousPage, 500);
previousPage();
});

let firstPage = () => {
fromNumber = 0;
let numberOfRows = Math.floor(window.innerHeight / 50);
let toNumber = fromNumber + numberOfRows;
(nextBtn as HTMLButtonElement).disabled = false;
(previousBtn as HTMLButtonElement).disabled = true;

getRecords(fromNumber, toNumber);
};
firstPageBtn?.addEventListener("click", () => {
firstPage();
});

let lastPage = () => {
let finalRecord = recordNumberTotal - 1;
let numberOfRows = Math.floor(window.innerHeight / 50);
fromNumber = finalRecord - numberOfRows;
(nextBtn as HTMLButtonElement).disabled = true;
(previousBtn as HTMLButtonElement).disabled = false;

getRecords(fromNumber, finalRecord);
};
lastPageBtn?.addEventListener("click", () => {
lastPage();
});
}

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.

This is to much logic happening in global space, please either move it into a class, or have it executed somewhere else.

21 changes: 11 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<!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>

<head>

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 convert this file back to using tabs.

<title>JS Onboard Project</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>

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 not really be using defer for this project... but there aren't any rules against it, so I can't ask you to remove it.

</head>
<body>
<div id="record-navigation-container"></div>
<div id="column-headings-container"></div>
<div id="info-columns-container"></div>
</body>
</html>

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"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/AshtonMar/onboard-javascript.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/AshtonMar/onboard-javascript/issues"
},
"homepage": "https://github.com/AshtonMar/onboard-javascript#readme",
"dependencies": {
"@types/jquery": "^3.5.14"
}
}
1 change: 0 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strconv"
"time"
)

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.

This file should not show up in this PR.

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 recordCount = 1000000
const columnCount = 11
const delayResponse = 500 * time.Millisecond
Expand Down
Loading