-
Notifications
You must be signed in to change notification settings - Fork 36
Updated Project #47
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?
Updated Project #47
Changes from 8 commits
ad2404b
575b160
038f93e
30785ac
96682be
e287191
b4e7f57
1f6c3a3
eef44e5
097c7c1
3e38dff
3c23cb2
d8792ca
55837eb
10b0d40
11ea6c4
2b53415
c15fe4c
90d744b
2361a84
eeb1c81
9580c60
cc6a0dd
f18872b
24aba76
2e7e23d
0d8a9d4
2313df4
817249c
cb7c3ea
f8204e9
249611b
7a4b0e2
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,183 @@ | ||||||
| import { ajax, css } from "jquery"; | ||||||
| let firstNumber: number = 0; | ||||||
| let lastNumber: number = 0; | ||||||
|
|
||||||
|
|
||||||
| async function fetchRecordCount(): Promise<number> { | ||||||
| try { | ||||||
| const recordCount = await fetch(`http://localhost:2050/recordCount`); | ||||||
| if (!recordCount.ok) { | ||||||
| throw new Error('Failed to fetch record count'); | ||||||
| } | ||||||
| const data = await recordCount.json() | ||||||
|
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.
Suggested change
|
||||||
| return data; | ||||||
| } catch (error) { | ||||||
| console.error('Error fetching the record count:', error); | ||||||
| throw error; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function fetchColumns(): void { | ||||||
|
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.
Suggested change
|
||||||
| fetch("http://localhost:2050/columns") | ||||||
| .then((response: Response) => { | ||||||
| return response.json() as Promise<string[]>; | ||||||
| }) | ||||||
| .then((columns: string[]) => { | ||||||
| const colArray = columns; | ||||||
|
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. theres no catch |
||||||
|
|
||||||
| for (let c = 0; c < colArray.length; c++) { | ||||||
|
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. use |
||||||
| $(".head").append(`<th>${colArray[c]}</th>`); | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
| fetchColumns() | ||||||
|
|
||||||
| function adjustRowsByScreenHeight() { | ||||||
|
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 use the |
||||||
| const screenHeight = window.innerHeight; | ||||||
| const availableHeight = screenHeight - 105 // subtracts the space used from the screeen | ||||||
| let rowHeight = 35 | ||||||
| if (availableHeight <= 0) { | ||||||
| return 0; | ||||||
| } else { | ||||||
| let maxRows = Math.floor(availableHeight / rowHeight); | ||||||
| return maxRows; | ||||||
| } | ||||||
| } | ||||||
|
FritzOnFire marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| $(window).on('resize', async () => { | ||||||
| let resizeTimeout: number; | ||||||
| resizeTimeout = setTimeout(async () => { | ||||||
| $('#loader').show() | ||||||
| await displayRecords() | ||||||
| let inputValue = $('#searchInput').val(); // priotizes the search input value if available | ||||||
| if (inputValue !== '') { | ||||||
| await updateRecordsAndResize(Number(inputValue)) | ||||||
| } | ||||||
| $('#loader').hide(); | ||||||
| }, 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. Missing |
||||||
|
|
||||||
| async function fetchRecords(from: number, to: number): Promise<any[]> { | ||||||
| const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); | ||||||
| if (!response.ok) { | ||||||
| throw new Error("Sorry, there's a problem with the network"); | ||||||
| } | ||||||
| return response.json(); | ||||||
| } | ||||||
|
|
||||||
| async function displayRecords(): Promise<void> { | ||||||
| try { | ||||||
| $('#loader').show() | ||||||
| let count = await fetchRecordCount() - 1; | ||||||
|
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 shouldn't have to make the backend calls the whole time, call once and save then in variables to be reused 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. still not done |
||||||
| let calculatedRows = adjustRowsByScreenHeight(); | ||||||
| const inputValue = $('#searchInput').val() as number; | ||||||
| if (calculatedRows === 0) { | ||||||
| lastNumber = firstNumber | ||||||
| } | ||||||
| if (firstNumber < 0) { | ||||||
|
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.
|
||||||
| firstNumber = 0; | ||||||
| lastNumber = firstNumber + (calculatedRows - 1); | ||||||
| } else { | ||||||
| lastNumber = firstNumber + (calculatedRows - 1); | ||||||
| } | ||||||
| let records; | ||||||
| if (lastNumber <= count && lastNumber >= 0) { | ||||||
| records = await fetchRecords(firstNumber, lastNumber); | ||||||
| $('#page').empty(); | ||||||
| $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); // changes the record range showing | ||||||
| $('#loader').hide() | ||||||
| } else { | ||||||
| firstNumber = count - (calculatedRows - 1) | ||||||
| lastNumber = count; | ||||||
| records = await fetchRecords(firstNumber, count); | ||||||
| $('#page').empty(); | ||||||
| $('#page').append(`Showing record: ${firstNumber} - ${count}`); | ||||||
| $('#loader').hide() | ||||||
| } | ||||||
| const tbody = $("tbody"); | ||||||
| tbody.empty(); | ||||||
| for (let r = 0; r < records.length; r++) { | ||||||
| $("tbody").append(`<tr class="row"></tr>`); // creates row for each record | ||||||
| const lastRow = $(".row:last"); | ||||||
| for (let i = 0; i < records[r].length; i++) { | ||||||
| lastRow.append(`<td>${records[r][i]}</td>`); //assign each record to their column in a specified row | ||||||
| } | ||||||
| if (records[r].includes(inputValue)) { | ||||||
| lastRow.css('background-color', '#DDC0B4'); // hightlights the searched row | ||||||
|
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. remove spaces around |
||||||
| } | ||||||
| tbody.append(lastRow); | ||||||
| } | ||||||
| } catch (error) { | ||||||
| console.error("Error displaying records:", error); | ||||||
| } | ||||||
| } | ||||||
| displayRecords() | ||||||
|
|
||||||
| async function updateRecordsAndResize(inputValue: 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 add a return type to this function. |
||||||
| let count = await fetchRecordCount() - 1; | ||||||
| if (inputValue < 0 || inputValue > count) { // check if the search input | ||||||
| $('.modal').css('display', 'block') //opens modal if search input is not within range | ||||||
| $('.modal-content').append(`<p>${inputValue} is not a number within the range.Please try a different number</p>`) | ||||||
| $('#searchInput').val(''); // empties search bar | ||||||
| return; | ||||||
| } | ||||||
| let calculatedRows = adjustRowsByScreenHeight(); | ||||||
| const quarterRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half | ||||||
| firstNumber = Math.max(0, inputValue - quarterRange); | ||||||
| lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); | ||||||
| await displayRecords(); | ||||||
| } | ||||||
|
|
||||||
| $('#closeModalBtn').on("click", () => { | ||||||
| $('.modal').css('display', 'none') // closes modal | ||||||
| }); | ||||||
|
|
||||||
| $('.btnSearch').on('click', async (event: any) => { | ||||||
| event.preventDefault(); | ||||||
| let inputValue = $('#searchInput').val() as 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 use |
||||||
| await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked | ||||||
| }); | ||||||
|
|
||||||
| async function rightArrow(): Promise<void> { | ||||||
| $('#searchInput').val('') | ||||||
| const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row | ||||||
| let count = await fetchRecordCount() - 1; | ||||||
| if (lastRow) { // checks if the last row exists | ||||||
| const cells = lastRow.querySelectorAll("td"); | ||||||
| const lastRecord: string[] = []; | ||||||
| cells.forEach((cell) => { | ||||||
| lastRecord.push(cell.textContent || ""); | ||||||
| }); | ||||||
| const lastID = parseFloat(lastRecord[0]); // determines te value in the last row | ||||||
| if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range | ||||||
| const tbody = $("tbody"); | ||||||
| tbody.empty(); // empties the table | ||||||
| firstNumber = lastID + 1; // calculates the first number of the page | ||||||
| let calculatedRows = await adjustRowsByScreenHeight(); | ||||||
| lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page | ||||||
| await displayRecords(); // display the new records | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| async function leftArrow(): Promise<void> { | ||||||
| $('#searchInput').val('') | ||||||
| let count = await fetchRecordCount() - 1; | ||||||
| const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row | ||||||
| if (firstRow) { // checks if the first row exists | ||||||
| const cells = firstRow.querySelectorAll("td"); | ||||||
| const firstRecord: string[] = []; | ||||||
| cells.forEach((cell) => { | ||||||
|
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 make use of a |
||||||
| firstRecord.push(cell.textContent || ""); | ||||||
| }); | ||||||
| const firstID = parseFloat(firstRecord[0]); // determines te value in the first row | ||||||
| if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range | ||||||
| const tbody = $("tbody"); | ||||||
| tbody.empty(); // empties the table | ||||||
| const calculatedRows = await adjustRowsByScreenHeight(); | ||||||
| lastNumber = firstID - 1; // calculates the last number of the page | ||||||
| firstNumber = lastNumber - (calculatedRows - 1) // uses the last number to calculate | ||||||
| await displayRecords(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,51 @@ | |
| <head> | ||
| <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"> | ||
| </head> | ||
|
|
||
| <body> | ||
| <p>Hello</p> | ||
| <div id="loader" ></div> | ||
| <h1 class="heading">OnBoard-Javascript</h1> | ||
| <div id="modal" class="modal"> | ||
| <div class="modal-content"> | ||
| <span id="closeModalBtn" class="close">×</span> | ||
| <p></p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="search-container"> | ||
| <form id="searchForm"> | ||
| <input type="number" id="searchInput" placeholder="Search.." name="search" autocomplete="off"> | ||
| <button class="btnSearch" type="submit">Submit</button> | ||
| </form> | ||
| </div> | ||
|
|
||
| <div class="showRecords"> | ||
| <button onclick="rightArrow()" class="arrow-right"></button> | ||
| <div id="page"></div> | ||
| <button onclick="leftArrow()" class="arrow-left"></button> | ||
| </div> | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| <table id="recordsTable"> | ||
| <thead> | ||
| <tr class="head"> | ||
| </tr> | ||
| </thead> | ||
|
|
||
| <tbody> | ||
|
|
||
| </tbody> | ||
| </table> | ||
|
|
||
|
|
||
| <script src="app.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. can move |
||
| </body> | ||
|
|
||
| </html> | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "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 -p .", | ||
| "start": "npm run build -- -w" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/ThokozaniNqwili/onboard-javascript.git" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "bugs": { | ||
| "url": "https://github.com/ThokozaniNqwili/onboard-javascript/issues" | ||
| }, | ||
| "homepage": "https://github.com/ThokozaniNqwili/onboard-javascript#readme", | ||
| "devDependencies": { | ||
| "typescript": "^3.8.3" | ||
| }, | ||
| "dependencies": { | ||
| "@types/jquery": "^3.5.16" | ||
| } | ||
| } |
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.
No need to set the variable and the type, either give a type, or set it