Remove instances of fetch()

This commit is contained in:
Michael Fatemi 2021-04-10 21:32:37 -04:00
parent 7e6951ac03
commit 50c57d9ed4
6 changed files with 66 additions and 96 deletions

View File

@ -1,6 +1,7 @@
import { useContext, useEffect, useState } from 'react'; import { useContext, useEffect, useState } from 'react';
import { Redirect, useLocation, useParams } from 'react-router-dom'; import { Redirect, useLocation, useParams } from 'react-router-dom';
import { API_ENDPOINT } from '../api/api'; import { API_ENDPOINT } from '../api/api';
import { makeAPIPostCall } from '../api/utils';
import AuthenticationContext from './AuthenticationContext'; import AuthenticationContext from './AuthenticationContext';
export default function Authenticator() { export default function Authenticator() {
@ -13,26 +14,15 @@ export default function Authenticator() {
); );
useEffect(() => { useEffect(() => {
fetch(`${API_ENDPOINT}/create_session`, { makeAPIPostCall('/create_session', { code, provider })
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code,
provider,
}),
})
.then((response) => { .then((response) => {
response.json().then((json) => { if (response.data.status === 'success') {
if (json.status === 'success') { localStorage.setItem('session_token', response.data.token);
localStorage.setItem('session_token', json.token);
refreshAuthState && refreshAuthState(); refreshAuthState && refreshAuthState();
setStatus('authenticated'); setStatus('authenticated');
} else { } else {
setStatus('errored'); setStatus('errored');
} }
});
}) })
.catch(() => { .catch(() => {
setStatus('errored'); setStatus('errored');

View File

@ -1,28 +1,24 @@
import React, { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { makeAPIGetCall } from '../api/utils';
const Groups = () => { const Groups = () => {
const [state, setState] = useState({ const [groups, setGroups] = useState<Carpool.Group[]>([
Groups: [
{ {
id: 1, _id: '1234',
group_title: 'TJ', name: 'TJ',
creator_id: 'michael',
member_ids: [],
}, },
], ]);
});
const callAPI = () => {
fetch(`${process.env.REACT_APP_API_ENDPOINT}/groups/`)
.then((response) => response.json())
.then((data) => {
if (data !== undefined) {
setState(data);
}
});
};
useEffect(() => { useEffect(() => {
callAPI(); makeAPIGetCall('/groups').then((res) => {
if (res.data.data) {
setGroups(res.data.data);
}
});
}, []); }, []);
return ( return (
<div className="bg-dark" style={{ minHeight: '100vh' }}> <div className="bg-dark" style={{ minHeight: '100vh' }}>
<h1 <h1
@ -40,7 +36,7 @@ const Groups = () => {
</a> </a>
<div className="container" style={{ fontFamily: 'Courier New' }}> <div className="container" style={{ fontFamily: 'Courier New' }}>
<br></br> <br></br>
{state.Groups.map((group, index) => { {groups.map((group, index) => {
return ( return (
<div <div
className="card card-body text-left" className="card card-body text-left"
@ -48,8 +44,8 @@ const Groups = () => {
backgroundColor: index % 2 === 0 ? '#F1EAE8' : '#FFFFFF', backgroundColor: index % 2 === 0 ? '#F1EAE8' : '#FFFFFF',
}} }}
> >
<form action={'/requestgroup/' + group.id} method="POST"> <form action={'/requestgroup/' + group._id} method="POST">
<p className="card-title">{group.group_title}</p> <p className="card-title">{group.name}</p>
<input <input
type="submit" type="submit"
value="Request to Join" value="Request to Join"

View File

@ -1,28 +1,24 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { makeAPIGetCall } from '../api/utils';
const MyGroups = () => { const MyGroups = () => {
const [state, setState] = useState({ const [groups, setGroups] = useState<Carpool.Group[]>([
MyGroups: [
{ {
id: 1, _id: '1234',
group_title: 'TJ', name: 'TJ',
creator_id: '12345Q',
member_ids: [],
}, },
], ]);
});
const callAPI = () => {
fetch(`${process.env.REACT_APP_API_ENDPOINT}/groups/`)
.then((response) => response.json())
.then((data) => {
if (data !== undefined) {
setState(data);
}
});
};
useEffect(() => { useEffect(() => {
callAPI(); makeAPIGetCall('/groups').then((res) => {
if (res.data.data) {
setGroups(res.data.data);
}
});
}, []); }, []);
return ( return (
<div className="bg-dark" style={{ minHeight: '100vh' }}> <div className="bg-dark" style={{ minHeight: '100vh' }}>
<h1 <h1
@ -40,7 +36,7 @@ const MyGroups = () => {
</a> </a>
<div className="container" style={{ fontFamily: 'Courier New' }}> <div className="container" style={{ fontFamily: 'Courier New' }}>
<br></br> <br></br>
{state.MyGroups.map((group, index) => { {groups.map((group, index) => {
let background; let background;
if (index % 2 === 0) { if (index % 2 === 0) {
background = '#F1EAE8'; background = '#F1EAE8';
@ -52,8 +48,8 @@ const MyGroups = () => {
className="card card-body text-left" className="card card-body text-left"
style={{ backgroundColor: background }} style={{ backgroundColor: background }}
> >
<a href={'/group/' + group.id} className="card-title"> <a href={'/group/' + group._id} className="card-title">
{group.group_title} {group.name}
</a> </a>
</div> </div>
); );

View File

@ -1,5 +1,6 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { API_ENDPOINT } from '../api/api'; import { API_ENDPOINT } from '../api/api';
import { makeAPIGetCall } from '../api/utils';
const MyPools = () => { const MyPools = () => {
// const id = props.match.params.id; // const id = props.match.params.id;
@ -55,12 +56,9 @@ const MyPools = () => {
]); ]);
useEffect(() => { useEffect(() => {
console.log(process.env); makeAPIGetCall('/my_pools').then((res) => {
fetch(`${API_ENDPOINT}/my_pools`) if (res.data.data) {
.then((response) => response.json()) setPools(res.data.data);
.then((json) => {
if (json) {
setPools(json.data);
} }
}); });
}, []); }, []);

View File

@ -1,5 +1,6 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { API_ENDPOINT } from '../api/api'; import { API_ENDPOINT } from '../api/api';
import { makeAPIGetCall } from '../api/utils';
const maybePluralize = (count: number, noun: string, suffix = 's') => const maybePluralize = (count: number, noun: string, suffix = 's') =>
`${count} ${noun}${count !== 1 ? suffix : ''}`; `${count} ${noun}${count !== 1 ? suffix : ''}`;
@ -58,12 +59,9 @@ const Pools = () => {
]); ]);
useEffect(() => { useEffect(() => {
console.log(process.env); makeAPIGetCall('/my_pools').then((res) => {
fetch(`${API_ENDPOINT}/my_pools`) if (res.data.data) {
.then((response) => response.json()) setPools(res.data.data);
.then((json) => {
if (json) {
setPools(json.data);
} }
}); });
}, []); }, []);

View File

@ -5,6 +5,7 @@ import React, {
FormEventHandler, FormEventHandler,
} from 'react'; } from 'react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { makeAPIGetCall } from '../api/utils';
const UpdatePool = () => { const UpdatePool = () => {
const id = useParams<{ id: string }>().id; const id = useParams<{ id: string }>().id;
@ -21,26 +22,17 @@ const UpdatePool = () => {
comments: ['What is the covid vaccination status of all the participants?'], comments: ['What is the covid vaccination status of all the participants?'],
}); });
const callAPI = useCallback(() => {
fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`)
.then((response) => response.json())
.then((data) => {
if (data !== undefined) {
setPool(data);
}
});
}, [id]);
const onSubmit: FormEventHandler<HTMLFormElement> = (e) => { const onSubmit: FormEventHandler<HTMLFormElement> = (e) => {
e.preventDefault(); e.preventDefault();
fetch(`${process.env.REACT_APP_API_ENDPOINT}/update_pool`) makeAPIGetCall('/update_pool').then((res) => {
.then((response) => response.json()) console.log(res);
.then((data) => {
console.log(data);
}); });
}; };
useEffect(() => { useEffect(() => {
callAPI(); makeAPIGetCall('/pool', { poolID: id }).then((res) => {
}, [callAPI]); if (res.data.data) setPool(res.data.data);
});
}, [id]);
return ( return (
<div <div
className="bg-dark" className="bg-dark"