├── src
├── sorting
│ └── sort.js
├── assets
│ └── hn_512.png
├── selctionFields
│ └── selctionFields.js
├── components
│ ├── SubmitForm.js
│ ├── Navbar.css
│ ├── NewsCard.js
│ └── Navbar.js
├── App.js
├── setupTests.js
├── App.test.js
├── reportWebVitals.js
├── index.js
├── services
│ └── hnapi.js
├── Story.js
├── mappers
│ └── mapTime.js
├── navigation
│ └── Routes.js
├── containers
│ └── StoriesContainer.js
└── App.css
├── public
├── robots.txt
├── favicon.ico
├── index.html
└── manifest.json
├── .gitignore
├── package.json
└── README.md
/src/sorting/sort.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/abhijith365/Hacker-News-clone/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/src/assets/hn_512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/abhijith365/Hacker-News-clone/HEAD/src/assets/hn_512.png
--------------------------------------------------------------------------------
/src/selctionFields/selctionFields.js:
--------------------------------------------------------------------------------
1 | export const selectFields = ({ id, by, url, time, title } = {}) => ({
2 | id,
3 | by,
4 | url,
5 | time,
6 | title,
7 | });
--------------------------------------------------------------------------------
/src/components/SubmitForm.js:
--------------------------------------------------------------------------------
1 |
2 | import React from 'react'
3 |
4 | export default function SubmitForm() {
5 | return (
6 |
7 |
404
8 |
9 | )
10 | }
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import './App.css';
2 | import Routes from './navigation/Routes';
3 |
4 |
5 | function App() {
6 | return (
7 |
8 |
9 |
10 |
11 |
12 |
13 | );
14 | }
15 |
16 | export default App;
17 |
--------------------------------------------------------------------------------
/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import { render, screen } from '@testing-library/react';
2 | import App from './App';
3 |
4 | test('renders learn react link', () => {
5 | render();
6 | const linkElement = screen.getByText(/learn react/i);
7 | expect(linkElement).toBeInTheDocument();
8 | });
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | Hacker News Clone
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 | import reportWebVitals from './reportWebVitals';
5 |
6 | ReactDOM.render(
7 |
8 |
9 | ,
10 | document.getElementById('root')
11 | );
12 |
13 | // If you want to start measuring performance in your app, pass a function
14 | // to log results (for example: reportWebVitals(console.log))
15 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
16 | reportWebVitals();
17 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/src/services/hnapi.js:
--------------------------------------------------------------------------------
1 | import axios from 'axios';
2 |
3 | import { selectFields } from '../selctionFields/selctionFields';
4 |
5 | export const baseUrl = 'https://hacker-news.firebaseio.com/v0/';
6 | export const newStoriesUrl = `${baseUrl}newstories.json`;
7 | export const storyUrl = `${baseUrl}item/`;
8 |
9 | export const getStory = async storyId => {
10 | const result = await axios
11 | .get(`${storyUrl + storyId}.json`)
12 | .then(({ data }) => data && selectFields(data));
13 |
14 | return result;
15 | };
16 |
17 | export const getStoryIds = async () => {
18 | const result = await axios.get(newStoriesUrl)
19 | .then(({ data }) => data)
20 |
21 | return result;
22 | };
--------------------------------------------------------------------------------
/src/Story.js:
--------------------------------------------------------------------------------
1 |
2 | import React, { useState, useEffect } from 'react';
3 | import NewsCard from './components/NewsCard';
4 | import { getStory } from './services/hnapi';
5 | import { mapTime } from './mappers/mapTime'; //unix time convertion
6 |
7 |
8 |
9 |
10 |
11 | export const Story = ({ storyId }) => {
12 | const [story, setStory] = useState({});
13 |
14 | //storing singular data and assing to stroy
15 | useEffect(() => {
16 | getStory(storyId).then(data => data && data.url && setStory(data));
17 | }, []);
18 |
19 | return story && story.url ? (
20 | <>
21 |
22 |
23 |
24 | >
25 | ) : null;
26 | };
27 |
--------------------------------------------------------------------------------
/src/mappers/mapTime.js:
--------------------------------------------------------------------------------
1 | export const mapTime = timestamp => {
2 | const seconds = Math.floor((new Date() - timestamp * 1000) / 1000);
3 |
4 | let interval = Math.floor(seconds / 31536000);
5 |
6 | if (interval > 1) {
7 | return `${interval} years`;
8 | }
9 | interval = Math.floor(seconds / 2592000);
10 |
11 | if (interval > 1) {
12 | return `${interval} months`;
13 | }
14 | interval = Math.floor(seconds / 86400);
15 |
16 | if (interval > 1) {
17 | return `${interval} days`;
18 | }
19 | interval = Math.floor(seconds / 3600);
20 |
21 | if (interval > 1) {
22 | return `${interval} hours`;
23 | }
24 | interval = Math.floor(seconds / 60);
25 |
26 | if (interval > 1) {
27 | return `${interval} minutes`;
28 | }
29 |
30 | return `${Math.floor(seconds)} seconds`;
31 | };
32 |
--------------------------------------------------------------------------------
/src/components/Navbar.css:
--------------------------------------------------------------------------------
1 | .n-logo {
2 | width: 103.72px;
3 | height: 4rem;
4 | color: #124191;
5 | display: flex;
6 | align-items: center;
7 | padding-left: 4.6vw;
8 | }
9 |
10 | .nav-toggle {
11 | padding: 1px 5.6vw 0 18px;
12 | }
13 |
14 | .navbar-ul {
15 | display: inline-flex;
16 | padding: 0 20px;
17 | }
18 |
19 | .nav-btn {
20 | color: transparent;
21 | background-color: transparent;
22 | border: none;
23 | }
24 |
25 | .navbar-li {
26 | color: rgba(255, 255, 255, 0.89);
27 | font-size: 1.05rem;
28 | font-weight: 400;
29 | list-style-type: none;
30 | padding: 0 12px;
31 | text-decoration: none;
32 | }
33 |
34 | .navbar-li:hover {
35 | color: white;
36 | cursor: pointer;
37 | transition: all .5s ease;
38 | transform: scale(1.1);
39 | text-decoration: none !important;
40 | }
--------------------------------------------------------------------------------
/src/navigation/Routes.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import Navbar from '../components/Navbar'
3 |
4 | import {
5 | BrowserRouter as Router,
6 | Switch,
7 | Route,
8 | } from "react-router-dom";
9 |
10 | import { StoriesContainer } from '../containers/StoriesContainer';
11 | import SubmitForm from '../components/SubmitForm';
12 |
13 |
14 | function Routes() {
15 |
16 |
17 | return (
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | );
41 |
42 | }
43 | export default Routes;
--------------------------------------------------------------------------------
/src/components/NewsCard.js:
--------------------------------------------------------------------------------
1 |
2 | import React from 'react'
3 |
4 | function NewsCard(props) {
5 | return (
6 |
7 |
8 |
9 |
10 |
11 |
12 | {props.header}
13 |
14 |
15 |
18 |
19 |
20 | By:{' '}{props.auth}
21 | Posted:{` `} {props.time}{` `}ago
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | )
30 | }
31 |
32 | export default NewsCard;
33 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "hacker-news-clone",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@material-ui/core": "^4.11.4",
7 | "@material-ui/icons": "^4.11.2",
8 | "@testing-library/jest-dom": "^5.13.0",
9 | "@testing-library/react": "^11.2.7",
10 | "@testing-library/user-event": "^12.8.3",
11 | "axios": "^0.21.1",
12 | "react": "^17.0.2",
13 | "react-dom": "^17.0.2",
14 | "react-paginate": "^7.1.3",
15 | "react-router-dom": "^5.2.0",
16 | "react-scripts": "4.0.3",
17 | "web-vitals": "^1.1.2"
18 | },
19 | "scripts": {
20 | "start": "react-scripts start",
21 | "build": "react-scripts build",
22 | "test": "react-scripts test",
23 | "eject": "react-scripts eject"
24 | },
25 | "eslintConfig": {
26 | "extends": [
27 | "react-app",
28 | "react-app/jest"
29 | ]
30 | },
31 | "browserslist": {
32 | "production": [
33 | ">0.2%",
34 | "not dead",
35 | "not op_mini all"
36 | ],
37 | "development": [
38 | "last 1 chrome version",
39 | "last 1 firefox version",
40 | "last 1 safari version"
41 | ]
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/containers/StoriesContainer.js:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useState } from 'react';
2 | import { getStoryIds } from '../services/hnapi';
3 | import { Story } from '../Story';
4 | import ReactPaginate from "react-paginate";
5 |
6 |
7 | export const StoriesContainer = () => {
8 | //assinging a empty array to stroryIds
9 | const [storyIds, setStoryIds] = useState([]);
10 |
11 | const [pageNumber, setPageNumber] = useState(0);
12 |
13 | //pagination
14 |
15 | const usersPerPage = 10;
16 | const pagesVisited = pageNumber * usersPerPage;
17 |
18 | const displayUsers = storyIds
19 | .slice(pagesVisited, pagesVisited + usersPerPage)
20 | .map((storyId) => {
21 | return (
22 |
23 | );
24 | });
25 |
26 | const pageCount = Math.ceil(storyIds.length / usersPerPage);
27 |
28 | const changePage = ({ selected }) => {
29 | setPageNumber(selected);
30 | };
31 |
32 |
33 | useEffect(() => {
34 |
35 | getStoryIds().then(data => setStoryIds(data));
36 | }, []);
37 |
38 | return
39 | {displayUsers}
40 |
51 |
52 | };
53 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | * {
2 | padding: 0;
3 | margin: 0;
4 |
5 | }
6 |
7 | body {
8 | background-color: black;
9 | }
10 |
11 | .container:hover {
12 | transition: all .1s ease;
13 | transform: scale(1.01);
14 | }
15 |
16 | /* CARD Styling */
17 | .card-container {
18 | display: inline-flex;
19 | margin-left: 5rem;
20 |
21 | }
22 |
23 | .card {
24 | border: 2px solid black;
25 | border-radius: 10px;
26 | background-color: #b3e3ff;
27 | display: flex;
28 | justify-content: center;
29 | position: relative;
30 | flex-grow: 1;
31 | width: 500px;
32 | height: 300px;
33 | margin: 20px;
34 | }
35 |
36 | .inner-card {
37 | width: 80%;
38 | margin-top: 4rem;
39 | }
40 |
41 | .inner-card h2 {
42 | margin-bottom: 20px;
43 | }
44 |
45 |
46 | .by {
47 | position: absolute;
48 | left: 0;
49 | bottom: 0;
50 | margin: 0 0 20px 20px;
51 | font-style: italic;
52 |
53 | }
54 |
55 | .posted {
56 | position: absolute;
57 | right: 0;
58 | bottom: 0;
59 | margin: 0 20px 20px 0;
60 | font-size: small;
61 | }
62 |
63 | .btn {
64 | margin-top: 3rem;
65 | }
66 |
67 | .btn a {
68 | padding: 10px 8px;
69 | border-radius: 4px;
70 | border-color: none;
71 | background-color: #34495e;
72 | color: white;
73 | text-decoration: none;
74 | }
75 |
76 | .btn a:hover {
77 | background-color: rgba(255, 255, 255, 0.603);
78 | color: #34495e;
79 | }
80 |
81 | /* pagination */
82 | .paginationBttns {
83 | width: 80%;
84 | height: 40px;
85 | list-style: none;
86 | display: flex;
87 | justify-content: center;
88 | margin: 2rem 0 2rem 5rem;
89 | }
90 |
91 | .paginationBttns a {
92 | padding: 10px;
93 | margin: 8px;
94 | border-radius: 5px;
95 | border: 1px solid #2b2eff;
96 | color: #2b2eff;
97 | cursor: pointer;
98 | }
99 |
100 | .paginationBttns a:hover {
101 | color: white;
102 | background-color: #2b2eff;
103 | }
104 |
105 | .paginationActive a {
106 | color: white;
107 | background-color: #2b2eff;
108 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # Getting Started with Create React App
4 |
5 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
6 |
7 | ## Available Scripts
8 |
9 | In the project directory, you can run:
10 |
11 | ### `npm start`
12 |
13 | Runs the app in the development mode.\
14 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
15 |
16 | The page will reload if you make edits.\
17 | You will also see any lint errors in the console.
18 |
19 | ### `npm test`
20 |
21 | Launches the test runner in the interactive watch mode.\
22 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
23 |
24 | ### `npm run build`
25 |
26 | Builds the app for production to the `build` folder.\
27 | It correctly bundles React in production mode and optimizes the build for the best performance.
28 |
29 | The build is minified and the filenames include the hashes.\
30 | Your app is ready to be deployed!
31 |
32 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
33 |
34 | ### `npm run eject`
35 |
36 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
37 |
38 | 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.
39 |
40 | 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.
41 |
42 | 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.
43 |
44 | ## Learn More
45 |
46 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
47 |
48 | To learn React, check out the [React documentation](https://reactjs.org/).
49 |
50 | ### Code Splitting
51 |
52 | 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)
53 |
54 | ### Analyzing the Bundle Size
55 |
56 | 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)
57 |
58 | ### Making a Progressive Web App
59 |
60 | 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)
61 |
62 | ### Advanced Configuration
63 |
64 | 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)
65 |
66 | ### Deployment
67 |
68 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
69 |
70 | ### `npm run build` fails to minify
71 |
72 | 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)
73 |
--------------------------------------------------------------------------------
/src/components/Navbar.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import './Navbar.css'
3 | import AppBar from '@material-ui/core/AppBar';
4 | import Toolbar from '@material-ui/core/Toolbar';
5 | import Typography from '@material-ui/core/Typography';
6 | import useScrollTrigger from '@material-ui/core/useScrollTrigger';
7 | import Slide from '@material-ui/core/Slide';
8 | import { fade, Grid, InputBase, Link, makeStyles } from '@material-ui/core';
9 | import Logo from '../assets/hn_512.png'
10 | import SearchIcon from '@material-ui/icons/Search';
11 |
12 |
13 |
14 |
15 | function HideOnScroll(props) {
16 | const { children, window } = props;
17 |
18 | const trigger = useScrollTrigger({ target: window ? window() : undefined });
19 |
20 | return (
21 |
22 | {children}
23 |
24 | );
25 | }
26 |
27 | const useStyles = makeStyles((theme) => (
28 | {
29 | typographyStyles: {
30 | display: 'inline-flex',
31 |
32 | flex: 1,
33 | },
34 | search: {
35 | position: 'relative',
36 | borderRadius: theme.shape.borderRadius,
37 | backgroundColor: fade(theme.palette.common.white, 0.15),
38 | '&:hover': {
39 | backgroundColor: fade(theme.palette.common.white, 0.25),
40 | },
41 | marginLeft: 0,
42 | width: '100%',
43 | [theme.breakpoints.up('sm')]: {
44 | marginLeft: theme.spacing(1),
45 | width: 'auto',
46 | },
47 | },
48 | searchIcon: {
49 | padding: theme.spacing(0, 2),
50 | height: '100%',
51 | position: 'absolute',
52 | pointerEvents: 'none',
53 | display: 'flex',
54 | alignItems: 'center',
55 | justifyContent: 'center',
56 | },
57 | inputRoot: {
58 | color: 'inherit',
59 | },
60 | inputInput: {
61 | padding: theme.spacing(1, 1, 1, 0),
62 | // vertical padding + font size from searchIcon
63 | paddingLeft: `calc(1em + ${theme.spacing(4)}px)`,
64 | transition: theme.transitions.create('width'),
65 | width: '100%',
66 | [theme.breakpoints.up('sm')]: {
67 | width: '12ch',
68 | '&:focus': {
69 | width: '20ch',
70 | },
71 | }
72 | }
73 | }
74 | ));
75 |
76 |
77 |
78 | const Navbar = () => {
79 |
80 | const classes = useStyles()
81 |
82 | const useStyle = {
83 | toolbar: {
84 | minHeight: '66.5px',
85 | backgroundColor: '#333333',
86 |
87 | }
88 | };
89 |
90 |
91 | return (
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
122 |
123 |
124 |
125 | Login
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
138 |
139 |
140 |
141 | );
142 |
143 | }
144 |
145 |
146 | export default Navbar;
147 |
--------------------------------------------------------------------------------