Skip to content

Commit

Permalink
feat: Add auto registration parameter to URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin authored and danicuki committed Sep 20, 2024
1 parent e2e2f30 commit 0655fed
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pages/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function authPage() {
>
{t('form.no_account')}{' '}
<Link
href="/auth/signup"
href={{ pathname: "/auth/signup", query: { from: Router.asPath } }} // Added Router.asPath to the signup URL
className="cursor-pointer text-sm font-medium leading-none text-primary-300 hover:text-gray-500 hover:no-underline focus:text-gray-500 focus:no-underline focus:outline-none dark:text-primary-300"
>
<span className="cursor-pointer text-primary-300 transition duration-150 ease-in-out hover:text-primary-400 dark:text-primary-300">
Expand Down
12 changes: 11 additions & 1 deletion pages/auth/signup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 22 additions & 2 deletions pages/courses/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NotFound />
Expand All @@ -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)) {
Expand All @@ -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])

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 (
<>
<Head>
Expand Down Expand Up @@ -456,4 +476,4 @@ export async function getServerSideProps({ params }) {
}
}

export default withProtected(Course)
export default withProtected(Course)
3 changes: 2 additions & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion public/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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?",
Expand Down

0 comments on commit 0655fed

Please sign in to comment.