Skip to content

Commit

Permalink
Merge pull request #30 from thebishaldeb/master
Browse files Browse the repository at this point in the history
#29: migration from js to ts.
  • Loading branch information
armujahid authored Oct 26, 2019
2 parents 4af9988 + ecefef4 commit 53d3bb5
Show file tree
Hide file tree
Showing 22 changed files with 14,599 additions and 149 deletions.
14,352 changes: 14,352 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"awesome-typescript-loader": "^5.2.1",
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"paginate-array": "^2.1.0",
Expand All @@ -12,7 +13,9 @@
"react-infinite-scroller": "^1.2.4",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"reactstrap": "^8.1.1"
"reactstrap": "^8.1.1",
"source-map-loader": "^0.2.4",
"typescript": "^3.6.4"
},
"scripts": {
"start": "react-scripts start",
Expand All @@ -21,6 +24,8 @@
"eject": "react-scripts eject"
},
"devDependencies": {
"@types/react-infinite-scroller": "^1.2.1",
"@types/reactstrap": "^8.0.5",
"axios-mock-adapter": "^1.17.0",
"eslint-plugin-jest": "^22.19.0",
"eslint-plugin-react": "^7.16.0"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ContentLoader from "react-content-loader"
import { Card } from 'reactstrap';

const CustomLoader = props => (
const CustomLoader = (props: any) => (
<Card>
<ContentLoader
height={95}
Expand Down
36 changes: 0 additions & 36 deletions src/components/Item.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/components/Item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useState, useEffect } from "react";
import Story from "./Story";
import Job from "./Job";
import { getItem } from "../utils/Api";
import CustomLoader from "./CustomLoader";

const Item = (props: any) => {
const [item, setItem]: any = useState(null);

useEffect(() => {
const fetchAndSetItem = async () => {
const result = await getItem(props.id);
if (result.resolved) {
setItem(result.data);
}
};
fetchAndSetItem();
}, [props.id]);

if (!item) {
return <CustomLoader />;
}
if (item!.type === "story") {
return <Story item={item} />;
}
if (item!.type === "job") {
return <Job item={item} />;
}
return <div>Rendering of {item!.type} is not currently supported :(</div>;
};

export default Item;
16 changes: 0 additions & 16 deletions src/components/Job.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/components/Job.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import { Card, CardBody, CardTitle } from "reactstrap";
import { handleClick } from "../utils/helper";

const Job = ({ item }: any) => {
return (
<Card onClick={() => (item.url ? window.open(item.url, "_blank") : null)}>
<CardBody>
<CardTitle>{item.title}</CardTitle>
<p className="card-text">
{item.score} points by{" "}
<u onClick={event => handleClick(event, "user", item.by)}>
{item.by}
</u>{" "}
|{" "}
<u onClick={event => handleClick(event, "item", item.id)}>
view full description
</u>
</p>
</CardBody>
</Card>
);
};

export default Job;
File renamed without changes.
50 changes: 0 additions & 50 deletions src/components/Stories.js

This file was deleted.

52 changes: 52 additions & 0 deletions src/components/Stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState, useEffect } from "react";
import Item from "./Item";
import { getList } from "../utils/Api";
import InfiniteScroll from "react-infinite-scroller";
import paginate from "paginate-array";

const Stories = (props: any) => {
const [stories, setStories] = useState(null);
const [renderlist, setrenderlist] = useState(Array());
const [hasMoreItems, sethasMoreItems] = useState(true);
const [initialLoad, setinitialLoad] = useState(true);

useEffect(() => {
const fetchAndSetStories = async () => {
const result = await getList(props.storytype);
if (result.resolved) {
setStories(result.data);
}
};
fetchAndSetStories();
}, [props.storytype]);

const loadItems = (pageNumber: any) => {
const pageItems = paginate(stories, pageNumber, 10); //10 items per page
setrenderlist([...renderlist, ...pageItems.data]);
setinitialLoad(false);
sethasMoreItems(pageItems.currentPage !== pageItems.totalPages);
};

if (!stories) {
return null;
}
const loader = <div className="loader">Loading ...</div>;

return (
<InfiniteScroll
pageStart={0}
loadMore={loadItems}
hasMore={hasMoreItems}
initialLoad={initialLoad}
loader={loader}
>
<div className="stories">
{renderlist.map(id => (
<Item key={id} id={id} />
))}
</div>
</InfiniteScroll>
);
};

export default Stories;
16 changes: 0 additions & 16 deletions src/components/Story.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/components/Story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import { Card, CardBody, CardTitle } from "reactstrap";
import { handleClick } from "../utils/helper";

const Story = ({ item }: any) => {
return (
<Card onClick={() => (item.url ? window.open(item.url, "_blank") : null)}>
<CardBody>
<CardTitle>{item.title}</CardTitle>
<p className="card-text">
{item.score} points by{" "}
<u onClick={event => handleClick(event, "user", item.by)}>
{item.by}
</u>{" "}
|{" "}
<u onClick={event => handleClick(event, "item", item.id)}>
{item.kids ? item.kids.length : 0} comments
</u>
</p>
</CardBody>
</Card>
);
};

export default Story;
File renamed without changes.
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
23 changes: 0 additions & 23 deletions src/utils/Api.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/utils/Api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from "axios";
import { API_URL } from "./constants";

export async function getItem(id: any) {
try {
const itemResonse = await axios.get(`${API_URL}/item/${id}.json`);
return { resolved: true, data: itemResonse.data };
} catch (error) {
return { resolved: false, data: error };
}
}

export async function getList(name: any) {
try {
const itemResonse = await axios.get(`${API_URL}/${name}.json`);
return { resolved: true, data: itemResonse.data };
} catch (error) {
return { resolved: false, data: error };
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils/helper.js → src/utils/helper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const handleClick = (event, type, id) => {
export const handleClick = (event: any, type: any, id: any) => {
window.open(`https://news.ycombinator.com/${type}?id=${id}`, "_blank")
event.preventDefault()
event.stopPropagation()
Expand Down
27 changes: 27 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"noImplicitAny": false
},
"include": [
"./src/**/*"
]
}
Loading

0 comments on commit 53d3bb5

Please sign in to comment.