Skip to content

Commit

Permalink
modify error page redirects (#2518) (#2532)
Browse files Browse the repository at this point in the history
(cherry picked from commit 84a3f8f)

Co-authored-by: Doug Lauder <[email protected]>
  • Loading branch information
mattermost-build and wiggin77 authored Mar 11, 2022
1 parent fa348e3 commit f0ddb87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 2 additions & 1 deletion webapp/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@
"error.workspace-undefined": "Not a valid workspace.",
"error.page.title": "Sorry, something went wrong",
"error.not-logged-in": "Your session may have expired or you're not logged in.",
"error.go-dashboard": "Go to the Dashboard",
"error.back-to-home": "Back to Home",
"error.back-to-boards": "Back to boards",
"error.go-login": "Log in",
"error.unknown": "An error occurred.",
"login.register-button": "or create an account if you don't have one",
Expand Down
15 changes: 10 additions & 5 deletions webapp/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ type ErrorDef = {
button1Text: string
button1Redirect: string | (() => string)
button1Fill: boolean
button1ClearHistory: boolean

button2Enabled: boolean
button2Text: string
button2Redirect: string | (() => string)
button2Fill: boolean
button2ClearHistory: boolean
}

function errorDefFromId(id: ErrorId | null): ErrorDef {
Expand All @@ -29,10 +31,12 @@ function errorDefFromId(id: ErrorId | null): ErrorDef {
button1Text: '',
button1Redirect: '',
button1Fill: false,
button1ClearHistory: false,
button2Enabled: false,
button2Text: '',
button2Redirect: '',
button2Fill: false,
button2ClearHistory: false,
}

const intl = useIntl()
Expand All @@ -41,25 +45,26 @@ function errorDefFromId(id: ErrorId | null): ErrorDef {
case ErrorId.WorkspaceUndefined: {
errDef.title = intl.formatMessage({id: 'error.workspace-undefined', defaultMessage: 'Not a valid workspace.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.go-dashboard', defaultMessage: 'Go to the Dashboard'})
errDef.button1Redirect = '/dashboard'
errDef.button1Text = intl.formatMessage({id: 'error.back-to-home', defaultMessage: 'Back to Home'})
errDef.button1Redirect = window.location.origin
errDef.button1Fill = true
break
}
case ErrorId.NotLoggedIn: {
errDef.title = intl.formatMessage({id: 'error.not-logged-in', defaultMessage: 'Your session may have expired or you\'re not logged in. Log in again to access Boards.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.go-login', defaultMessage: 'Log in'})
errDef.button1Text = intl.formatMessage({id: 'error.go-login', defaultMessage: 'Login'})
errDef.button1Redirect = '/login'
errDef.button1Fill = true
break
}
default: {
errDef.title = intl.formatMessage({id: 'error.unknown', defaultMessage: 'An error occurred.'})
errDef.button1Enabled = true
errDef.button1Text = intl.formatMessage({id: 'error.go-dashboard', defaultMessage: 'Go to the Dashboard'})
errDef.button1Redirect = '/dashboard'
errDef.button1Text = intl.formatMessage({id: 'error.back-to-boards', defaultMessage: 'Go to the Dashboard'})
errDef.button1Redirect = '/'
errDef.button1Fill = true
errDef.button1ClearHistory = true
break
}
}
Expand Down
23 changes: 16 additions & 7 deletions webapp/src/pages/errorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,39 @@ import Button from '../widgets/buttons/button'
import './errorPage.scss'

import {errorDefFromId, ErrorId} from '../errors'
import {UserSettings} from '../userSettings'

const ErrorPage = () => {
const history = useHistory()
const queryString = new URLSearchParams(useLocation().search)
const errid = queryString.get('id')
const errorDef = errorDefFromId(errid as ErrorId)

const handleButtonClick = useCallback((path: string | (()=>string)) => {
let url = '/dashboard'
const handleButtonClick = useCallback((path: string | (()=>string), clearHistory: boolean) => {
let url = '/'
if (typeof path === 'function') {
url = path()
} else if (path) {
url = path as string
}
history.push(url)
if (clearHistory) {
UserSettings.lastBoardId = ''
UserSettings.lastViewId = ''
}
if (url === window.location.origin) {
window.location.href = url
} else {
history.push(url)
}
}, [history])

const makeButton = ((path: string | (()=>string), txt: string, fill: boolean) => {
const makeButton = ((path: string | (()=>string), txt: string, fill: boolean, clearHistory: boolean) => {
return (
<Button
filled={fill}
size='large'
onClick={async () => {
handleButtonClick(path)
handleButtonClick(path, clearHistory)
}}
>
{txt}
Expand All @@ -56,10 +65,10 @@ const ErrorPage = () => {
<ErrorIllustration/>
<br/>
{
(errorDef.button1Enabled ? makeButton(errorDef.button1Redirect, errorDef.button1Text, errorDef.button1Fill) : null)
(errorDef.button1Enabled ? makeButton(errorDef.button1Redirect, errorDef.button1Text, errorDef.button1Fill, errorDef.button1ClearHistory) : null)
}
{
(errorDef.button2Enabled ? makeButton(errorDef.button2Redirect, errorDef.button2Text, errorDef.button2Fill) : null)
(errorDef.button2Enabled ? makeButton(errorDef.button2Redirect, errorDef.button2Text, errorDef.button2Fill, errorDef.button2ClearHistory) : null)
}
</div>
</div>
Expand Down

0 comments on commit f0ddb87

Please sign in to comment.