From 8b4f8e55962690145834866edd64d45b87b3a545 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 20:10:33 +0300 Subject: feat: implement get/post functions --- src/requests.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/requests.ts (limited to 'src') diff --git a/src/requests.ts b/src/requests.ts new file mode 100644 index 0000000..c653073 --- /dev/null +++ b/src/requests.ts @@ -0,0 +1,12 @@ +import axios from 'axios'; + +const baseApiUrl = 'http://localhost:3030'; + +export const get = (url: string) => { + return axios.get(baseApiUrl + url) +}; + +export const post = (url: string, data: object) => { + return axios.post(baseApiUrl + url, data); +}; + -- cgit v1.2.3 From e92ebe01e3a33dcc75b5e5ffd8d29933a2cf17df Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 20:14:46 +0300 Subject: feat: integrate Feed component with backend --- src/Feed/Feed.tsx | 31 +++++++++++++++++++++---------- src/index.tsx | 2 +- 2 files changed, 22 insertions(+), 11 deletions(-) (limited to 'src') 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 = ({ polls }) => { - const classes = usedStyles(); +const Feed: React.FC = ({ page }) => { + const [polls, setPolls] = useState([]); + 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 ( -
- { - polls.map(poll => ) - } +
+ {polls.map(poll => )}
); }; 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' && } - +
); -- cgit v1.2.3 From b85725ec0fd5088a619a20d2a6370b1f4a38cc22 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 10 Jun 2020 20:22:33 +0300 Subject: refactor: add types to requests --- src/requests.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/requests.ts b/src/requests.ts index c653073..486502d 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -1,12 +1,10 @@ -import axios from 'axios'; +import axios, { AxiosResponse } from 'axios'; -const baseApiUrl = 'http://localhost:3030'; +type Request = (url: string, data?: Record) => Promise; -export const get = (url: string) => { - return axios.get(baseApiUrl + url) -}; +const baseApiUrl = 'http://localhost:3030'; -export const post = (url: string, data: object) => { - return axios.post(baseApiUrl + url, data); -}; +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); -- cgit v1.2.3