-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relates #55
- Loading branch information
Showing
64 changed files
with
4,120 additions
and
24,713 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,123 @@ | ||
import React from 'react'; | ||
import { BrowserRouter as Router } from 'react-router-dom'; | ||
import KindergartenForm from '../Components/KindergartenForm'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { | ||
BrowserRouter as Router, | ||
Route, | ||
Switch, | ||
Redirect, | ||
} from 'react-router-dom'; | ||
import axios from 'axios'; | ||
import { notification } from 'antd'; | ||
|
||
import NavBar from '../Components/Layout/Navbar'; | ||
import Footer from '../Components/Layout/Footer'; | ||
import SideBar from '../Components/Layout/SideBar'; | ||
import Home from '../Pages/HomePage'; | ||
import LoginPage from '../Pages/LoginPage'; | ||
import AuthContext from '../Context/AuthContext'; | ||
import LogoutContext from '../Context/LogoutContext'; | ||
import About from '../Pages/ِAboutPage'; | ||
|
||
import './style.css'; | ||
|
||
const App = () => ( | ||
<Router> | ||
<KindergartenForm | ||
dorpListOptions={[{ value: 'غزة', id: 1, disabled: false }]} | ||
/> | ||
</Router> | ||
); | ||
const App = () => { | ||
const [userData, setUserData] = useState({}); | ||
const [isOK, setIsOk] = useState(false); | ||
const [role, setRole] = useState(null); | ||
const checkAuth = async () => { | ||
try { | ||
const { | ||
data: { data }, | ||
} = await axios.get('/api/v1/getAuthUser'); | ||
setUserData(data); | ||
setRole(data.is_admin === 'true' ? 'admin' : 'user'); | ||
setIsOk(true); | ||
} catch (error) { | ||
setRole(null); | ||
setUserData({}); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const source = axios.CancelToken.source(); | ||
checkAuth(); | ||
return () => { | ||
source.cancel('clean up axios'); | ||
}; | ||
}, [role]); | ||
|
||
const logout = async () => { | ||
try { | ||
const source = axios.CancelToken.source(); | ||
await axios.get('/api/v1/logout'); | ||
setRole(null); | ||
setUserData({}); | ||
return () => { | ||
source.cancel('clean up axios'); | ||
}; | ||
} catch (err) { | ||
return notification.open({ | ||
message: 'حدث خطأ في السيرفر, يرجى المحاولة لاحقا', | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<Router> | ||
<Switch> | ||
<AuthContext.Provider value={{ role, userData, checkAuth }}> | ||
<LogoutContext.Provider value={{ logout }}> | ||
<Route exact path={['/', '/about', '/kindergarten/:id', '/search']}> | ||
<NavBar /> | ||
</Route> | ||
|
||
<Route exact path="/"> | ||
<Home /> | ||
</Route> | ||
|
||
<Route path="/about"> | ||
<About /> | ||
</Route> | ||
|
||
<Route exact path="/kindergarten/:id" /> | ||
|
||
<Route exact path="/login"> | ||
{!role ? <LoginPage /> : <Redirect to="/" />} | ||
</Route> | ||
|
||
<Route exact path="/signup" /> | ||
|
||
<Route exact path={['/', '/about', '/kindergarten/:id', '/search']}> | ||
<Footer /> | ||
</Route> | ||
|
||
{isOK && role === 'admin' && ( | ||
<Route path="/dashboard"> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
width: '100vw', | ||
}} | ||
> | ||
<SideBar /> | ||
<div | ||
style={{ | ||
width: '100vw', | ||
}} | ||
> | ||
<NavBar /> | ||
<Route exact path="/dashboard/all" /> | ||
<Route exact path="/dashboard/add" /> | ||
<Route exact path="/dashboard/notification" /> | ||
</div> | ||
</div> | ||
</Route> | ||
)} | ||
</LogoutContext.Provider> | ||
</AuthContext.Provider> | ||
</Switch> | ||
</Router> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.