Generateurv2/frontend/components/room/Join.jsx

211 lines
6.4 KiB
React
Raw Normal View History

2022-05-18 10:15:54 +02:00
import { useRouter } from "next/router";
import { useContext, useEffect, useState } from "react";
2022-06-24 13:42:16 +02:00
import { useSessionContext } from "../../context/session.context.js";
2022-05-18 10:15:54 +02:00
import {
useWebsocketContext,
WebsocketContext,
} from "../../context/websocket.context.js";
import { checkRoom, getRoom, joinRoom } from "../../requests/requests.room.js";
import styles from "../../styles/room/join.module.scss";
2022-06-24 13:42:16 +02:00
import { isBrowser, isEmpty, parseClassName } from "../../utils/utils.js";
import Input from "../Input.jsx";
2022-05-18 10:15:54 +02:00
import Layout from "../Layout.js";
2022-06-24 13:42:16 +02:00
import { MdKeyboardArrowDown } from "react-icons/md";
2022-05-18 10:15:54 +02:00
export default function Join() {
const [infos, setInfos] = useState({
nick: "",
pin: "",
reconnectCode: "",
joined: null,
});
const router = useRouter();
const { connect, send, isConnected } = useContext(WebsocketContext);
2022-06-24 13:42:16 +02:00
const { authData } = useSessionContext();
useEffect(() => {
if (!isEmpty(authData)) {
setInfos({ ...infos, nick: authData.username });
}
}, [authData]);
2022-05-18 10:15:54 +02:00
useEffect(() => {
if (router.query.mode.length == 2) {
2022-06-24 13:42:16 +02:00
checkRoom(
router.query.mode[1],
isBrowser && isEmpty(authData) && sessionStorage.getItem("clientId")
)
2022-05-18 10:15:54 +02:00
.then((res) => {
2022-06-25 00:26:07 +02:00
connect(`/ws/room/${res.id_code}`);
2022-06-11 23:39:03 +02:00
if (res.is_auth) {
router.push({ pathname: "/room/" + res.id_code }, undefined, {
shallow: true,
});
2022-06-24 13:42:16 +02:00
} else {
setInfos({ ...infos, pin: res.id_code, joined: false });
2022-06-11 23:39:03 +02:00
}
2022-05-18 10:15:54 +02:00
})
.catch((err) => {
router.push({ pathname: "/room/join" }, undefined, { shallow: true });
});
}
2022-06-11 23:39:03 +02:00
return () => {
setInfos({ nick: "", pin: "", reconnectCode: "", joined: null });
2022-06-24 13:42:16 +02:00
};
2022-05-18 10:15:54 +02:00
}, []);
2022-06-24 13:42:16 +02:00
2022-05-18 10:15:54 +02:00
const [errorRoom, setErrorRoom] = useState({ message: "" });
2022-06-11 23:39:03 +02:00
useEffect(() => {
2022-06-24 13:42:16 +02:00
if (
!isEmpty(authData) &&
infos.joined == false
) {
if (isConnected) {
2022-06-11 23:39:03 +02:00
send({
data: {
type: "login",
nick: "",
},
});
}
}
2022-06-24 13:42:16 +02:00
}, [infos.joined, isConnected, authData]);
const [fade, setFade] = useState(null);
useEffect(() => {
if (fade != null) {
setTimeout(() => {
setInfos({ ...infos, joined: !fade });
}, 300);
}
}, [fade]);
const [active, setActive] = useState(false);
2022-05-18 10:15:54 +02:00
return (
2022-06-24 13:42:16 +02:00
<Layout page="Rejoindre une salle">
<div className={styles.main}>
{infos.joined == null && (
<div
className={parseClassName([
styles.form,
fade ? styles.swipe : undefined,
])}
>
<h1 className={styles.title}>Rejoindre une salle</h1>
<Input
type="text"
placeholder="Entrer un code..."
className="exo-input marginb-p0"
value={infos.pin}
onChange={(e) => {
setInfos({ ...infos, pin: e.target.value });
}}
/>
<p className="error-msg margint-p0">{errorRoom.message}</p>
<button
className="exo-btn margint-p2"
onClick={() => {
checkRoom(infos.pin, (isBrowser && isEmpty(authData)) && sessionStorage.getItem('clientId'))
.then((res) => {
setErrorRoom({ message: "" });
2022-06-25 00:26:07 +02:00
connect(`/ws/room/${res.id_code}`);
2022-06-24 13:42:16 +02:00
if (res.is_auth) {
router.push(
{ pathname: "/room/" + res.id_code },
undefined,
{
shallow: true,
}
);
}
setFade(true);
router.push(
{ pathname: "/room/join/" + res.id_code },
undefined,
{
shallow: true,
2022-06-11 23:39:03 +02:00
}
2022-06-24 13:42:16 +02:00
);
})
.catch((err) => {
if (err.response.data.code == "111") {
2022-05-18 10:15:54 +02:00
setInfos({ ...infos, joined: false });
2022-06-24 13:42:16 +02:00
setErrorRoom({ message: "" });
} else if (err.response.data.code == "4044") {
setErrorRoom({ message: err.response.data.error });
}
});
}}
>
{isBrowser && localStorage.getItem("token") !== null
? "Rejoindre !"
: "Suivant !"}
</button>
</div>
)}
{infos.joined == false && (
<div className={styles.form}>
<h1 className={styles.title}>Entrez un pseudo</h1>
<Input
type="text"
disabled={active}
placeholder="Pseudo"
className="exo-input marginb-p0"
value={infos.nick}
onChange={(e) => {
setInfos({ ...infos, nick: e.target.value });
}}
/>
<p className="error-msg margint-p0">{errorRoom.message}</p>
<p
onClick={() => {
setActive(!active);
}}
className={parseClassName([
styles.activeToggler,
active ? styles.active : styles.unactive,
])}
>
<MdKeyboardArrowDown /> Avec un code de reconnexion
</p>
{active && (
<Input
2022-05-18 10:15:54 +02:00
type="text"
2022-06-24 13:42:16 +02:00
placeholder="Code"
className="marginl-p10"
2022-05-18 10:15:54 +02:00
value={infos.reconnectCode}
onChange={(e) => {
setInfos({ ...infos, reconnectCode: e.target.value });
}}
/>
2022-06-24 13:42:16 +02:00
)}
<button
className="exo-btn margint-p2"
onClick={() => {
if (isConnected && !active) {
send({
data: {
type: "login",
nick: infos.nick,
},
});
} else if (isConnected && active) {
send({
data: {
type: "relogin",
code: infos.reconnectCode,
},
});
2022-05-18 10:15:54 +02:00
}
2022-06-24 13:42:16 +02:00
}}
>
Rejoindre !
</button>
</div>
)}
</div>
2022-05-18 10:15:54 +02:00
</Layout>
);
}