Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Created June 5, 2020 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3coder/1ec296936999d0e8c5be1db392576afb to your computer and use it in GitHub Desktop.
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
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