Skip to content

Commit

Permalink
Fix Vetrina module
Browse files Browse the repository at this point in the history
* Add InitializationError constructor to OrderFailure
* Remove UnexpectedServerError from PurchaseState
* Change prop unexpectedError type to JSX
* Render external view on InitializationError, UnexpectedError,
  ServerError
  • Loading branch information
ae-mo committed May 15, 2020
1 parent bb0c3ac commit 16575b1
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions packages/vetrina/src/Vetrina/Vetrina.purs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Props =
{ onClose :: Effect Unit
, onLogin :: Effect Unit
, products :: Either Error (Array Product)
, unexpectedError :: Maybe JSX
, unexpectedError :: JSX
}

fromJSProps :: JSProps -> Props
Expand All @@ -63,7 +63,7 @@ fromJSProps jsProps =
, not null products -> Right products
| otherwise -> Left productError
Nothing -> Left productError
, unexpectedError : toMaybe jsProps.unexpectedError
, unexpectedError : fromMaybe mempty $ toMaybe jsProps.unexpectedError
}

type State =
Expand All @@ -76,7 +76,6 @@ type State =
, products :: Array Product
, productSelection :: Maybe Product
, paymentMethod :: User.PaymentMethod
, unexpectedError :: JSX
}

type Self = React.Self Props State
Expand All @@ -92,11 +91,11 @@ data PurchaseState
| PurchaseFailed OrderFailure
| PurchaseSetPassword
| PurchaseCompleted AccountStatus
| UnexpectedServerError

data OrderFailure
= EmailInUse String
| SubscriptionExists
| InitializationError
| FormFieldError (Array NewPurchase.FormInputField)
| AuthenticationError
| ServerError
Expand Down Expand Up @@ -126,41 +125,34 @@ initialState =
, products: []
, productSelection: Nothing
, paymentMethod: CreditCard
, unexpectedError: mempty
}

didMount :: Self -> Effect Unit
didMount self =
case self.props.unexpectedError of
Just view ->
self.setState _ { purchaseState = UnexpectedServerError
, unexpectedError = view
}
Nothing -> do
sentryDsn <- sentryDsn_
logger <- Sentry.mkLogger sentryDsn Nothing
self.setState _ { logger = logger }
-- Before rendering the form, we need to:
-- 1. fetch the user if access token is found in the browser
Aff.launchAff_ do
Aff.finally
-- When user has been fetched, hide loading spinner
(liftEffect $ self.setState \s -> s { isLoading = Nothing })
do
-- Try to login with local storage information and set user to state
tryMagicLogin self

products <- liftEffect $ case self.props.products of
Right p -> pure p
Left err -> do
self.setState _ { purchaseState = PurchaseFailed $ UnexpectedError "" }
logger.error err
throwError err

liftEffect $ self.setState _ { products = products }
-- If there is only one product given, automatically select that for the customer
when (length products == 1) $
liftEffect $ self.setState _ { productSelection = head products }
didMount self = do
sentryDsn <- sentryDsn_
logger <- Sentry.mkLogger sentryDsn Nothing
self.setState _ { logger = logger }
-- Before rendering the form, we need to:
-- 1. fetch the user if access token is found in the browser
Aff.launchAff_ do
Aff.finally
-- When user has been fetched, hide loading spinner
(liftEffect $ self.setState \s -> s { isLoading = Nothing })
do
-- Try to login with local storage information and set user to state
tryMagicLogin self

products <- liftEffect $ case self.props.products of
Right p -> pure p
Left err -> do
self.setState _ { purchaseState = PurchaseFailed $ InitializationError }
logger.error err
throwError err

liftEffect $ self.setState _ { products = products }
-- If there is only one product given, automatically select that for the customer
when (length products == 1) $
liftEffect $ self.setState _ { productSelection = head products }

tryMagicLogin :: Self -> Aff Unit
tryMagicLogin self =
Expand Down Expand Up @@ -265,6 +257,10 @@ render self = vetrinaContainer self $
, productSelection: self.state.productSelection
, onLogin: self.props.onLogin
}
ServerError -> self.props.unexpectedError
UnexpectedError _ -> self.props.unexpectedError
InitializationError ->
self.props.unexpectedError
_ ->
Purchase.Error.error
{ onRetry: onRetry
Expand All @@ -286,8 +282,6 @@ render self = vetrinaContainer self $
, user: self.state.user
, accountStatus
}
UnexpectedServerError ->
self.state.unexpectedError
where
onRetry = self.setState _ { purchaseState = NewPurchase }

Expand Down

0 comments on commit 16575b1

Please sign in to comment.