Created
June 5, 2020 08:17
-
-
Save dmi3coder/1ec296936999d0e8c5be1db392576afb to your computer and use it in GitHub Desktop.
Main React view with login functionality for Quarkus and React communication with Swagger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useEffect, useState} from 'react'; | |
import './App.css'; | |
import Networking from "./Networking"; | |
import Job from "./Job"; | |
import Auth from "./Auth"; | |
function App() { | |
const [jobs, setJobs] = useState(null); | |
const [newJobContent, setNewJobContent] = useState(""); | |
const loadJobs = () => { | |
Networking.exec({ | |
endpoint: client => client.apis.default.getPosts, | |
success: result => { | |
setJobs(result.body) | |
} | |
}) | |
} | |
useEffect(() => { | |
if(isAuthenticated()) { | |
loadJobs() | |
} | |
}, []) | |
const isAuthenticated = () => { | |
return localStorage.getItem("id_token") !== null; | |
} | |
const submitNewJob = () => { | |
Networking.exec({ | |
endpoint: client => client.apis.default.post_posts, | |
data: {requestBody: {"description": newJobContent, "title": newJobContent}}, | |
success: result => loadJobs() | |
}) | |
} | |
const mainView = isAuthenticated() ? | |
<div> | |
<h1>Job posts</h1> | |
{jobs && jobs.map(it => <Job job={it}/>)} | |
<h2>Create new job</h2> | |
<input onChange={(e) => setNewJobContent(e.target.value)}/> | |
<button onClick={submitNewJob}>Submit Job</button> | |
</div> : | |
<div> | |
<Auth authCallback={() => window.location.reload(false)}/> | |
</div> | |
return ( | |
<div className="App"> | |
<div className="App-header" style={{padding: '3vh'}}> | |
{mainView} | |
</div> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment