diff --git a/src/pages/admin.tsx b/src/pages/admin.tsx index 11884a9b..ac403db3 100644 --- a/src/pages/admin.tsx +++ b/src/pages/admin.tsx @@ -11,6 +11,7 @@ import {Tab} from "@headlessui/react"; import clsx from "clsx"; import Lists from "./(admin)/Lists"; import BatchCodeGenerator from "./(admin)/BatchCodeGenerator"; +import {shouldRedirectHome} from "@/utils/navigation.disabled"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -26,7 +27,7 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; } - if (user.type !== "developer") { + if (shouldRedirectHome(user) || user.type !== "developer") { res.setHeader("location", "/"); res.statusCode = 302; res.end(); diff --git a/src/pages/exam.tsx b/src/pages/exam.tsx index 6b2ea0ec..1023196f 100644 --- a/src/pages/exam.tsx +++ b/src/pages/exam.tsx @@ -34,6 +34,7 @@ import AbandonPopup from "@/components/AbandonPopup"; import {evaluateSpeakingAnswer, evaluateWritingAnswer} from "@/utils/evaluation"; import {useRouter} from "next/router"; import {getExam} from "@/utils/exams"; +import {shouldRedirectHome} from "@/utils/navigation.disabled"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -49,6 +50,17 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; } + if (shouldRedirectHome(user)) { + res.setHeader("location", "/"); + res.statusCode = 302; + res.end(); + return { + props: { + user: null, + }, + }; + } + return { props: {user: req.session.user}, }; diff --git a/src/pages/exercises.tsx b/src/pages/exercises.tsx index 622cdb82..a4f66e32 100644 --- a/src/pages/exercises.tsx +++ b/src/pages/exercises.tsx @@ -37,6 +37,7 @@ import AbandonPopup from "@/components/AbandonPopup"; import {evaluateSpeakingAnswer, evaluateWritingAnswer} from "@/utils/evaluation"; import {useRouter} from "next/router"; import {getExam} from "@/utils/exams"; +import {shouldRedirectHome} from "@/utils/navigation.disabled"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -52,6 +53,17 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; } + if (shouldRedirectHome(user)) { + res.setHeader("location", "/"); + res.statusCode = 302; + res.end(); + return { + props: { + user: null, + }, + }; + } + return { props: {user: req.session.user}, }; diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index 1627a739..cb8e3b9d 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -17,6 +17,7 @@ import {EmploymentStatus, EMPLOYMENT_STATUS, Gender, User} from "@/interfaces/us import countryCodes from "country-codes-list"; import {countries, TCountries} from "countries-list"; import CountrySelect from "@/components/Low/CountrySelect"; +import {shouldRedirectHome} from "@/utils/navigation.disabled"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -32,6 +33,17 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; } + if (shouldRedirectHome(user)) { + res.setHeader("location", "/"); + res.statusCode = 302; + res.end(); + return { + props: { + user: null, + }, + }; + } + return { props: {user: req.session.user}, }; diff --git a/src/pages/record.tsx b/src/pages/record.tsx index dab44fa4..4f4c5868 100644 --- a/src/pages/record.tsx +++ b/src/pages/record.tsx @@ -21,6 +21,7 @@ import {calculateBandScore} from "@/utils/score"; import {BsBook, BsHeadphones, BsMegaphone, BsPen} from "react-icons/bs"; import Select from "react-select"; import useGroups from "@/hooks/useGroups"; +import {shouldRedirectHome} from "@/utils/navigation.disabled"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -36,6 +37,17 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; } + if (shouldRedirectHome(user)) { + res.setHeader("location", "/"); + res.statusCode = 302; + res.end(); + return { + props: { + user: null, + }, + }; + } + return { props: {user: req.session.user}, }; diff --git a/src/pages/stats.tsx b/src/pages/stats.tsx index c806429f..54d873f9 100644 --- a/src/pages/stats.tsx +++ b/src/pages/stats.tsx @@ -21,6 +21,7 @@ import Select from "react-select"; import useGroups from "@/hooks/useGroups"; import DatePicker from "react-datepicker"; import moment from "moment"; +import {shouldRedirectHome} from "@/utils/navigation.disabled"; ChartJS.register(LinearScale, CategoryScale, PointElement, LineElement, LineController, Legend, Tooltip); @@ -40,6 +41,17 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; } + if (shouldRedirectHome(user)) { + res.setHeader("location", "/"); + res.statusCode = 302; + res.end(); + return { + props: { + user: null, + }, + }; + } + return { props: {user: req.session.user}, }; diff --git a/src/pages/test.tsx b/src/pages/test.tsx deleted file mode 100644 index c8b17112..00000000 --- a/src/pages/test.tsx +++ /dev/null @@ -1,190 +0,0 @@ -/* eslint-disable @next/next/no-img-element */ - -import Head from "next/head"; -import Navbar from "@/components/Navbar"; -import {ToastContainer} from "react-toastify"; -import {withIronSessionSsr} from "iron-session/next"; -import {sessionOptions} from "@/lib/session"; -import useUser from "@/hooks/useUser"; -import Sidebar from "@/components/Sidebar"; -import dynamic from "next/dynamic"; -import {BsCheckCircleFill, BsMicFill, BsPauseCircle, BsPauseFill, BsPlayCircle, BsPlayFill, BsTrashFill} from "react-icons/bs"; -import {useEffect, useState} from "react"; -import Layout from "@/components/High/Layout"; - -const Waveform = dynamic(() => import("../components/Waveform"), {ssr: false}); -const ReactMediaRecorder = dynamic(() => import("react-media-recorder").then((mod) => mod.ReactMediaRecorder), { - ssr: false, -}); - -export const getServerSideProps = withIronSessionSsr(({req, res}) => { - const user = req.session.user; - - if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); - return { - props: { - user: null, - }, - }; - } - - return { - props: {user: req.session.user}, - }; -}, sessionOptions); - -export default function Page() { - const {user} = useUser({redirectTo: "/login"}); - const [recordingDuration, setRecordingDuration] = useState(0); - const [isRecording, setIsRecording] = useState(false); - const [mediaBlob, setMediaBlob] = useState(); - - useEffect(() => { - let recordingInterval: NodeJS.Timer | undefined = undefined; - if (isRecording) { - recordingInterval = setInterval(() => setRecordingDuration((prev) => prev + 1), 1000); - } else if (recordingInterval) { - clearInterval(recordingInterval); - } - - return () => { - if (recordingInterval) clearInterval(recordingInterval); - }; - }, [isRecording]); - - return ( - <> - - Exam | EnCoach - - - - - - {user && ( - - setMediaBlob(blob)} - render={({status, startRecording, stopRecording, pauseRecording, resumeRecording, clearBlobUrl, mediaBlobUrl}) => ( -
-

Record your answer:

-
- {status === "idle" && ( - <> -
- {status === "idle" && ( - { - setRecordingDuration(0); - startRecording(); - setIsRecording(true); - }} - className="h-5 w-5 text-mti-gray-cool cursor-pointer" - /> - )} - - )} - {status === "recording" && ( - <> -
- - {Math.round(recordingDuration / 60) - .toString(10) - .padStart(2, "0")} - : - {Math.round(recordingDuration % 60) - .toString(10) - .padStart(2, "0")} - -
-
-
- { - setIsRecording(false); - pauseRecording(); - }} - className="text-red-500 w-8 h-8 cursor-pointer" - /> - { - setIsRecording(false); - stopRecording(); - }} - className="text-mti-purple-light w-8 h-8 cursor-pointer" - /> -
- - )} - {status === "paused" && ( - <> -
- - {Math.floor(recordingDuration / 60) - .toString(10) - .padStart(2, "0")} - : - {Math.floor(recordingDuration % 60) - .toString(10) - .padStart(2, "0")} - -
-
-
- { - setIsRecording(true); - resumeRecording(); - }} - className="text-mti-purple-light w-8 h-8 cursor-pointer" - /> - { - setIsRecording(false); - stopRecording(); - }} - className="text-mti-purple-light w-8 h-8 cursor-pointer" - /> -
- - )} - {status === "stopped" && mediaBlobUrl && ( - <> - -
- { - setRecordingDuration(0); - clearBlobUrl(); - }} - /> - - { - clearBlobUrl(); - setRecordingDuration(0); - startRecording(); - setIsRecording(true); - }} - className="h-5 w-5 text-mti-gray-cool cursor-pointer" - /> -
- - )} -
-
- )} - /> - - )} - - ); -} diff --git a/src/pages/users.tsx b/src/pages/users.tsx deleted file mode 100644 index f6a692c5..00000000 --- a/src/pages/users.tsx +++ /dev/null @@ -1,101 +0,0 @@ -import {withIronSessionSsr} from "iron-session/next"; -import {sessionOptions} from "@/lib/session"; -import {Type, User} from "@/interfaces/user"; -import Head from "next/head"; -import {Avatar} from "primereact/avatar"; -import {useEffect, useState} from "react"; -import {FilterMatchMode, FilterOperator} from "primereact/api"; -import useUsers from "@/hooks/useUsers"; -import {DataTable} from "primereact/datatable"; -import {Column} from "primereact/column"; -import {capitalize} from "lodash"; -import {levelCalculator} from "@/resources/level"; -import {Dropdown} from "primereact/dropdown"; - -export const getServerSideProps = withIronSessionSsr(({req, res}) => { - const user = req.session.user; - - if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); - return { - props: { - user: null, - }, - }; - } - - return { - props: {user: req.session.user}, - }; -}, sessionOptions); - -export default function Users({user}: {user: User}) { - const {users, isLoading} = useUsers(); - const [filters] = useState({ - name: {value: null, matchMode: FilterMatchMode.CONTAINS}, - type: {value: null, matchMode: FilterMatchMode.EQUALS}, - }); - - const userTypes: Type[] = ["admin", "developer", "owner", "student", "teacher"]; - - const typeRowFilterTemplate = (options: any) => { - return ( - options.filterApplyCallback(e.value)} - placeholder="Select One" - className="p-column-filter" - showClear - style={{minWidth: "12rem"}} - /> - ); - }; - - return ( - <> - - EnCoach | Users - - - - -
-
- - - - - levelCalculator(data.experience).currentLevel} /> - capitalize(data.type)} - /> - -
-
- - ); -} diff --git a/src/utils/navigation.disabled.ts b/src/utils/navigation.disabled.ts index 6b2ffb76..4b3a85e4 100644 --- a/src/utils/navigation.disabled.ts +++ b/src/utils/navigation.disabled.ts @@ -1,5 +1,17 @@ +import {User} from "@/interfaces/user"; +import moment from "moment"; + export const preventNavigation = (navDisabled: boolean, focusMode: boolean): boolean => { if (navDisabled) return true; if (focusMode) return true; return false; }; + +export const shouldRedirectHome = (user: User) => { + if (user.isDisabled) return true; + if (user.isFirstLogin) return true; + if (!user.demographicInformation) return true; + if (user.subscriptionExpirationDate && moment(new Date()).isAfter(user.subscriptionExpirationDate)) return true; + + return false; +};