Skip to content
Open
Show file tree
Hide file tree
Changes from 18 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
261 changes: 261 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
let headingColumns: any = document.querySelector("#column-headings-container"); // Headings
let infoColumns: any = document.querySelector("#info-columns-container"); // Information

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 way your code is written these lines will execute before those elements actually exist on the webpage. So they can definitely not be here. You should only try and get things from the webpage after it has been rendered.

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.

Also change the any to the actual type.

let fromNumber: number = 0;
let count: number = 0;
let timeout: number = 0;

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

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

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

let createNavigation = () => {

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 change this variable to a normal function, seeing as that you never override the value.

let recordNav: any = document.querySelector("#record-navigation-container"); // Navigation area
recordNav.innerHTML = `
<div class="navigation-btns">
<button value="first" class="first-page-btn">First Page</button>
<button value="previous" class="previous-records-btn">Previous</button>
<button value="next" class="next-records-btn">Next</button>
<button value="last" class="last-page-btn">Last Page</button>
<button onclick="recordSelection()" id="confirmation-btn">Get 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.

You should not specify your onclick in the HTML. You should do it in the typescript code.

</div>
<div class="current-page-container">
<p class=current-page></p>
</div>
`;
};

function recordSelection() {
let selectionArea: any = document.querySelector("#record-navigation-container");
let singleRecordSelection = `
<button id="return-btn">Return</button>
<div id="user-input-data">
<div class="navigation-input-area-id" id="id">
<label class="record-labels" for="record-id"
>Enter record ID :
</label>
<input
type="text"
min="0"
minlength="1"
maxlength="6"

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.

Hardcoding the max length here is a bad idea, as the maximum amount of rows that you could get is unknown.

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>
`;

selectionArea.innerHTML = "";
selectionArea.innerHTML = singleRecordSelection;

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 clear it since you are anyway assigning it a new value that overwrites the previous one?


let returnBtn: any = document.querySelector("#return-btn");
let recordIdValue: any = document.querySelector("#record-id.navigation-input");
let numberOfRows = Math.floor(window.innerHeight / 50);

returnBtn.addEventListener("click", () => {
createNavigation();
let currentPage: any = document.querySelector(".current-page");
currentPage.innerHTML = `${fromNumber} / ${fromNumber + numberOfRows}.`;
});

let getSingleRecord: any = document.querySelector("#get-record-btn");

getSingleRecord.addEventListener("click", () => {
fromNumber = Number(recordIdValue.value);
let toNumber = fromNumber + numberOfRows;
let check = ["undefined", "string", ""];

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

Tell me what's wrong? Error what?

}
});
}

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 headingsData: any = `<h1 class="column-heading">${headings}</h1>`;
headingColumns.innerHTML += headingsData;
}

let 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 change this variable into a normal function, as you don't seem to be overriding the variable anywhere.

fetch("http://localhost:2050/columns", {
method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((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 check if response has an error here.

.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 () around the parameters or specify the types, as the compiler currently thinks they are of type any.

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

for (let i = 0; i < headingDataList.length; i++) {
headings = headingDataList[i];
createHeadingGrid(headings);
}
resizeScreenData();
})
.catch((error) => {

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 () around the error parameter or specify the type, as the compiler currently thinks it is of type any.

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.

// 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>`;
infoColumns.innerHTML += infoDataRow;
// Gets the created rows.
let finalInfoDataRow: any = document.querySelector("#info-row-" + columnData[0] + ".info-rows");

// Loops through
for (let x = 0; x < columnData.length; x++) {
let infoData = `<p class="info-row-data">${columnData[x]}</p>`;
finalInfoDataRow.innerHTML += infoData;
}
}

let resizeScreenData = () => {
let toNumber: number;
let selectionArea: any = document.querySelector("#record-navigation-container");

if (selectionArea.contains(document.querySelector(".navigation-btns"))) {
let numberOfRows = Math.floor(window.innerHeight / 50);

nextBtn.disabled = false;
previousBtn.disabled = false;

if (fromNumber + numberOfRows >= 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.

I only noticed now you are working on a hardcoded value for the max recordCount (999999). Please ask the API once for that value and store it. You can then use it accordingly instead of the working of a hardcoded values.

let toNumber = 999999;
fromNumber = toNumber - numberOfRows;
nextBtn.disabled = true;
previousBtn.disabled = false;
} else if (fromNumber <= 0) {
nextBtn.disabled = false;
previousBtn.disabled = true;
fromNumber = 0;
}

toNumber = fromNumber + numberOfRows;

getRecords(fromNumber, toNumber);
}
};

function getRecords(fromNumber: number, toNumber: number) {
fetch("http://localhost:2050/records?from=" + fromNumber + "&to=" + toNumber, {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's make this Template literals.

method: "GET",
headers: { "Content-Type": "application/json" },
})
.then((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 check if response has an error here.

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

Add a catch to log and handle errors.

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 () around the parameters or specify the types, as the compiler currently thinks they are of type any.

let columnDataList = JSON.parse(data);
infoColumns.innerHTML = "";
for (let i = 0; i < columnDataList.length; i++) {
dynamicGrid(columnDataList[i]);
}

let currentPage: any = document.querySelector(".current-page");
currentPage.innerHTML = `${fromNumber} / ${toNumber}.`;
})
.catch((error) => {

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 () around the error parameter or specify the type, as the compiler currently thinks it is of type any.

console.log(error);
});
}

resizeScreenData = debounce(resizeScreenData, 100);
window.addEventListener("resize", resizeScreenData);
createNavigation();
headingRowCreation();

let nextBtn: any = document.querySelector(".next-records-btn");
let previousBtn: any = document.querySelector(".previous-records-btn");
let firstPageBtn: any = document.querySelector(".first-page-btn");
let lastPageBtn: any = document.querySelector(".last-page-btn");

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

if (toNumber >= 999999) {
toNumber = 999999;
fromNumber = toNumber - numberOfRows;
nextBtn.disabled = true;
previousBtn.disabled = false;
}

getRecords(fromNumber, toNumber);
count = 0;
};

nextBtn.addEventListener("click", () => {
count++;
nextPage = debounce(nextPage, 100);
nextPage();
});

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

if (fromNumber <= 0) {
nextBtn.disabled = false;
previousBtn.disabled = true;
fromNumber = 0;
}

getRecords(fromNumber, toNumber);
count = 0;
};

previousBtn.addEventListener("click", () => {
count++;
previousPage = debounce(previousPage, 100);
previousPage();
});

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

getRecords(fromNumber, toNumber);
};

firstPageBtn.addEventListener("click", () => {
firstPage();
});

let lastPage = () => {
let toNumber = 999999;
let numberOfRows = Math.floor(window.innerHeight / 50);
fromNumber = toNumber - numberOfRows;
nextBtn.disabled = true;
previousBtn.disabled = false;

getRecords(fromNumber, toNumber);
};

lastPageBtn.addEventListener("click", () => {
console.log("Hello");

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 fancy logging

lastPage();
});
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"
}
}
2 changes: 0 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ 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

var columns = [columnCount]string{"ID", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be any changes to this file. Looks like you accidentally removed the new lines. Make sure this file doesn't even popup on Files changed on GitHub.

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Comment on lines 15 to 21

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 touch the backend. Frontend only.


Expand Down
Loading