diff options
-rw-r--r-- | package-lock.json | 31 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/Feed/Feed.tsx | 31 | ||||
-rw-r--r-- | src/index.tsx | 2 | ||||
-rw-r--r-- | src/requests.ts | 10 |
5 files changed, 64 insertions, 11 deletions
diff --git a/package-lock.json b/package-lock.json index dc40a7d..b8e8d3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2312,6 +2312,37 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.10.0.tgz", "integrity": "sha512-3YDiu347mtVtjpyV3u5kVqQLP242c06zwDOgpeRnybmXlYYsLbtTrUBUm8i8srONt+FWobl5aibnU1030PeeuA==" }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "axobject-query": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.1.2.tgz", diff --git a/package.json b/package.json index b57298f..9863e0e 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "dependencies": { "@material-ui/core": "^4.10.1", "@material-ui/icons": "^4.9.1", + "axios": "^0.19.2", "react": "^16.13.1", "react-dom": "^16.13.1", "react-scripts": "3.4.1", diff --git a/src/Feed/Feed.tsx b/src/Feed/Feed.tsx index 33e6d03..51a3c4c 100644 --- a/src/Feed/Feed.tsx +++ b/src/Feed/Feed.tsx @@ -1,30 +1,41 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Poll } from '../types'; import PollCard from '../PollCard/PollCard'; +import { get } from '../requests'; interface PropTypes { - polls: Poll[], + page: string; } -const usedStyles = makeStyles(theme => ({ - feed: { +const useStyles = makeStyles(theme => ({ + root: { position: 'relative', maxWidth: theme.spacing(75), margin: '0 auto' } })); -const Feed: React.FC<PropTypes> = ({ polls }) => { - const classes = usedStyles(); +const Feed: React.FC<PropTypes> = ({ page }) => { + const [polls, setPolls] = useState<Poll[]>([]); + const classes = useStyles(); + + let endpoint: string; + if (page === 'feed') endpoint = '/polls'; + else if (page === 'profile') endpoint = '/profile'; + + useEffect(() => { + get(endpoint).then(response => { + setPolls(response.data); + }); + }, []); return ( - <div className={classes.feed}> - { - polls.map(poll => <PollCard poll={poll} />) - } + <div className={classes.root}> + {polls.map(poll => <PollCard poll={poll} />)} </div> ); }; export default Feed; + diff --git a/src/index.tsx b/src/index.tsx index 0d9751c..5fd57f0 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -77,7 +77,7 @@ const App: React.FC = () => { { page === 'profile' && <ProfileInfo profile={polls[0]} /> } - <Feed polls={polls} /> + <Feed page={page} /> </div> </ThemeProvider> ); diff --git a/src/requests.ts b/src/requests.ts new file mode 100644 index 0000000..486502d --- /dev/null +++ b/src/requests.ts @@ -0,0 +1,10 @@ +import axios, { AxiosResponse } from 'axios'; + +type Request = (url: string, data?: Record<string, unknown>) => Promise<AxiosResponse>; + +const baseApiUrl = 'http://localhost:3030'; + +export const get: Request = url => axios.get(baseApiUrl + url); +export const del: Request = url => axios.delete(baseApiUrl + url); +export const post: Request = (url, data) => axios.post(baseApiUrl + url, data); + |