-
Notifications
You must be signed in to change notification settings - Fork 36
Ashton Martin #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Ashton Martin #45
Changes from 28 commits
3d76748
fc17d76
f1e2d5e
abf2e25
236ae3e
78d3646
20f8768
25982e1
d5a0150
27e37ab
8e1c83c
28c0127
c0cc0dd
0a5e24a
22ac146
d868815
a25fa90
ed88ede
fdc8503
891521b
9c4f823
2240555
ca85f76
9c05769
e991ebf
b085bdd
921241f
a58c56d
19aeeb5
78f5400
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /node_modules | ||
| app.js | ||
| app.js.map | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,319 @@ | ||
| let fromNumber = 0; | ||
| let recordNumberTotal: number; | ||
| let count = 0; | ||
| let timeout = 0; | ||
|
|
||
| window.onload = () => { | ||
| createNavigation(); | ||
| recordCount(); | ||
| headingRowCreation(); | ||
| fromNumber = 0; | ||
| new pageNavigation(); | ||
| }; | ||
|
|
||
| function checkResponseError(response: Response) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please specify a return type.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump |
||
| if (!response.ok) { | ||
| throw Error(response.statusText); | ||
| } | ||
| return response; | ||
| } | ||
|
|
||
| function debounce(func: any, delay: number) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please specify the type of |
||
| 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): Promise<void> { | ||
| return fetch(`http://localhost:2050/records?from=${fromNumber}&to=${toNumber}`, { | ||
| method: "GET", | ||
| headers: { "Content-Type": "application/json" }, | ||
| }) | ||
| .then(checkResponseError) | ||
| .then((response: Response) => response.json()) | ||
| .then((data: string) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how this is a |
||
| let infoColumns: HTMLElement | null = document.getElementById("info-columns-container"); // Information | ||
| let currentPage: HTMLElement | null = document.getElementById("current-page"); | ||
|
|
||
| if (infoColumns !== null) { | ||
| (infoColumns as HTMLDivElement).innerHTML = ""; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have to hard cast to |
||
|
|
||
| for (let i of data) { | ||
| dynamicGrid(i); | ||
| } | ||
| } | ||
|
|
||
| if (currentPage !== null) { | ||
| (currentPage as HTMLParagraphElement).innerHTML = `${fromNumber} / ${toNumber}.`; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the comment before, please remove the hard cast. |
||
| } | ||
| }) | ||
| .catch((error: Error) => { | ||
| console.log(error); | ||
| }); | ||
| } | ||
|
|
||
| function recordSelection() { | ||
| let recordNav: HTMLElement | null = document.getElementById("record-navigation-container"); // Navigation area | ||
| if (!recordNav) { | ||
| alert("The navigation is not working correctly refresh the page"); | ||
| } else { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a |
||
| 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" 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>`; | ||
|
|
||
| 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"); | ||
|
|
||
| if (returnBtn !== null) { | ||
| // Resets to the first page | ||
| returnBtn.addEventListener("click", () => { | ||
| createNavigation(); | ||
| fromNumber = 0; | ||
| getRecords(fromNumber, fromNumber + numberOfRows); | ||
| }); | ||
| } | ||
|
|
||
| if (getSingleRecord !== null) { | ||
| getSingleRecord.addEventListener("click", () => { | ||
| let recordIdValue = (recordIdInput as HTMLInputElement).value; | ||
| fromNumber = Number(recordIdValue); | ||
|
|
||
| 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: string) { | ||
| let headingColumns: HTMLElement | null = document.getElementById("column-headings-container"); // Headings | ||
| let headingsData = `<h1 class="column-heading">${headings}</h1>`; | ||
|
|
||
| if (headingColumns !== null) { | ||
| (headingColumns as HTMLDivElement).innerHTML += headingsData; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to hard cast after you have checked the |
||
| } | ||
| } | ||
|
|
||
| function recordCount(): Promise<void> { | ||
| return fetch("http://localhost:2050/recordCount", { | ||
| method: "GET", | ||
| headers: { "Content-Type": "application/json" }, | ||
| }) | ||
| .then(checkResponseError) | ||
| .then((response: Response) => response.json()) | ||
| .then((data: number) => { | ||
| recordNumberTotal = data; | ||
| }) | ||
| .catch((error: Error) => { | ||
| console.log(error); | ||
| }); | ||
| } | ||
|
|
||
| function headingRowCreation(): Promise<void> { | ||
| return fetch("http://localhost:2050/columns", { | ||
| method: "GET", | ||
| headers: { "Content-Type": "application/json" }, | ||
| }) | ||
| .then(checkResponseError) | ||
| .then((response: Response) => response.json()) | ||
| .then((data: string) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how |
||
| for (let i of data) { | ||
| createHeadingGrid(i); | ||
| } | ||
| resizeScreenData(); | ||
| }) | ||
| .catch((error: Error) => { | ||
| console.log(error); | ||
| }); | ||
| } | ||
|
|
||
| function dynamicGrid(columnData: string) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how |
||
| 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) { | ||
| infoColumns.innerHTML += infoDataRow; | ||
| // Gets the created rows. | ||
| let finalInfoDataRow = document.getElementById("info-row-" + columnData[0]); | ||
| if (finalInfoDataRow !== null) { | ||
| // Loops through | ||
| for (let x of columnData) { | ||
| let infoData = `<p class="info-row-data">${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) { | ||
| if (recordNav.contains(navBtns as HTMLDivElement)) { | ||
| let numberOfRows = Math.floor(window.innerHeight / 50); | ||
|
|
||
| (nextBtn as HTMLButtonElement).disabled = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should check if |
||
| (previousBtn as HTMLButtonElement).disabled = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should check if |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener("resize", debounce(resizeScreenData, 500)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not have this is global scope. |
||
|
|
||
| class pageNavigation { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Classes need to be PascalCase |
||
| nextBtn: HTMLElement | null; | ||
| previousBtn: HTMLElement | null; | ||
| firstPageBtn: HTMLElement | null; | ||
| lastPageBtn: HTMLElement | null; | ||
| confirmationBtn: HTMLElement | null; | ||
|
|
||
| constructor() { | ||
| this.nextBtn = document.getElementById("next-records-btn"); | ||
| this.previousBtn = document.getElementById("previous-records-btn"); | ||
| this.firstPageBtn = document.getElementById("first-page-btn"); | ||
| this.lastPageBtn = document.getElementById("last-page-btn"); | ||
| this.confirmationBtn = document.getElementById("confirmation-btn"); | ||
|
|
||
| if (this.confirmationBtn !== null) { | ||
| this.confirmationBtn.addEventListener("click", recordSelection); | ||
| } | ||
|
|
||
| if (this.nextBtn !== null && this.previousBtn !== null && this.firstPageBtn !== null && this.lastPageBtn !== null) { | ||
| let nextPage = () => { | ||
| let numberOfRows = Math.floor(window.innerHeight / 50); | ||
| fromNumber = fromNumber + numberOfRows * count; | ||
| let toNumber = fromNumber + numberOfRows; | ||
|
|
||
| let finalRecord = recordNumberTotal - 1; | ||
|
|
||
| (this.previousBtn as HTMLButtonElement).disabled = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have already checked for the |
||
|
|
||
| if (toNumber >= finalRecord) { | ||
| (this.nextBtn as HTMLButtonElement).disabled = true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as before about not hard casting. |
||
| fromNumber = finalRecord - numberOfRows; | ||
| } | ||
|
|
||
| getRecords(fromNumber, toNumber); | ||
| count = 0; | ||
| }; | ||
| this.nextBtn.addEventListener("click", () => { | ||
| count++; | ||
| nextPage = debounce(nextPage, 500); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If your deadset on debouncing this functionality, then you should debounce the |
||
| nextPage(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You definitely want to remove this line. |
||
| }); | ||
|
|
||
| let previousPage = () => { | ||
| let numberOfRows = Math.floor(window.innerHeight / 50); | ||
| fromNumber = fromNumber - numberOfRows * count; | ||
| let toNumber = fromNumber + numberOfRows; | ||
| (this.nextBtn as HTMLButtonElement).disabled = false; | ||
|
|
||
| if (fromNumber <= 0) { | ||
| (this.previousBtn as HTMLButtonElement).disabled = true; | ||
| fromNumber = 0; | ||
| } | ||
|
|
||
| getRecords(fromNumber, toNumber); | ||
| count = 0; | ||
| }; | ||
| this.previousBtn.addEventListener("click", () => { | ||
| count++; | ||
| previousPage = debounce(previousPage, 500); | ||
| previousPage(); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as I made on |
||
|
|
||
| let firstPage = () => { | ||
| fromNumber = 0; | ||
| let numberOfRows = Math.floor(window.innerHeight / 50); | ||
| let toNumber = fromNumber + numberOfRows; | ||
| (this.nextBtn as HTMLButtonElement).disabled = false; | ||
| (this.previousBtn as HTMLButtonElement).disabled = true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about the other buttons, you have already checked the |
||
|
|
||
| getRecords(fromNumber, toNumber); | ||
| }; | ||
| this.firstPageBtn.addEventListener("click", () => { | ||
| firstPage(); | ||
| }); | ||
|
|
||
| let lastPage = () => { | ||
| let finalRecord = recordNumberTotal - 1; | ||
| let numberOfRows = Math.floor(window.innerHeight / 50); | ||
| fromNumber = finalRecord - numberOfRows; | ||
| (this.nextBtn as HTMLButtonElement).disabled = true; | ||
| (this.previousBtn as HTMLButtonElement).disabled = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous comment about these two buttons. |
||
|
|
||
| getRecords(fromNumber, finalRecord); | ||
| }; | ||
| this.lastPageBtn.addEventListener("click", () => { | ||
| lastPage(); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,18 @@ | ||||||||||||||
| <!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> | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather keep this to one line.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump |
||||||||||||||
| <link rel="stylesheet" href="./style.css" /> | ||||||||||||||
| </head> | ||||||||||||||
| <body> | ||||||||||||||
| <div id="record-navigation-container"></div> | ||||||||||||||
| <div id="column-headings-container"></div> | ||||||||||||||
| <div id="info-columns-container"></div> | ||||||||||||||
| </body> | ||||||||||||||
| <script src="./app.js"></script> | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line needs to remain in your |
||||||||||||||
| </html> | ||||||||||||||
|
|
||||||||||||||
| 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" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should create the functions and classes before you use them (They should appear before the
window.onloadline)