Skip to content

Commit

Permalink
Merge manual
Browse files Browse the repository at this point in the history
  • Loading branch information
blagoslav-mihaylov committed Jul 9, 2021
2 parents 26ec20d + 71b6117 commit c6f0ca5
Show file tree
Hide file tree
Showing 29 changed files with 2,370 additions and 1,578 deletions.
309 changes: 187 additions & 122 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';

import firebase from "firebase/app";
import "firebase/auth";
import firebase from 'firebase/app';
import 'firebase/auth';

import axios from 'axios';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';
Expand All @@ -13,132 +13,197 @@ import Modules from './modules/Modules';
import styled from 'styled-components';
import ProcessProtocols from './process_protocols/ProcessProtocols';

import { apiKey, authDomain, databaseURL, projectId } from "../../config/keys";
import { apiKey, authDomain, databaseURL, projectId } from '../../config/keys';

const AppStyle = styled.div`
font-family: Montserrat, sans-serif;
font-family: Montserrat, sans-serif;
`;

export const AuthContext = React.createContext();

export default props => {

const [state, setState] = useState({user: null, loading: true, token: '', parties: [], countries: [], organizations: []});

useEffect(() => {
firebase.initializeApp({ apiKey, authDomain, databaseURL, projectId });

firebase.app().auth().onAuthStateChanged(async user => {
if (user) {
const idToken = await user.getIdToken();

setState({...state, loading: true});
const res = await axios.get(`${apiHost()}/me`, {
headers: { 'Authorization': `Bearer ${idToken}` }
});

const res2 = await axios.get(`${apiHost()}/parties`, {
headers: { 'Authorization': `Bearer ${idToken}` }
});

const res3 = await axios.get(`${apiHost()}/countries`, {
headers: { 'Authorization': `Bearer ${idToken}` }
});

const res4 = await axios.get(`${apiHost()}/organizations`, {
headers: { 'Authorization': `Bearer ${idToken}` }
})


setState({user: res.data, loading: false, token: idToken, parties: res2.data, countries: res3.data, organizations: res4.data});
} else {
setState({user: null, loading: false, token: '', parties: [], countries: [], organizations: []});
}
});
}, []);

const logIn = ({ email, password }) => {
return firebase.auth().signInWithEmailAndPassword(email, password);
};

const logOut = () => {
firebase.app().auth().signOut();
};

const apiHost = () => {
if(!process.env.API_HOST) {
return 'https://d1tapi.dabulgaria.bg';
export default (props) => {
const [state, setState] = useState({
user: null,
loading: true,
token: '',
parties: [],
countries: [],
organizations: [],
roles: [],
});

useEffect(() => {
firebase.initializeApp({ apiKey, authDomain, databaseURL, projectId });

firebase
.app()
.auth()
.onAuthStateChanged(async (user) => {
if (user) {
const idToken = await user.getIdToken();

setState({ ...state, loading: true });
const res = await axios.get(`${apiHost()}/me`, {
headers: { Authorization: `Bearer ${idToken}` },
});

const res2 = await axios.get(`${apiHost()}/parties`, {
headers: { Authorization: `Bearer ${idToken}` },
});

const res3 = await axios.get(`${apiHost()}/countries`, {
headers: { Authorization: `Bearer ${idToken}` },
});

const res4 = await axios.get(`${apiHost()}/organizations`, {
headers: { Authorization: `Bearer ${idToken}` },
});

const res5 = await axios.get(`${apiHost()}/users/roles`, {
headers: { Authorization: `Bearer ${idToken}` },
});

setState({
user: res.data,
loading: false,
token: idToken,
parties: res2.data,
countries: res3.data,
organizations: res4.data,
roles: res5.data,
});
} else {
return process.env.API_HOST;
}
};

const authGet = async (path) => {
const res = await axios.get(`${apiHost()}${path}`, { headers: { 'Authorization': `Bearer ${state.token}` }});
return res;
};

const authPost = async (path, body) => {
let res;
try {
res = await axios.post(`${apiHost()}${path}`, body? body : {}, { headers: { 'Authorization': `Bearer ${state.token}` }});
} catch(err) {
alert(`Error ${err.response.status}: ${err.response.statusText}\n${err.response.data.message.map((m, i) => `\n${i+1}. ${m}`)}`);
throw err;
}
return res;
};

const authDelete = async (path) => {
const res = await axios.delete(`${apiHost()}${path}`, { headers: { 'Authorization': `Bearer ${state.token}` }});
return res;
};

const authPut = async (path, body) => {
let res;
try {
res = await axios.put(`${apiHost()}${path}`, body? body : {}, { headers: { 'Authorization': `Bearer ${state.token}` }});
} catch(err) {
alert(`Error ${err.response.status}: ${err.response.statusText}\n${err.response.data.message.map((m, i) => `\n${i+1}. ${m}`)}`);
throw err;
}
return res;
};

const authPatch = async (path, body) => {
let res;
try {
res = await axios.patch(`${apiHost()}${path}`, body? body : {}, { headers: { 'Authorization': `Bearer ${state.token}` }});
} catch(err) {
alert(`Error ${err.response.status}: ${err.response.statusText}\n${err.response.data.message.map((m, i) => `\n${i+1}. ${m}`)}`);
throw err;
setState({
user: null,
loading: false,
token: '',
parties: [],
countries: [],
organizations: [],
roles: [],
});
}
return res;
};

return(
<AppStyle>
<BrowserRouter>
<AuthContext.Provider value={{
user: state.user, token: state.token, parties: state.parties, countries: state.countries, organizations: state.organizations,
logIn, logOut, authGet, authPost, authDelete, authPut, authPatch}}>
{
state.loading
? <Loading fullScreen/>
: <Switch>
<Route path='/login'>
{state.user? <Redirect to='/'/> : <Login/>}
</Route>
<Route exact path='/protocols/process'>
{!state.user? <Redirect to='/login'/> : <ProcessProtocols/>}
</Route>
<Route path='/'>
{!state.user? <Redirect to='/login'/> : <Modules/>}
</Route>
</Switch>
}
</AuthContext.Provider>
</BrowserRouter>
</AppStyle>
);
});
}, []);

const logIn = ({ email, password }) => {
return firebase.auth().signInWithEmailAndPassword(email, password);
};

const logOut = () => {
firebase.app().auth().signOut();
};

const apiHost = () => {
if (!process.env.API_HOST) {
return 'https://d1tapi.dabulgaria.bg';
} else {
return process.env.API_HOST;
}
};

const authGet = async (path) => {
const res = await axios.get(`${apiHost()}${path}`, {
headers: { Authorization: `Bearer ${state.token}` },
});
return res;
};

const authPost = async (path, body) => {
let res;
try {
res = await axios.post(`${apiHost()}${path}`, body ? body : {}, {
headers: { Authorization: `Bearer ${state.token}` },
});
} catch (err) {
alert(
`Error ${err.response.status}: ${
err.response.statusText
}\n${err.response.data.message.map((m, i) => `\n${i + 1}. ${m}`)}`
);
throw err;
}
return res;
};

const authDelete = async (path) => {
const res = await axios.delete(`${apiHost()}${path}`, {
headers: { Authorization: `Bearer ${state.token}` },
});
return res;
};

const authPut = async (path, body) => {
let res;
try {
res = await axios.put(`${apiHost()}${path}`, body ? body : {}, {
headers: { Authorization: `Bearer ${state.token}` },
});
} catch (err) {
alert(
`Error ${err.response.status}: ${
err.response.statusText
}\n${err.response.data.message.map((m, i) => `\n${i + 1}. ${m}`)}`
);
throw err;
}
return res;
};

const authPatch = async (path, body) => {
let res;
try {
res = await axios.patch(`${apiHost()}${path}`, body ? body : {}, {
headers: { Authorization: `Bearer ${state.token}` },
});
} catch (err) {
alert(
`Error ${err.response.status}: ${
err.response.statusText
}\n${err.response.data.message.map((m, i) => `\n${i + 1}. ${m}`)}`
);
throw err;
}
return res;
};

return (
<AppStyle>
<BrowserRouter>
<AuthContext.Provider
value={{
user: state.user,
token: state.token,
parties: state.parties,
countries: state.countries,
organizations: state.organizations,
roles: state.roles,
logIn,
logOut,
authGet,
authPost,
authDelete,
authPut,
authPatch,
}}
>
{state.loading ? (
<Loading fullScreen />
) : (
<Switch>
<Route path="/login">
{state.user ? <Redirect to="/" /> : <Login />}
</Route>
<Route exact path="/protocols/process">
{!state.user ? <Redirect to="/login" /> : <ProcessProtocols />}
</Route>
<Route path="/">
{!state.user ? <Redirect to="/login" /> : <Modules />}
</Route>
</Switch>
)}
</AuthContext.Provider>
</BrowserRouter>
</AppStyle>
);
};
Loading

0 comments on commit c6f0ca5

Please sign in to comment.