-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use switch and abstract helper texts
- Loading branch information
Showing
1 changed file
with
22 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
||
|
@@ -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 => { | ||
|
@@ -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 | ||
|