Last active
July 6, 2021 20:54
-
-
Save andrelandgraf/2a403e7ba5e39924bd8675c92d54ac9d to your computer and use it in GitHub Desktop.
Communication of status / errors codes between client and server via URLSearchParams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useEffect } from 'react'; | |
import { useSearchParams } from 'react-router-dom'; | |
export default function useErrorNotifications() { | |
const { pushNotification } = useContext(NotificationsContext); | |
const [searchParams, setSearchParams] = useSearchParams(); | |
const errorCode = searchParams.get('error'); | |
const onClose = useCallback(() => { | |
searchParams.delete('error'); | |
setSearchParams(searchParams, { replace: true }); | |
}, [searchParams, setSearchParams]); | |
useEffect(() => { | |
if (errorCode) { | |
const [title, message] = getErrorMessage(errorCode); | |
pushNotification({ | |
title: title, | |
message: message, | |
isPermanent: true, | |
isToast: false, | |
type: MessageType.error, | |
onClose, | |
}); | |
} | |
}, [errorCode, onClose]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment