Skip to content

Commit

Permalink
ADD:Refresh_S (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail-kharrobi committed Oct 21, 2023
2 parents b7b19cb + d96510e commit 4179777
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 136 deletions.
4 changes: 2 additions & 2 deletions backend/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function bootstrap() {
const corsOptions = {
origin: [
'http://localhost:3000',
'http://142.93.161.63/',
'http://164.92.243.105/',
'http://142.93.161.63',
'http://164.92.243.105',
],
credentials: true,
};
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ services :
- backend
ports:
- 80:80
- 3001:3001
init: true
restart: always

volumes:
front:
Expand Down
54 changes: 43 additions & 11 deletions frontend/code/src/Api/base.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import axios from 'axios'
export const api = axios.create({
baseURL: `${process.env.REACT_APP_API_ENDPOINT}`,
timeout: 10000,
withCredentials:true,
headers: {
"Cache-Control": "no-cache",
'Content-Type': 'application/json',

},
});
import axios from 'axios';
import toast from 'react-hot-toast';

const api = axios.create({
baseURL: `${process.env.REACT_APP_API_ENDPOINT}`,
timeout: 10000,
withCredentials: true,
headers: {
"Cache-Control": "no-cache",
'Content-Type': 'application/json',
},
});

let refreshAttempted = false;

const errorHandler = async (error:any) => {

if (error.response.status === 401) {
if (!refreshAttempted ) {
try {
refreshAttempted = true;
await api.get("auth/refresh");
return api.request(error.config);
} catch (refreshError) {
}
} else {
refreshAttempted = false
}
}
else
{
toast.error(`${error.response.data.message}`)
}
return Promise.reject({ ...error });
};

api.interceptors.response.use(

(response) => response,
(error) => errorHandler(error)
);

export default api;
32 changes: 32 additions & 0 deletions frontend/code/src/Components/FirstLogin/UploadAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useForm, SubmitHandler } from "react-hook-form"


type Inputs = {
Avatar: string
AvatarRequired: string
}


export const UploadAvatar = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<Inputs>()
const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data)



return (
<form onSubmit={handleSubmit(onSubmit)}>
<input type="file" {...register("Avatar")} />


<input {...register("AvatarRequired", { required: true })} />
{errors.AvatarRequired && <span>This field is required</span>}


<input type="submit" />
</form>
)
}
21 changes: 17 additions & 4 deletions frontend/code/src/Components/FirstLogin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@

export const FirstLogin = () =>
{
import { UploadAvatar } from "./UploadAvatar";
export const FirstLogin = () => {
return (
<div className="flex h-screen bg-base-100">
<div className="h-12 w-12 "></div>
<>
<div className="absolute h-full w-full bg-black opacity-40 z-20"></div>

<div className="z-30 absolute h-[70vh] opacity-90 rounded-3xl w-[50vw] top-1/4 right-1/4 bg-base-100 shadow-2xl shadow-white border-4 border-white">

<div className="flex flex-col p-10 gap-6 justify-center items-center content-center">
<div>
Avatar
</div>
<UploadAvatar/>
</div>


</div>
</>

);
}
6 changes: 4 additions & 2 deletions frontend/code/src/Components/Game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const Game = () => {


// },[gameState.height, gameState.width])
/* eslint-disable */
useEffect(() => {
window.addEventListener('resize', () => {
const divh = document.getElementById('Game')?.offsetHeight
Expand All @@ -58,7 +59,8 @@ export const Game = () => {
if(gameState.p1Score === 0 && gameState.p2Score === 0) {
gameState.setBall({x:0.5 * gameState.width,y:0.5 * gameState.height,speed:0,cx:gameState.height /100,cy:gameState.height /100})
}
},)
// disable
},[])

useEffect(() => {
const divh = document.getElementById('Game')?.offsetHeight
Expand Down Expand Up @@ -100,7 +102,7 @@ export const Game = () => {
},20);
return () => clearInterval(oldinter)
}
},[gameState.width , gameState.height , first , gameState])
},[gameState.width , gameState.height])
console.log(gameState.lPaddle)
return (
<div className="flex flex-col gap-10 justify-start md:justify-center md:items-center items-center pt-12 md:pt-0 h-full w-full" >
Expand Down
208 changes: 110 additions & 98 deletions frontend/code/src/Components/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,108 +1,120 @@
import { Logo } from "./Assets/Logo";
import { Alert } from "./Assets/Alert";
import { Avatar } from "./Assets/Avatar";
import { Search } from "./Assets/Search";
import { Dash } from "./Assets/Dash";
import { Game } from "./Assets/Game";
import { Message } from "./Assets/Message";
import { Profile } from "./Assets/Profile";
import { Settings } from "./Assets/Settings";
import { Out } from "./Assets/Out";
import { FC, PropsWithChildren, useLayoutEffect } from "react";
import { Outlet } from "react-router";
import { matchRoutes, useLocation } from "react-router-dom";
import { useUserStore } from "../../Stores/stores";
import { useNavigate } from "react-router-dom";
import { api } from "../../Api/base";
const routes = [
{ path: "Profile/:id" },
{ path: "Settings" },
{ path: "Home" },
{ path: "Chat" },
{ path: "Play" },
{ path: "Pure" },
{ path: "Game" },
];
import { Logo } from './Assets/Logo'
import { Alert } from './Assets/Alert'
import { Avatar } from './Assets/Avatar'
import { Search } from './Assets/Search'
import { Dash } from './Assets/Dash'
import { Game } from './Assets/Game'
import { Message } from './Assets/Message'
import { Profile } from './Assets/Profile'
import { Settings } from './Assets/Settings'
import { Out } from './Assets/Out'
import { FC,PropsWithChildren, useLayoutEffect } from 'react'
import { Outlet ,} from 'react-router'
import { matchRoutes, useLocation } from "react-router-dom"
import { useUserStore } from '../../Stores/stores'
import { useNavigate } from 'react-router-dom'
import api from '../../Api/base'
// import { FirstLogin } from '../FirstLogin'

const routes = [{ path: "Profile/:id" } , {path : "Settings"} , {path : "Home"}, {path:"Chat"} , {path:"Play"}, {path:"Pure"}, {path:"Game"}]
const HideBg = () => {
return (
<div className='absolute h-screen bg-black opacity-20 blur-xl z-[1999]'>
</div>
)
}
const useCurrentPath = () => {
const location = useLocation();
const [{ route }]: any = matchRoutes(routes, location);
return route.path;
};
const location = useLocation()
const [{route}] :any = matchRoutes(routes, location)
return route.path
}

export const Layout: FC<PropsWithChildren> = (): JSX.Element => {
const user = useUserStore();
const navigate = useNavigate();
useLayoutEffect(() => {
const log = async () => {
try {
await user.login();
} catch (e) {
try {
await api.get("/auth/refresh");
await user.login();
} catch (e) {
navigate("/");
user.logout();
}
}
};
log();
export const Layout : FC<PropsWithChildren> = () : JSX.Element =>
{
const user = useUserStore();
const navigate = useNavigate();

// eslint-disable-next-line
}, []);
const path: string = useCurrentPath();
const obj = { x: "30", y: "20" };
return (
useLayoutEffect(() => {
const log = async() => {
try{
await user.login()
}
catch(e)
{
try
{
await api.get("/auth/refresh")
await user.login()
}catch(e)
{
navigate("/")
user.logout()
}

}

}
log()
//eslint-disable-next-line
},[])
const path : string = useCurrentPath()
const obj = {x:"30",y:"20"}
return (
<>
{user.isLogged && (
<div data-theme="mytheme" className=" h-screen ">
<div className=" flex flex-row w-screen h-[8vh] bg-base-200">
<div className="flex justify-start items-center z-50 pl-1 sm:pl-2 h-full w-full">
<Logo {...obj} />
</div>
<div className="flex items-center justify-end pr-6 gap-6 h-full w-full">
<Search />
<Alert />
<Avatar picture={`${user.picture.medium}`} />
</div>
</div>
<div className="flex">
<div className="sm:flex flex-col hidden justify-around items-stretch h-[92vh] bg-base-200 overflow-auto md:pt-10 sm:w-[11vw] md:w-[9vw] lg:w-[8vw] xl:w-[7vw] 2xl:w-[6vw] 3xl:w-[5vw]">
<div className="flex flex-col pl-[1.4vw] justify-evenly content-start gap-y-10 pb-44 ">
<Dash selected={path === "Home" ? true : false} />
<Game selected={path === "Play" ? true : false} />
<Message selected={path === "Chat" ? true : false} />
<Profile selected={path === "Profile/:id" ? true : false} />
<Settings selected={path === "Settings" ? true : false} />
</div>
<div className="flex flex-col pl-[1vw] justify-start">
<Out />
</div>
{/* {
!user.profileComplet && <FirstLogin/>
} */}
{user.isLogged && !user.profileComplet && <HideBg/>}
{user.isLogged &&
<div data-theme="mytheme" className={`h-screen ${!user.profileComplet ? "":""}`}>

<div className=' flex flex-row w-screen h-[8vh] bg-base-200'>
<div className='flex justify-start items-center z-50 pl-1 sm:pl-2 h-full w-full'>
<Logo {...obj}/>
</div>
<div className='flex items-center justify-end pr-6 gap-6 h-full w-full'>
<Search/>
<Alert />
<Avatar picture={`${user.picture.medium}`} />
</div>
</div>
<div className=" h-[8vh] fixed bottom-0 sm:hidden btm-nav bg-base-200 flex justify-end z-50">
<button className="">
<Dash selected={path === "Home" ? true : false} />
</button>
<button className="">
<Game selected={path === "Play" ? true : false} />
</button>
<button className="">
<Message selected={path === "Chat" ? true : false} />
</button>
<button className="">
<Profile selected={path === "Profile/:id" ? true : false} />
</button>
<button className="">
<Settings selected={path === "Settings" ? true : false} />
</button>
<div className='flex'>

<div className='sm:flex flex-col hidden justify-around items-stretch h-[92vh] bg-base-200 overflow-auto md:pt-10 sm:w-[11vw] md:w-[9vw] lg:w-[8vw] xl:w-[7vw] 2xl:w-[6vw] 3xl:w-[5vw]'>
<div className="flex flex-col pl-[1.4vw] justify-evenly content-start gap-y-10 pb-44 ">
<Dash selected={path === "Home" ? true : false}/>
<Game selected={path === "Play" ? true : false}/>
<Message selected={path === "Chat" ? true : false}/>
<Profile selected={path === "Profile/:id" ? true : false}/>
<Settings selected={path === "Settings" ? true : false}/>
</div>
<div className="flex flex-col pl-[1vw] justify-start">
<Out/>
</div>
</div>
<div className="sm:-ml-4 sm:w-[92vw] xl:w-[96vw] md:w-[93.5vw] w-screen right-0 z-10 h-[84vh] sm:h-[92vh] bg-accent sm:rounded-tl-2xl">
<Outlet />
<div className=" h-[8vh] fixed bottom-0 sm:hidden btm-nav bg-base-200 flex justify-end z-50">
<button className="">
<Dash selected={path === "Home" ? true : false} />
</button>
<button className="">
<Game selected={path === "Play" ? true : false}/>
</button>
<button className="">
<Message selected={path === "Chat" ? true : false}/>
</button>
<button className="">
<Profile selected={path === "Profile/:id" ? true : false}/>
</button>
<button className="">
<Settings selected={path === "Settings" ? true : false}/>
</button>
</div>
<div className='sm:-ml-4 sm:w-[92vw] xl:w-[96vw] md:w-[93.5vw] w-screen right-0 z-10 h-[84vh] sm:h-[92vh] bg-accent sm:rounded-tl-2xl'>
<Outlet/>
</div>
</div>
</div>
</div>
)}
}
</>
);
};
)
}
Loading

0 comments on commit 4179777

Please sign in to comment.