Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ad2404b
update
ThokozaniNqwili Aug 11, 2023
575b160
update
ThokozaniNqwili Aug 11, 2023
038f93e
update
ThokozaniNqwili Aug 15, 2023
30785ac
update
ThokozaniNqwili Aug 18, 2023
96682be
update
ThokozaniNqwili Aug 23, 2023
e287191
update
ThokozaniNqwili Aug 25, 2023
b4e7f57
format change
ThokozaniNqwili Aug 28, 2023
1f6c3a3
review changes
ThokozaniNqwili Aug 28, 2023
eef44e5
update requested changes
ThokozaniNqwili Aug 29, 2023
097c7c1
update requested changes, index and style
ThokozaniNqwili Aug 29, 2023
3e38dff
update search
ThokozaniNqwili Aug 29, 2023
3c23cb2
first push 30/08
ThokozaniNqwili Aug 30, 2023
d8792ca
onclick
ThokozaniNqwili Aug 30, 2023
55837eb
last push 30/08
ThokozaniNqwili Aug 30, 2023
10b0d40
09/05
ThokozaniNqwili Sep 5, 2023
11ea6c4
update
ThokozaniNqwili Sep 5, 2023
2b53415
update
ThokozaniNqwili Sep 5, 2023
c15fe4c
09/06
ThokozaniNqwili Sep 6, 2023
90d744b
09/08
ThokozaniNqwili Sep 8, 2023
2361a84
11/09
ThokozaniNqwili Sep 11, 2023
eeb1c81
12/09
ThokozaniNqwili Sep 12, 2023
9580c60
15/09
ThokozaniNqwili Sep 15, 2023
cc6a0dd
15/09
ThokozaniNqwili Sep 15, 2023
f18872b
18/09
ThokozaniNqwili Sep 18, 2023
24aba76
18/09
ThokozaniNqwili Sep 18, 2023
2e7e23d
26/09
ThokozaniNqwili Sep 26, 2023
0d8a9d4
update
ThokozaniNqwili Sep 26, 2023
2313df4
update
ThokozaniNqwili Sep 27, 2023
817249c
09/28
ThokozaniNqwili Sep 28, 2023
cb7c3ea
10/02
ThokozaniNqwili Oct 2, 2023
f8204e9
updates
ThokozaniNqwili Oct 2, 2023
249611b
03/10
ThokozaniNqwili Oct 3, 2023
7a4b0e2
04/10
ThokozaniNqwili Oct 5, 2023
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
199 changes: 199 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { ajax, css } from "jquery";
var firstNumber: number = 0;
var lastNumber: 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.

would be nice to have a default

var calculatedRows: number | null = null;
var count: number | null = null;
Comment thread
ThokozaniNqwili marked this conversation as resolved.
Outdated

async function fetchRecordCount(): Promise<number> { // fetches the number of records from the server
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
const data = await recordCount.json()
const data = JSON.parse(recordCount)

return data;
} catch (error) {
console.error('Error fetching the record count:', error);
throw error;
}
}

function fetchColumns(): void { //fetches the different columns headings from server
fetch("http://localhost:2050/columns")
.then((response: Response) => {
return response.json() as Promise<string[]>;
})
.then((columns: string[]) => {
const colArray = columns; // assigns the columns to an array

for (let c = 0; c < colArray.length; c++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

use for..of loop please

$(".head").append(`<th>${colArray[c]}</th>`); // creates a single column for each heading
}
})
}
fetchColumns()

async function adjustRowsByScreenHeight(): Promise<number> { // calculates the number of rows that can fit the screen

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is this an async?

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 made a mistake because of my understanding, I will work on it.

const screenHeight = window.innerHeight; // calculates screen height
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; // returns the maximum rows that can fit into screen
}
}
Comment thread
FritzOnFire marked this conversation as resolved.
Outdated

let resizeTimeout: 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.

Another global variable

$(window).on('resize', async () => {
clearTimeout(resizeTimeout); // cancel previously schedule Timeout

resizeTimeout = setTimeout(async () => {
$('#loader').show()
calculatedRows = await adjustRowsByScreenHeight(); // to recalcualte the number of max rows
await displayRecords()

let inputValue = $('#searchInput').val(); // priotizes the search input value if available
if (inputValue !== '') {
await updateRecordsAndResize(Number(inputValue))
}
$('#loader').hide();
}, 500);
})

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.

Missing ;


async function fetchRecords(from: number, to: number): Promise<any[]> { // fetches records from specified number to a certain number
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> { //displays records from firstNumber to lastNumber
try {
$('#loader').show()
count = await fetchRecordCount() - 1;
calculatedRows = await adjustRowsByScreenHeight();
const inputValue = $('#searchInput').val() as number;
if (calculatedRows === 0) {
lastNumber = firstNumber

Comment thread
FritzOnFire marked this conversation as resolved.
}
if (!lastNumber) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this and the else does exactly the same

lastNumber = firstNumber + (calculatedRows - 1);
}
if (firstNumber < 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.

else if and move in the same line as }

firstNumber = 0;
lastNumber = firstNumber + (calculatedRows - 1);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

move the else on the same line as the } please

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()

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 space

}


Comment thread
FritzOnFire marked this conversation as resolved.
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

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 spaces around


}
tbody.append(lastRow);
}
} catch (error) {
console.error("Error displaying records:", error);
}
}
displayRecords()

async function updateRecordsAndResize(inputValue: number) { // calulates the range of records according to the search input
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;
}
calculatedRows = await 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;
await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked

});

async function rightArrow(): Promise<void> { // moves records to the next list
$('#searchInput').val('')
const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row
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
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> { // moves records to the previous list
$('#searchInput').val('')
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) => {

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 make use of a for..of loop, even a for loop is fine, but for of is preferred

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();
}
}
}
43 changes: 42 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">&times;</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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

can move script tag into the head tag

</body>

</html>
Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
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"
}
}
Loading