Skip to content

Commit

Permalink
refactor: use switch and abstract helper texts
Browse files Browse the repository at this point in the history
  • Loading branch information
foochifa committed Sep 15, 2023
1 parent a6c20ea commit b9ebcd9
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions frontend/src/features/admin-form/share/ShareFormModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
import dedent from 'dedent'
import { StatusCodes } from 'http-status-codes'

import { featureFlags, GO_VALIDATION_ERROR_MESSAGE } from '~shared/constants'
import {
featureFlags,
GO_ALREADY_EXIST_ERROR_MESSAGE,
GO_VALIDATION_ERROR_MESSAGE,
} from '~shared/constants'

import { BxsCheckCircle, BxsErrorCircle } from '~/assets/icons'

Expand Down Expand Up @@ -73,6 +77,12 @@ const goLinkClaimSuccessHelperText: goLinkHelperTextType = {
),
}

const GO_VALIDATION_FAILED_HELPER_TEXT =
'Short links should only consist of lowercase letters, numbers and hyphens.'
const GO_ALREADY_EXIST_HELPER_TEXT = 'Short link is already in use.'
const GO_UNEXPECTED_ERROR_HELPER_TEXT =
'Something went wrong. Try refreshing this page. If this issue persists, contact [email protected].'

const getGoLinkClaimFailureHelperText = (
text: string,
): goLinkHelperTextType => {
Expand Down Expand Up @@ -209,14 +219,19 @@ export const ShareFormModal = ({
} catch (err) {
setClaimGoLoading(false)

let errMessage =
'Something went wrong. Try refreshing this page. If this issue persists, contact [email protected].'
let errMessage = GO_UNEXPECTED_ERROR_HELPER_TEXT

if (err instanceof HttpError && err.code === StatusCodes.BAD_REQUEST)
errMessage =
err.message === GO_VALIDATION_ERROR_MESSAGE
? 'Short links should only consist of lowercase letters, numbers and hyphens.'
: 'Short link is already in use.'
switch (err.message) {
case GO_VALIDATION_ERROR_MESSAGE:
errMessage = GO_VALIDATION_FAILED_HELPER_TEXT
break
case GO_ALREADY_EXIST_ERROR_MESSAGE:
errMessage = GO_ALREADY_EXIST_HELPER_TEXT
break
default:
// will use unexpected error text
}

setGoLinkHelperText(getGoLinkClaimFailureHelperText(errMessage))
return
Expand Down

0 comments on commit b9ebcd9

Please sign in to comment.