Skip to content

Commit 4fb4322

Browse files
Updated from Links to Posts based on new API specs
1 parent fd795b8 commit 4fb4322

20 files changed

+207
-274
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ In the project directory, you can run:
66

77
### `yarn start`
88

9-
Runs the app in the development mode.<br />
9+
Run the app in the development mode.<br />
1010
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
1111

1212
### `yarn test`
1313

14-
Launches the test runner in the interactive watch mode.
14+
Launch the test runner in the interactive watch mode.
1515

1616
### `yarn build`
1717

src/App.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react";
22
import {Route, Switch, Redirect} from "react-router-dom";
3-
import Links from "./pages/links/Links";
3+
import Posts from "./pages/posts/Posts";
44
import Login from "./pages/login/Login";
55
import Registration from "./pages/registration/Registration";
66
import NavBar from "./components/layout/NavBar";
7-
import NewLink from "./pages/links/NewLink";
7+
import NewPost from "./pages/posts/NewPost";
88
import NotFound from "./pages/error/NotFound";
99

1010
const App = () => (
@@ -14,10 +14,10 @@ const App = () => (
1414
<Switch>
1515
<Route path="/login" component={Login}/>
1616
<Route path="/registration" component={Registration}/>
17-
<Route path="/links/new" component={NewLink}/>
17+
<Route path="/posts/new" component={NewPost}/>
1818

19-
<Route path="/links" component={Links} />
20-
<Redirect exact path="/" to="/links"/>
19+
<Route path="/posts" component={Posts} />
20+
<Redirect exact path="/" to="/posts"/>
2121
<Route path="*">
2222
<NotFound />
2323
</Route>

src/components/layout/NavBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const NavBar = () => {
2525
if (user.access_token) {
2626
authenticatedLinks = (<Nav className="" navbar>
2727
<NavItem>
28-
<NavLink href="/links/new">
29-
<i className="fas fa-plus-circle"/> Add Link
28+
<NavLink href="/posts/new">
29+
<i className="fas fa-plus-circle"/> Add Post
3030
</NavLink>
3131
</NavItem>
3232

src/components/links/Link.test.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/components/links/Link.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/components/links/LinkList.tsx

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/components/links/TagNav.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/components/links/Pagination.test.tsx renamed to src/components/posts/Pagination.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import Pagination from './Pagination';
4-
import {LinksModel, LinksPaginationModel} from "../../models/LinkModels";
4+
import {PostsModel, PostsPaginationModel} from "../../models/PostModels";
55
import {MemoryRouter} from "react-router-dom";
66

77
test('Renders Pagination', () => {
8-
const linksModel: LinksModel = {
8+
const postsModel: PostsModel = {
99
data: [],
1010
hasNext: false,
1111
hasPrevious: false,
@@ -15,7 +15,7 @@ test('Renders Pagination', () => {
1515
totalPages: 2,
1616
totalElements: 50
1717
}
18-
const paginationModel : LinksPaginationModel= { page:1, links:linksModel, tag: '', query: ''};
18+
const paginationModel : PostsPaginationModel= { page:1, posts:postsModel, query: ''};
1919

2020
render(<MemoryRouter><Pagination {...paginationModel}/></MemoryRouter>);
2121
const firstLinkElement = screen.getByText(/First/i);

src/components/links/Pagination.tsx renamed to src/components/posts/Pagination.tsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
11
import React from "react";
22
import classNames from "classnames";
33
import {NavLink} from "react-router-dom";
4-
import {LinksPaginationModel} from "../../models/LinkModels";
4+
import {PostsPaginationModel} from "../../models/PostModels";
55

6-
const Pagination: React.FC<LinksPaginationModel> = (linksPagination) : JSX.Element => {
7-
let links = linksPagination.links
8-
let firstPageUrl = "/links?page=1";
9-
let prevPageUrl = `/links?page=${links.pageNumber-1}`;
10-
let nextPageUrl = `/links?page=${links.pageNumber+1}`;
11-
let lastPageUrl = `/links?page=${links.totalPages}`;
6+
const Pagination: React.FC<PostsPaginationModel> = (postsPagination) : JSX.Element => {
7+
let posts = postsPagination.posts
8+
let firstPageUrl = "/posts?page=1";
9+
let prevPageUrl = `/posts?page=${posts.pageNumber-1}`;
10+
let nextPageUrl = `/posts?page=${posts.pageNumber+1}`;
11+
let lastPageUrl = `/posts?page=${posts.totalPages}`;
1212

13-
if(linksPagination.tag) {
14-
firstPageUrl += `&tag=${linksPagination.tag}`;
15-
prevPageUrl += `&tag=${linksPagination.tag}`;
16-
nextPageUrl += `&tag=${linksPagination.tag}`;
17-
lastPageUrl += `&tag=${linksPagination.tag}`;
18-
}
19-
20-
if(linksPagination.query) {
21-
firstPageUrl += `&query=${linksPagination.query}`;
22-
prevPageUrl += `&query=${linksPagination.query}`;
23-
nextPageUrl += `&query=${linksPagination.query}`;
24-
lastPageUrl += `&query=${linksPagination.query}`;
13+
if(postsPagination.query) {
14+
firstPageUrl += `&query=${postsPagination.query}`;
15+
prevPageUrl += `&query=${postsPagination.query}`;
16+
nextPageUrl += `&query=${postsPagination.query}`;
17+
lastPageUrl += `&query=${postsPagination.query}`;
2518
}
2619

2720
return (
2821
<div>
29-
{ links.totalPages > 1 &&
22+
{ posts.totalPages > 1 &&
3023
<nav aria-label="Page navigation">
3124
<ul className="pagination pagination justify-content-center">
3225
<li className={classNames({
3326
"page-item": true,
34-
disabled: !links.hasPrevious
27+
disabled: !posts.hasPrevious
3528
})}>
3629
<NavLink className="page-link" to={`${firstPageUrl}`}>First</NavLink>
3730
</li>
3831
<li className={classNames({
3932
"page-item": true,
40-
disabled: !links.hasPrevious
33+
disabled: !posts.hasPrevious
4134
})}>
4235
<NavLink className="page-link" to={`${prevPageUrl}`}>Previous</NavLink>
4336
</li>
4437
<li className={classNames({
4538
"page-item": true,
46-
disabled: !links.hasNext
39+
disabled: !posts.hasNext
4740
})}>
4841
<NavLink className="page-link" to={`${nextPageUrl}`}>Next</NavLink>
4942
</li>
5043
<li className={classNames({
5144
"page-item": true,
52-
disabled: !links.hasNext
45+
disabled: !posts.hasNext
5346
})}>
5447
<NavLink className="page-link" to={`${lastPageUrl}`}>Last</NavLink>
5548
</li>

src/components/posts/Post.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import Post from './Post';
4+
import { PostModel} from "../../models/PostModels";
5+
import { MemoryRouter } from 'react-router-dom';
6+
7+
test('Renders Post', () => {
8+
const post : PostModel= {id: 1, title: 'sivalabs', url: 'https://sivalabs.in', content: 'sivalabs blog'};
9+
10+
render(<MemoryRouter><Post {...post}/></MemoryRouter>);
11+
const textElements = screen.getAllByText(/sivalabs/i);
12+
expect(textElements.length).toBeGreaterThan(0)
13+
});

src/components/posts/Post.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react";
2+
import {PostModel} from "../../models/PostModels";
3+
4+
const Post: React.FC<PostModel> = (post) => {
5+
6+
return (
7+
<div className="alert alert-primary" role="alert">
8+
<h3 className="alert-heading">
9+
<a href={post.url} target="_blank" rel="noopener noreferrer">{post.title}</a>
10+
</h3>
11+
<p className="mb-0">
12+
{post.content}
13+
</p>
14+
</div>
15+
);
16+
}
17+
18+
export default Post;

src/components/posts/PostList.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from "react";
2+
import Post from "./Post";
3+
import {PostsModel} from "../../models/PostModels";
4+
5+
const PostList: React.FC<PostsModel> = (links) => {
6+
7+
return (
8+
<div>
9+
{links.data.map(link => <Post key={link.id} {...link}/>)}
10+
</div>
11+
);
12+
};
13+
14+
export default PostList;

src/components/links/Search.tsx renamed to src/components/posts/Search.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ const Search : React.FC<SearchProps>= ( props ) => {
1111
<div className={'pb-2'}>
1212
<form className="row g-3 align-items-center">
1313
<div className="col">
14-
<input className="col-md-12 form-control" type="search" name="query" placeholder="Search for"
14+
<input className="col-md-12 form-control"
15+
type="search"
16+
name="query"
17+
placeholder="Enter search keyword"
1518
value={query}
1619
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setQuery(e.target.value)}/>
1720
</div>
1821
<div className="col-auto">
19-
<button className="btn btn-primary btn" type="button" onClick={(e) => props.ClickHandler(query)}>Search</button>
22+
<button className="btn btn-primary btn"
23+
type="button"
24+
onClick={(e) => props.ClickHandler(query)}>
25+
Search
26+
</button>
2027
</div>
2128
</form>
2229
</div>
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
export interface LinkModel {
2+
export interface PostModel {
33
id: number,
44
title: string,
55
url: string,
6-
tags: string[]
6+
content: string
77
}
88

9-
export interface LinksModel {
10-
data: LinkModel[],
9+
export interface PostsModel {
10+
data: PostModel[],
1111
totalElements: number,
1212
pageNumber: number,
1313
totalPages: number,
@@ -17,9 +17,8 @@ export interface LinksModel {
1717
hasPrevious: boolean;
1818
}
1919

20-
export interface LinksPaginationModel {
21-
tag?: string
20+
export interface PostsPaginationModel {
2221
query?: string
2322
page?: number
24-
links: LinksModel
23+
posts: PostsModel
2524
}

0 commit comments

Comments
 (0)