Skip to content

Commit a2bc709

Browse files
committed
Make table controlled component
1 parent b31a73a commit a2bc709

File tree

5 files changed

+103
-76
lines changed

5 files changed

+103
-76
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "my-app",
33
"version": "0.1.0",
44
"private": true,
5-
"homepage": "http://mustkeom.github.io/data-table-with-pagination",
5+
"homepage": "http://mustkem.github.io/data-table-with-pagination",
66
"dependencies": {
77
"axios": "^0.19.0",
88
"bootstrap": "^4.6.0",

src/App.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
1-
import React from 'react';
1+
import React from "react";
22
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
33

4-
import logo from './logo.svg';
4+
import logo from "./logo.svg";
55

6-
import 'bootstrap/dist/css/bootstrap.min.css';
7-
import './App.css';
6+
import "bootstrap/dist/css/bootstrap.min.css";
7+
import "./App.css";
8+
9+
import { appStore } from "./AppStore/store";
10+
import { Provider } from "react-redux";
11+
import Home from "./Components/Home";
12+
import styles from "./Components/table.module.css";
813

9-
import { appStore } from './AppStore/store';
10-
import { Provider } from 'react-redux';
11-
import TableView from './Components/TableView';
1214

1315
function App() {
1416
return (
1517
<div className="App">
1618
<Provider store={appStore}>
1719
<Router>
18-
<header className="App-header">
19-
<img src={logo} className="App-logo" alt="logo" />
20-
<p>
21-
Data Table
22-
</p>
23-
</header>
20+
<h3 className={`${styles.container} ${styles.heading}`}>Data Table</h3>
2421
<div className="content">
2522
<Switch>
26-
<Route path="/" exact component={TableView} />
27-
<Route path="/user/:id" component={() => (<div className="container">
28-
User Page
29-
<div>
30-
Mock url for user is not working.
31-
https://demo9197058.mockable.io/users/1
32-
33-
</div>
34-
<div>
35-
I will implement this page once it will start working. Leveing it for now.
36-
</div>
37-
</div>)} />
23+
<Route path="/" exact component={Home} />
24+
<Route
25+
path="/user/:id"
26+
component={() => <div className="container">User Page</div>}
27+
/>
3828
</Switch>
39-
4029
</div>
4130
</Router>
4231
</Provider>

src/Components/Home.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { useEffect } from "react";
2+
import { connect } from "react-redux";
3+
import styles from "./table.module.css";
4+
5+
import TableComponent from "./Table";
6+
7+
import { getList } from "../AppStore/actions";
8+
9+
const tableHeader = {
10+
id: "User ID",
11+
name: "Name",
12+
email: "Email",
13+
username: "Username",
14+
website: "Website",
15+
phone: "Phone",
16+
city: "City",
17+
company: "Company",
18+
};
19+
20+
const Home = (props) => {
21+
const { getList, userList } = props;
22+
23+
useEffect(() => {
24+
getList();
25+
}, [getList]);
26+
27+
return (
28+
<div className={styles.container}>
29+
<TableComponent
30+
tableHeader={tableHeader}
31+
userList={userList}
32+
itemsPerPage={5}
33+
/>
34+
</div>
35+
);
36+
};
37+
38+
const mapStateToProps = (state) => {
39+
return {
40+
userList: state.tableListReducer.userList || [],
41+
};
42+
};
43+
44+
const mapDispatchToProps = (dispatch) => {
45+
return {
46+
getList: () => dispatch(getList()),
47+
};
48+
};
49+
50+
export default connect(mapStateToProps, mapDispatchToProps)(Home);

src/Components/TableView.js renamed to src/Components/Table.js

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
import React, { useState, useEffect } from "react";
2-
import { connect } from "react-redux";
32
import Table from "react-bootstrap/Table";
43

5-
import { getList } from "../AppStore/actions";
64
import TableItem from "./tableItem";
75
import Pagination from "../Shared/Pagination";
86
import styles from "./table.module.css";
97

10-
const tableHeader = {
11-
id: "User ID",
12-
name: "Name",
13-
email: "Email",
14-
username: "Username",
15-
website: "Website",
16-
phone: "Phone",
17-
city: "City",
18-
company: "Company",
19-
};
20-
218
const TableView = (props) => {
22-
const { getList, userList } = props;
9+
const { userList, tableHeader, itemsPerPage } = props;
10+
2311
const [searchQuery, setSearchQuery] = useState("");
2412
const [items, setItems] = useState([]);
2513
const [page, setPage] = useState(1);
2614
const [sortKey, setSortKey] = useState("");
27-
const itemsPerPage = 5;
28-
29-
useEffect(() => {
30-
getList();
31-
}, [getList]);
3215

3316
useEffect(() => {
3417
setInitialTableState();
@@ -103,16 +86,16 @@ const TableView = (props) => {
10386
});
10487

10588
return (
106-
<div className={styles.container}>
107-
<div className="table-view">
108-
<div className={styles.headerContainer}>
109-
<input
110-
placeholder="Search by Name"
111-
value={searchQuery}
112-
onChange={handleSearch}
113-
/>
114-
<button onClick={setInitialTableState}>Reset</button>
115-
</div>
89+
<div className={styles.tableView}>
90+
<div className={styles.headerContainer}>
91+
<input
92+
placeholder="Search by Name"
93+
value={searchQuery}
94+
onChange={handleSearch}
95+
/>
96+
<button onClick={setInitialTableState}>Reset</button>
97+
</div>
98+
{items.length > 0 && (
11699
<Table>
117100
<thead>
118101
<tr>
@@ -148,27 +131,18 @@ const TableView = (props) => {
148131
))}
149132
</tbody>
150133
</Table>
151-
<Pagination
152-
page={page}
153-
totalItems={userList.length}
154-
itemsPerPage={itemsPerPage}
155-
handleChange={handleChangePage}
156-
/>
157-
</div>
134+
)}
135+
{items.length === 0 && (
136+
<div className={styles.noRecord}>No record Found</div>
137+
)}
138+
<Pagination
139+
page={page}
140+
totalItems={userList.length}
141+
itemsPerPage={itemsPerPage}
142+
handleChange={handleChangePage}
143+
/>
158144
</div>
159145
);
160146
};
161147

162-
const mapStateToProps = (state) => {
163-
return {
164-
userList: state.tableListReducer.userList || [],
165-
};
166-
};
167-
168-
const mapDispatchToProps = (dispatch) => {
169-
return {
170-
getList: () => dispatch(getList()),
171-
};
172-
};
173-
174-
export default connect(mapStateToProps, mapDispatchToProps)(TableView);
148+
export default TableView;

src/Components/table.module.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,17 @@
1818
display: flex;
1919
justify-content: space-between;
2020
}
21+
22+
.noRecord{
23+
padding:20px;
24+
margin:20px;
25+
}
26+
27+
.heading{
28+
padding:10px;
29+
}
30+
31+
.tableView{
32+
padding: 15px;
33+
box-shadow: 0px 1px 5px 0px #b3b3b3;
34+
}

0 commit comments

Comments
 (0)