From 0655fedcd5f3b992834788dfde2264d8444f094d Mon Sep 17 00:00:00 2001 From: Yan Luiz Date: Wed, 18 Sep 2024 19:48:03 -0300 Subject: [PATCH] feat: Add auto registration parameter to URL --- pages/auth/index.js | 2 +- pages/auth/signup/index.js | 12 +++++++++++- pages/courses/[id].js | 24 ++++++++++++++++++++++-- public/locales/en/translation.json | 3 ++- public/locales/pt-BR/translation.json | 3 ++- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/pages/auth/index.js b/pages/auth/index.js index da0a55e2..dd6ece3f 100644 --- a/pages/auth/index.js +++ b/pages/auth/index.js @@ -83,7 +83,7 @@ function authPage() { > {t('form.no_account')}{' '} diff --git a/pages/auth/signup/index.js b/pages/auth/signup/index.js index 927e5c2b..534efec5 100644 --- a/pages/auth/signup/index.js +++ b/pages/auth/signup/index.js @@ -9,13 +9,23 @@ import { withPublic } from '../../../hooks/route' import { Button } from '@nextui-org/react' import { FcGoogle } from 'react-icons/fc' import { GrGithub } from 'react-icons/gr' +import { useRouter } from 'next/router' function signUpPage() { const { signup, loginGoogle, loginGithub } = useAuth() const [showpass, setShowPass] = useState(false) + const router = useRouter() const { register, handleSubmit } = useForm() - const onSignUpSubmit = (data) => signup(data) + const onSignUpSubmit = async (data) => { + try { + await signup(data) + const destination = router.query.from || '/courses' + router.push(destination) + } catch (error) { + toast.error(error.message) + } + } const onSignUpError = (errors, e) => { toast.error(errors, e, { position: 'top-right', diff --git a/pages/courses/[id].js b/pages/courses/[id].js index 3f7923e6..e7989b5c 100644 --- a/pages/courses/[id].js +++ b/pages/courses/[id].js @@ -22,6 +22,7 @@ import { dateFormat } from '../../lib/dateFormat' import { useTranslation } from 'react-i18next' import RenderField from '../../components/RenderField' import { toast } from 'react-toastify' +import { useRouter } from 'next/router' function Course({ course, currentDate }) { if (!course.active) return @@ -38,6 +39,8 @@ function Course({ course, currentDate }) { const courseTitle = course?.metadata?.[language]?.title || course?.title const courseDescription = course?.metadata?.[language]?.description || course?.description const courseSections = course?.metadata?.[language]?.sections || course?.sections + const router = useRouter() + const { auto_subscribe } = router.query useEffect(() => { if (course?.metadata && !course.metadata.hasOwnProperty(language)) { @@ -55,7 +58,8 @@ function Course({ course, currentDate }) { }, [user, cohort]) useEffect(async () => { if (cohorts) { - setCohort(getCurrentCohort(user, cohorts, course, currentDate)) + let currentCohort = getCurrentCohort(user, cohorts, course, currentDate) + setCohort(currentCohort) } }, [cohorts, user]) @@ -92,6 +96,7 @@ function Course({ course, currentDate }) { const registerUserInCohort = async () => { await registerUserInCohortInFirestore(cohort.id, auth.currentUser.uid) setRegisterOnCohort(true) + toast.success(t('messages.course_registration_success')) } const userIsRegisteredInCurrentCohort = () => { return !!user?.cohorts.find( @@ -241,6 +246,21 @@ function Course({ course, currentDate }) { }) } + useEffect(() => { + const autoSubscribe = async () => { + if (auto_subscribe === 'true' && user && cohort && !userIsRegisteredInCurrentCohort()) { + try { + await registerUserInCohort() + router.replace(`/courses/${course.id}`, undefined, { shallow: true }) + } catch (error) { + console.error('Error when auto_subscribing:', error) + } + } + } + + autoSubscribe() + }, [user, cohort, auto_subscribe]) + return ( <> @@ -456,4 +476,4 @@ export async function getServerSideProps({ params }) { } } -export default withProtected(Course) +export default withProtected(Course) \ No newline at end of file diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 27859251..b4cb707a 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -129,7 +129,8 @@ "already_on_first_lesson": "You are already on the first lesson.", "data_updated_success": "Data updated successfully!", "error_updating_data": "Error updating data", - "language_not_available": "Content not available for the selected language. Using the default language." + "language_not_available": "Content not available for the selected language. Using the default language.", + "course_registration_success": "Successfully registered for the course!" }, "twitter-share": "Networking is everything, how about sharing your progress with your friends on Twitter?", "buttons": { diff --git a/public/locales/pt-BR/translation.json b/public/locales/pt-BR/translation.json index dc546bc7..c40e54c8 100644 --- a/public/locales/pt-BR/translation.json +++ b/public/locales/pt-BR/translation.json @@ -129,7 +129,8 @@ "already_on_first_lesson": "Você já está na primeira lição.", "data_updated_success": "Dados atualizados com sucesso!", "error_updating_data": "Erro ao atualizar dados", - "language_not_available": "Conteúdo não disponível para o idioma selecionado. Usando o idioma padrão." + "language_not_available": "Conteúdo não disponível para o idioma selecionado. Usando o idioma padrão.", + "course_registration_success": "Registrado no curso com sucesso!" }, "twitter-share": "Networking é tudo, que tal compartilhar seu progresso com os seus amigos no Twitter?", "link_github_account": "Deseja vincular o github à sua conta já cadastrada?",