Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(auth): add fancy sign-in/sign-up errors
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Mar 29, 2019
1 parent 7903b0a commit bedcb9c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
6 changes: 2 additions & 4 deletions front/src/domain/store/fetchOrFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Option } from 'tsoption'
import { Api } from '@front/domain/api'
import { ExtraArg, State } from '@front/domain/store'
import { getToken } from '@front/domain/user/selectors/getToken'
import { tryOr } from '@front/helpers/tryOr'

interface FetchActions {
request: () => AnyAction
Expand Down Expand Up @@ -41,10 +42,7 @@ export const fetchOrFail = (

dispatch(success())
} catch (e) {
dispatch(failure(e.message))

// tslint:disable-next-line:no-console
console.log(e.code) // Try log HTTP errors
dispatch(failure(tryOr(() => e.response.data.message, e.message)))

throw e
}
Expand Down
2 changes: 2 additions & 0 deletions front/src/features/landing/features/sign-in/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { pushRoute } from '@front/features/routing'
import { InputType } from '@front/ui/components/form/input/InputType'
import { Label } from '@front/ui/components/form/label'
import { LoadingButton } from '@front/ui/components/form/loading-button'
import { useErrorAlert } from '@front/ui/hooks/useErrorAlert'
import { Card } from '@front/ui/components/layout/card'

import * as styles from '../SignForm.css'
Expand All @@ -23,6 +24,7 @@ export const SignIn = () => {
}, [])

const fetching = useMappedState(getSignInFetching)
useErrorAlert(fetching.error)

return (
<Form onSubmit={onSubmit}>
Expand Down
2 changes: 2 additions & 0 deletions front/src/features/landing/features/sign-up/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { pushRoute } from '@front/features/routing'
import { InputType } from '@front/ui/components/form/input/InputType'
import { Label } from '@front/ui/components/form/label'
import { LoadingButton } from '@front/ui/components/form/loading-button'
import { useErrorAlert } from '@front/ui/hooks/useErrorAlert'
import { Card } from '@front/ui/components/layout/card'

import * as styles from '../SignForm.css'
Expand All @@ -23,6 +24,7 @@ export const SignUp = () => {
}, [])

const fetching = useMappedState(getSignUpFetching)
useErrorAlert(fetching.error)

return (
<Form onSubmit={onSubmit}>
Expand Down
7 changes: 7 additions & 0 deletions front/src/helpers/tryOr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const tryOr = <T>(calculate: () => T, or: T): T => {
try {
return calculate()
} catch (e) {
return or
}
}
18 changes: 18 additions & 0 deletions front/src/ui/hooks/useErrorAlert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Option } from 'tsoption'
import { useEffect } from 'react'
import { notification } from 'antd'

export const useErrorAlert = (
error: Option<string>,
customMessage?: string,
) => {
useEffect(() => {
if (error.nonEmpty()) {
const message = !!customMessage ? customMessage : error.get()

notification.error({
message,
})
}
}, [error])
}

0 comments on commit bedcb9c

Please sign in to comment.