Skip to content

Completed assignment Keerthi C #20

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "saaslabs",
"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"
]
},
"devDependencies": {
"web-vitals": "^4.2.4"
}
}
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
73 changes: 73 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

import './App.css';
import Table from './components/table/index.tsx';
import {theadData} from './mockData/mockData.js';
import {useEffect, useState} from 'react';
import { getService } from './services/http.ts';
import {returnDataWrtTableContent} from './utils/helpers.ts';


function App() {

// console.log(data);
const [tbodyData, settbodyData] = useState([]);
const [startIndex, setStartIndex] = useState(0);
const [endIndex, setendIndex] = useState(5);
const [loading, setLoading] = useState(false);
const [tableData, setTbaldata] = useState([]);

const callAPI = async ()=>{
setLoading(true);
const resp = await getService('https://raw.githubusercontent.com/saaslabsco/frontend-assignment/refs/heads/master/frontend-assignment.json');
const {data, pageSize} = returnDataWrtTableContent(resp);
settbodyData([...data]);
setLoading(false);
setTbaldata([...resp])
}

useEffect(()=>{
callAPI();
},[])

const handlePrevPage =()=>{
setStartIndex(startIndex - 5);
setendIndex(endIndex - 5);
}

const handleNextPage =()=>{
setStartIndex(endIndex);
setendIndex(endIndex + 5);
}

useEffect(()=>{
const data = returnDataWrtTableContent(tableData, startIndex, endIndex);
settbodyData([...data.data]);
console.log(startIndex, endIndex);

},[startIndex, endIndex])

function Pagination (){

return (
<div>
<button onClick={()=>handlePrevPage()} disabled = {startIndex <= 0}>Prev</button>
<button onClick={()=>handleNextPage()} disabled = {endIndex >= tableData.length}>Next</button>
</div>
)
}


return (
<div className="App">
<span style={{fontSize: '16px', fontWeight: '600'}}>SaasLabs Assignment</span>
<Table
theadData = {theadData}
tbodyData = {tbodyData}
loading = {loading}
/>
<Pagination />
</div>
);
}

export default App;
20 changes: 20 additions & 0 deletions src/components/table/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#tbl {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}


#tbl td, #tbl th {
border: 1px solid #ddd;
padding: 8px;
}


#tbl th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #04AA6D;
color: white;
}
51 changes: 51 additions & 0 deletions src/components/table/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import TableRow from "./tableRow/index.tsx";
import TableHeader from "./tableHeader/index.tsx";
import './index.css'

type tbodyType = {
"s.no": number;
"amt.pledged": number;
blurb: string;
by: string;
country: string;
currency: string;
"end.time": string;
location: string;
"percentage.funded": number;
"num.backers": string;
state: string;
title: string;
type: string;
url: string;
};

interface tableProps {
theadData: string[];
tbodyData: tbodyType[];
loading: boolean
}

const Table = (props: tableProps) => {
const { theadData = [], tbodyData = [], loading } = props;
return (
<table id="tbl">
<thead>
<tr>
{theadData.map((h) => {
return <TableHeader key={h} item={h} />;
})}
</tr>
</thead>
<tbody>
{
loading ? <div>Loading ...</div> : tbodyData.map((item) => {
return <TableRow key={item.id} data={item.items} loading={loading} />;
})}

</tbody>
</table>
);
};

export default Table;
8 changes: 8 additions & 0 deletions src/components/table/tableHeader/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render, screen } from '@testing-library/react';
import TableHeader from './index.tsx';

test('render table header with correct title', () => {
render(<TableHeader item={'S.No.'} />);
const textEle = screen.getByText(/S.No./i);
expect(textEle).toBeInTheDocument();
});
11 changes: 11 additions & 0 deletions src/components/table/tableHeader/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

const TableHeader = ({ item }) => {
return (
<td style = {{fontWeight: 600}}title={item}>
{item}
</td>
);
};

export default TableHeader;
9 changes: 9 additions & 0 deletions src/components/table/tableRow/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { render, screen } from '@testing-library/react';
import TableRow from './index.tsx';

test('render table body with correct title', () => {
const mock = [1,2,3]
render(<TableRow data={mock} />);
const textEle = screen.getByText(/1/i);
expect(textEle).toBeInTheDocument();
});
28 changes: 28 additions & 0 deletions src/components/table/tableRow/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import React from "react";

type dataObj = {
id: number,
items: number[]
}

interface props{
data: dataObj,
loading: boolean,
}

const TableRow = (props: props) => {
const { data = [] } = props;
return (
<>
<tr>
{data?.map((item) => {
return <td key={item}>{item}</td>;
})}
</tr>

</>
);
};

export default TableRow;
13 changes: 13 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
1 change: 1 addition & 0 deletions src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading