Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

492 nahradit stranku verifikacia dialogom #497

Merged
merged 4 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/Alert/AlertBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const AlertBox: FC = () => {
title: container.alertBox?.title ?? '',
isOpen: false,
})
if (container.alertBox?.onCloseCallback) container.alertBox.onCloseCallback()
}

return (
Expand Down
17 changes: 13 additions & 4 deletions src/components/RegisterForm/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const defaultValues: RegisterFormValues = {
}

export const RegisterForm: FC = () => {
const {handleSubmit, control, watch, setValue, getValues, setError, reset} = useForm<RegisterFormValues>({
const {handleSubmit, control, watch, setValue, getValues, setError} = useForm<RegisterFormValues>({
defaultValues,
values: defaultValues,
})
Expand Down Expand Up @@ -85,7 +85,17 @@ export const RegisterForm: FC = () => {
return axios.post<IGeneralPostResponse>(`/api/user/registration?seminar=${seminar}`, transformFormData(data))
},
// TODO: show alert/toast and redirect to homepage instead of redirect to info page
onSuccess: () => router.push(`${router.asPath}/../verifikacia`),
onSuccess: () =>
alert(
'Verifikačný e-mail bol odoslaný na zadanú e-mailovú adresu. Ak ho do pár minút neuvidíš, skontroluj, či ti náhodou neprišiel do priečinku spam',
{
title: 'Registrácia',
onCloseCallback: () => {
setOverride(true)
router.push(`${router.asPath}/../`)
},
},
),
onError: (error: AxiosError<RegisterErrorResponseData>) => {
if (error.response?.status === 400) {
if (error.response.data.email) {
Expand All @@ -98,7 +108,6 @@ export const RegisterForm: FC = () => {
})

const onSubmit: SubmitHandler<RegisterFormValues> = (data) => {
reset(undefined, {keepValues: true})
submitFormData(data)
}

Expand All @@ -112,7 +121,7 @@ export const RegisterForm: FC = () => {

const [dialogOpen, setDialogOpen] = useState(false)

const {continueNavigation} = useNavigationTrap({
const {continueNavigation, setOverride} = useNavigationTrap({
shouldBlockNavigation: isDirty,
onNavigate: () => {
setDialogOpen(true)
Expand Down
11 changes: 0 additions & 11 deletions src/components/Verification/Verification.tsx

This file was deleted.

9 changes: 0 additions & 9 deletions src/pages/malynar/verifikacia/[[...params]].tsx

This file was deleted.

9 changes: 0 additions & 9 deletions src/pages/matik/verifikacia/[[...params]].tsx

This file was deleted.

15 changes: 0 additions & 15 deletions src/pages/strom/verifikacia/[[...params]].tsx

This file was deleted.

1 change: 1 addition & 0 deletions src/utils/AlertContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface AlertProps {
isOpen: boolean
title?: string
message?: string
onCloseCallback?: () => void
}

const useAlertBox = () => {
Expand Down
9 changes: 7 additions & 2 deletions src/utils/useAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import {AlertContainer} from '@/utils/AlertContainer'
export const useAlert = () => {
const container = useContainer(AlertContainer)

const alert = (message: string, title?: string) => {
container.setAlertBox({message: message, title: title ?? 'Upozornenie', isOpen: true})
const alert = (message: string, options?: {title?: string; onCloseCallback?: () => void}) => {
container.setAlertBox({
message: message,
title: options?.title ?? 'Upozornenie',
isOpen: true,
onCloseCallback: options?.onCloseCallback,
})
}

return {alert}
Expand Down
9 changes: 5 additions & 4 deletions src/utils/useNavigationTrap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useRouter} from 'next/router'
import {useCallback, useEffect, useRef} from 'react'
import {useCallback, useEffect, useRef, useState} from 'react'

interface NavigationTrapProps {
shouldBlockNavigation: boolean
Expand All @@ -12,6 +12,7 @@ export const useNavigationTrap = ({shouldBlockNavigation, onNavigate}: Navigatio
const currentPath = router.asPath
const nextPath = useRef('')
const navConfirmed = useRef(false)
const [override, setOverride] = useState(false)

const killNavigation = useCallback(() => {
router.events.emit('routeChangeError', '', '', {shallow: false})
Expand All @@ -22,7 +23,7 @@ export const useNavigationTrap = ({shouldBlockNavigation, onNavigate}: Navigatio

useEffect(() => {
const pageNavigate = (path: string) => {
if (navConfirmed.current) return
if (override || navConfirmed.current) return
if (shouldBlockNavigation && path !== currentPath) {
nextPath.current = path
onNavigate()
Expand All @@ -41,12 +42,12 @@ export const useNavigationTrap = ({shouldBlockNavigation, onNavigate}: Navigatio
router.events.off('routeChangeStart', pageNavigate)
window.removeEventListener('beforeunload', pageExit)
}
}, [shouldBlockNavigation, currentPath, killNavigation, onNavigate, router.events])
}, [shouldBlockNavigation, currentPath, killNavigation, onNavigate, override, router.events])

const continueNavigation = () => {
navConfirmed.current = true
router.push(nextPath.current)
}

return {nextPath, continueNavigation}
return {nextPath, continueNavigation, setOverride}
}
Loading