Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions project-table/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions project-table/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
35 changes: 35 additions & 0 deletions project-table/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "project-table",
"version": "0.1.0",
"private": true,
"dependencies": {
"cra-template": "1.2.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-scripts": "5.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
66 changes: 66 additions & 0 deletions project-table/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.app-container {
font-family: Arial, sans-serif;
margin: 20px;
text-align: center;
}

.projects-table {
width: 80%;
margin: 0 auto;
border-collapse: collapse;
border: 1px solid #ddd;
margin-bottom: 20px;
}

.projects-table th,
.projects-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}

.projects-table th {
background-color: #f4f4f4;
font-weight: bold;
}

.projects-table tr:nth-child(even) {
background-color: #f9f9f9;
}

.projects-table tr:hover {
background-color: #f1f1f1;
}

.pagination-list {
list-style: none;
padding: 0;
display: flex;
justify-content: center;
}

.pagination-list li {
margin: 0 5px;
}

.pagination-list button {
border: none;
background: #007bff;
color: white;
padding: 5px 10px;
cursor: pointer;
border-radius: 5px;
}

.pagination-list .active button {
background: #0056b3;
}

.pagination-list button:hover {
background: #0056b3;
}

.pagination-list button:disabled {
background: #d6d6d6;
cursor: not-allowed;
}
75 changes: 75 additions & 0 deletions project-table/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { useState, useEffect } from "react";
import "./App.css";
import Pagination from "./Pagination";

const App = () => {
const [projects, setProjects] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [isLoading, setIsLoading] = useState(true);
const recordsPerPage = 5;

useEffect(() => {
const fetchProjects = async () => {
try {
const response = await fetch(
"https://raw.githubusercontent.com/saaslabsco/frontend-assignment/refs/heads/master/frontend-assignment.json"
);
if (!response.ok) {
throw new Error("Failed to fetch data");
}
const data = await response.json();
setProjects(data);
} catch (error) {
console.error("Error fetching projects:", error);
} finally {
setIsLoading(false);
}
};

fetchProjects();
}, []);

if (isLoading) {
return <p>Loading...</p>;
}

const indexOfLastRecord = currentPage * recordsPerPage;
const indexOfFirstRecord = indexOfLastRecord - recordsPerPage;
const currentRecords = projects?.slice(indexOfFirstRecord, indexOfLastRecord);
const totalPages = Math.ceil(projects?.length / recordsPerPage);

const handlePageChange = (pageNumber) => {
setCurrentPage(pageNumber);
};

return (
<div className="app-container">
<h1>Projects status table</h1>
<table className="projects-table" role="table">
<thead>
<tr>
<th scope="col">S.No.</th>
<th scope="col">Percentage Funded</th>
<th scope="col">Amount Pledged</th>
</tr>
</thead>
<tbody>
{currentRecords?.map((project, index) => (
<tr key={index}>
<td>{indexOfFirstRecord + index + 1}</td>
<td>{project["percentage.funded"]}</td>
<td>${project["amt.pledged"].toLocaleString()}</td>
</tr>
))}
</tbody>
</table>
<Pagination
totalPages={totalPages}
currentPage={currentPage}
onPageChange={handlePageChange}
/>
</div>
);
};

export default App;
80 changes: 80 additions & 0 deletions project-table/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
import App from "./App";
import Pagination from "./Pagination";

// Mocking the fetch API
beforeEach(() => {
global.fetch = jest.fn(() =>
Promise.resolve({
json: () =>
Promise.resolve([
{
"s.no": 0,
"amt.pledged": 15823,
"percentage.funded": 186,
},
{
"s.no": 1,
"amt.pledged": 6859,
"percentage.funded": 8,
},
]),
})
);
});

afterEach(() => {
jest.restoreAllMocks();
});


// test("renders loading message while fetching data", async () => {
// render(<App />);
// const loadingMessage = screen.getByText(/Loading/i);
// expect(loadingMessage).toBeInTheDocument();

// // Wait for data to be fetched
// await waitFor(() => {
// const heading = screen.getByText(/Projects status table/i);
// expect(heading).toBeInTheDocument();
// });
// });



// test("renders table with correct columns", async () => {
// render(<App />);
// const loadingMessage = screen.getByText(/Loading/i);
// expect(loadingMessage).toBeInTheDocument();

// await waitFor(() => {
// const column1 = screen.getByText(/S.No./i);
// const column2 = screen.getByText(/Percentage Funded/i);
// const column3 = screen.getByText(/Amount Pledged/i);

// expect(column1).toBeInTheDocument();
// expect(column2).toBeInTheDocument();
// expect(column3).toBeInTheDocument();
// });
// });

test("renders pagination buttons", () => {
const onPageChange = jest.fn();
render(<Pagination totalPages={5} currentPage={1} onPageChange={onPageChange} />);

const prevButton = screen.getByText(/Prev/i);
const nextButton = screen.getByText(/Next/i);

expect(prevButton).toBeInTheDocument();
expect(nextButton).toBeInTheDocument();
});

test("calls onPageChange when clicking a page button", () => {
const onPageChange = jest.fn();
render(<Pagination totalPages={5} currentPage={1} onPageChange={onPageChange} />);

const page2Button = screen.getByText("2");
fireEvent.click(page2Button);

expect(onPageChange).toHaveBeenCalledWith(2);
});
29 changes: 29 additions & 0 deletions project-table/src/Pagination.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.pagination-list {
display: flex;
justify-content: center;
margin: 20px 0;
}
.pagination-list li {
margin: 0 5px;
}
.pagination-list button {
padding: 8px 12px;
border-radius: 5px;
border: 1px solid #007bff;
background: #007bff;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
.pagination-list .active button {
background: #0056b3;
cursor: default;
}
.pagination-list button:hover {
background-color: #0056b3;
}
.pagination-list button:disabled {
background: #d6d6d6;
cursor: not-allowed;
}

Loading