Skip to content

Commit

Permalink
working checkin from web app
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-dudu committed Jan 13, 2025
1 parent 3fd8f20 commit 7f031ca
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
Expand Down
31 changes: 30 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import logo from './logo.svg';
import './App.css';
import React from 'react';
import CourtsList from './CourtsList.tsx';
import { useAuth } from "react-oidc-context";

function App() {

const auth = useAuth();

if (auth.isLoading) {
return <div>Loading...</div>;
}

if (auth.error) {
return <div>Encountering error... {auth.error.message}</div>;
}

// if (auth.isAuthenticated) {
// return (
// <div>
// <pre> Hello: {auth.user?.profile.email} </pre>
// <pre> ID Token: {auth.user?.id_token} </pre>
// <pre> Access Token: {auth.user?.access_token} </pre>
// <pre> Refresh Token: {auth.user?.refresh_token} </pre>

// <button onClick={() => auth.removeUser()}>Sign out</button>
// </div>
// );
// }

return (
<div className="App">
<div>
<button onClick={() => auth.signinRedirect()}>Sign in</button>
{/* <button onClick={() => signOutRedirect()}>Sign out</button> */}
<button onClick={() => auth.removeUser()}>Sign out</button>
</div>
{auth.isAuthenticated && 'I AM AUTH' }
<CourtsList />
</div>
);
Expand Down
17 changes: 7 additions & 10 deletions src/CourtsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ const CourtsList: React.FC = () => {
const auth = useAuth();

useEffect(() => {
// Fetch courts when component mounts
fetchCourts();
}, []);

const fetchCourts = async () => {
try {
const response = await fetch("https://4vvelb2907.execute-api.eu-central-1.amazonaws.com/v1/courts");
const response = await fetch(process.env.REACT_APP_COURT_GET_LINK || '');
const data = await response.json();
setCourts(data.body);
} catch (error) {
Expand All @@ -26,21 +25,20 @@ const CourtsList: React.FC = () => {
return;
}

const token = auth.user?.id_token; // Get the ID token from Cognito
const token = auth.user?.id_token;

try {
const response = await fetch("https://4vvelb2907.execute-api.eu-central-1.amazonaws.com/v1/checkin", {
method: "PUT",
const response = await fetch( process.env.REACT_APP_COURT_CHECKIN_LINK || '', {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify({ courtID })
});

body: JSON.stringify({ courtID }),
});
if (response.ok) {
alert("Check-in successful!");
fetchCourts(); // Refresh courts list
fetchCourts();
} else {
const errorData = await response.json();
alert(`Error: ${errorData.message}`);
Expand All @@ -50,7 +48,6 @@ const CourtsList: React.FC = () => {
alert("An error occurred during check-in.");
}
};
console.log('courts', courts)

return (
<div>
Expand Down
20 changes: 13 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App.tsx';
import { AuthProvider } from "react-oidc-context";

const cognitoAuthConfig = {
authority: process.env.REACT_APP_COGNITO_AUTH_CONFIG_AUTHORTITY,
client_id: process.env.REACT_APP_COGNITO_AUTH_CONFIG_CLIENT_ID,
redirect_uri: process.env.REACT_APP_COGNITO_AUTH_CONFIG_REDIRECT,
response_type: "code",
scope: "phone openid email",
};


const rootElement = document.getElementById('root');
if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render(
<React.StrictMode>
<App />
<AuthProvider {...cognitoAuthConfig}>
<App />
</AuthProvider>
</React.StrictMode>
);
);;
} else {
console.error("Root element not found");
}

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
// reportWebVitals();

0 comments on commit 7f031ca

Please sign in to comment.