Skip to content
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

Pagination #28

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
2 changes: 1 addition & 1 deletion src/API/APICalls.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import instance from './apiwrapper';

export const getEmployees = (sort, direction) => instance.get('/api/employee', { params: { sort: `${sort},${direction}` } });
export const getEmployees = (page, sort, direction) => instance.get('/api/employee', { params: { page, sort: `${sort},${direction}` } });

export const getEmployee = employeeId => instance.get(`/api/employee/${employeeId}`);

Expand Down
44 changes: 31 additions & 13 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { useState, useEffect } from 'react';
import { Card, Divider } from 'semantic-ui-react';
import { Card, Divider, Pagination } from 'semantic-ui-react';
import EmployeeCard from './components/Card';
import EmployeeForm from './components/EmployeeForm';
import Message from './components/SuccessMessage';
import logo from './logo.svg';
import './App.css';
import { getEmployees, addEmployee } from './API/APICalls';
import Sort from './components/Sort';
import { CentredDiv } from './components/Styled';
import PlaceholderCard from './components/PlaceholderCard';

const App = () => {
// add new employee form attributes
Expand All @@ -22,6 +24,9 @@ const App = () => {
const [state, setState] = useState(false);
const [actionType, setActionType] = useState('');

// State hooks for pagination
const [totalPages, setTotalPages] = useState(0);

const mapper = {
fnInput: setfn,
lnInput: setln,
Expand All @@ -43,15 +48,19 @@ const App = () => {
mapper[evt.target.name](value);
};

const getAllPersons = (sort = 'createdAt', direction = 'asc') => getEmployees(sort, direction)
.then((res) => {
console.log('/getEmployee response:', res.data);
setPersons(res.data.content);
})
.catch((error) => {
setPersons([]);
console.log(error);
});
const getAllPersons = (page = 0, sort = 'createdAt', direction = 'asc') => {
setPersons([]);
getEmployees(page, sort, direction)
.then((res) => {
console.log('/getEmployee response:', res.data);
setPersons(res.data.content);
setTotalPages(res.data.totalPages);
})
.catch((error) => {
setPersons([]);
console.log(error);
});
};

const handleSubmit = () => {
console.log('in submit:', employee);
Expand All @@ -70,6 +79,10 @@ const App = () => {
});
};

const handlePageChange = (event, data) => {
getAllPersons(data.activePage - 1);
};

useEffect(() => {
getAllPersons();
}, []);
Expand All @@ -85,10 +98,15 @@ const App = () => {
<Sort refresh={getAllPersons} />
</Divider>
<Card.Group>
{persons.map(person => (
<EmployeeCard refresh={getAllPersons} key={person.id} person={person} />
))}
{persons.length !== 0
? persons.map(person => (
<EmployeeCard refresh={getAllPersons} key={person.id} person={person} />
))
: [...Array(16).keys()].map(() => <PlaceholderCard />)}
</Card.Group>
<CentredDiv>
<Pagination defaultActivePage={1} onPageChange={handlePageChange} totalPages={totalPages} />
</CentredDiv>
</div>
);
};
Expand Down
27 changes: 27 additions & 0 deletions src/components/PlaceholderCard.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { Card, Placeholder } from 'semantic-ui-react';

const PlaceholderCard = () => (
<Card>
<Card.Content>
<Card.Header>
<Placeholder>
<Placeholder.Header>
<Placeholder.Line />
</Placeholder.Header>
</Placeholder>
</Card.Header>
<Placeholder>
<Placeholder.Paragraph>
<Placeholder.Line length="medium" />
<Placeholder.Line length="short" />
<Placeholder.Line length="medium" />
<Placeholder.Line length="short" />
<Placeholder.Line length="short" />
</Placeholder.Paragraph>
</Placeholder>
</Card.Content>
</Card>
);

export default PlaceholderCard;
9 changes: 8 additions & 1 deletion src/components/Styled.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import styled from 'styled-components';
import {
Form, Button, Segment,
Form,
Button,
Segment,
} from 'semantic-ui-react';

/* eslint-disable import/prefer-default-export */
Expand Down Expand Up @@ -38,3 +40,8 @@ export const StyledSegment = styled(Segment)`
font-style: italic;
}
`;

export const CentredDiv = styled.div`
text-align: center;
padding: 10px;
`;