Skip to content

Commit

Permalink
refactor(actions/user): Extract code for showing a generic alert.
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Sep 1, 2023
1 parent f6b2a82 commit 6938b8f
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions lib/actions/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export const rememberStop = createAction('REMEMBER_STOP')
const rememberLocalUserPlace = createAction('REMEMBER_LOCAL_USER_PLACE')
const deleteRecentPlace = createAction('DELETE_LOCAL_USER_RECENT_PLACE')

function genericErrorAlert(intl, messageObject) {
if (intl) {
alert(
intl.formatMessage(
{ id: 'actions.user.genericError' },
{ err: JSON.stringify(messageObject) }
)
)
}
}

function createNewUser(auth0User) {
return {
accessibilityRoutingByDefault: false,
Expand Down Expand Up @@ -335,14 +346,7 @@ export function createOrUpdateUser(userData, silentOnSuccess = false, intl) {
// (This sorts saved places, and, for existing users, fetches trips.)
dispatch(setUser(returnedUser, isCreatingUser))
} else {
if (intl) {
alert(
intl.formatMessage(
{ id: 'actions.user.genericError' },
{ err: JSON.stringify(message) }
)
)
}
genericErrorAlert(intl, message)
}
}
}
Expand Down Expand Up @@ -381,12 +385,7 @@ export function deleteUser(userData, auth0, intl) {
// Log out user and route them to the home page.
auth0.logout({ returnTo: window.location.origin })
} else {
alert(
intl.formatMessage(
{ id: 'actions.user.genericError' },
{ err: JSON.stringify(message) }
)
)
genericErrorAlert(intl, message)
}
}
}
Expand Down Expand Up @@ -465,12 +464,7 @@ export function createOrUpdateUserMonitoredTrip(
// Finally, navigate to the saved trips page.
dispatch(routeTo(TRIPS_PATH))
} else {
alert(
intl.formatMessage(
{ id: 'actions.user.genericError' },
{ err: JSON.stringify(message) }
)
)
genericErrorAlert(intl, message)
}
}
}
Expand Down Expand Up @@ -531,12 +525,7 @@ export function confirmAndDeleteUserMonitoredTrip(tripId, intl) {
// Reload user's monitored trips after deletion.
dispatch(fetchMonitoredTrips())
} else {
alert(
intl.formatMessage(
{ id: 'actions.user.genericError' },
{ err: JSON.stringify(message) }
)
)
genericErrorAlert(intl, message)
}
}
}
Expand Down Expand Up @@ -579,12 +568,7 @@ export function requestPhoneVerificationSms(newPhoneNumber, intl) {
// Refetch user and update application state with new phone number and verification status.
dispatch(fetchOrInitializeUser())
} else {
alert(
intl.formatMessage(
{ id: 'actions.user.genericError' },
{ err: JSON.stringify(message) }
)
)
genericErrorAlert(intl, message)
}
} else {
// Alert user if they have been throttled.
Expand Down

0 comments on commit 6938b8f

Please sign in to comment.